动手动脑 类与对象

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);
    }
}

 

posted @ 2019-11-03 19:57  嘻嘻哒丸子  阅读(90)  评论(0编辑  收藏  举报