其他分享
首页 > 其他分享> > 动手动脑 类与对象

动手动脑 类与对象

作者:互联网

class Grandparent 
{
    public Grandparent()
     {
            System.out.println("GrandParent Created.");
    
}
    public Grandparent(String string) 
    {
            System.out.println("GrandParent Created.String:" + string);
 }
}
class Parent extends Grandparent
{
    public Parent()
     {
            //super("Hello.Grandparent.");
            System.out.println("Parent Created");
       // super("Hello.Grandparent.");
      }
}
class Child extends Parent 
{
    public Child()
     {
        System.out.println("Child Created");
      }
}
public class TestInherits 
{
    public static void main(String args[])
     {
            Child c = new Child();  
  }
}
package o;
public class ParentChildTest {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();
         
        parent=child;
        parent.printValue();
         
        parent.myValue++;
        parent.printValue();
         
        ((Child)parent).myValue++;
        parent.printValue();       
    }
}
class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}

 

标签:parent,Parent,对象,动脑,printValue,动手,myValue,Child,public
来源: https://www.cnblogs.com/xiatian21/p/11788679.html