关于继承与多态的简单认识

(1)继承条件下的构造方法调用

package Demo1;


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

输出:

结论:

在继承条件下,程序自动先调用父类的构造函数,后调用子类的构造函数。同时,如果使用super调用父类构造方法,必须将函数放在子类方法的第一句。

(2)直接输出类创建的对象

public class ExplorationJDKSource {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(new A());
    }

}

class A{}

输出:

根据Javap -c反汇编以上代码:

最后,可根据反汇编追查源码:

System.out.println的源码如下:

    public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }

其中的valueOf源码追查:

 public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

toString源码追查:

  public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

所以,我们可以明白了为何输出了一串很诡异的字符串。

(3)toString的简单认识

package Demo1;

public class Fruit {
    public String toString()
    {
        return "Fruit toString.";
    }

    public static void main(String args[])
    {
        Fruit f=new Fruit();
        System.out.println("f="+f);
        System.out.println("f="+f.toString());
    }

}

输出;

所以,在“+”号运算中,与字符串相连的对象总是默认调用toString方法。

(4)“变态”的多态

package Demo1;

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

输出:

结论:

当子类与父类拥有一样的方法,并且让一个父类变量引用一个子类对象时,到底调用哪个方法,由对象自己的“真实”类型所决定,这就是说:对象是子类型的,它就调用子类型的方法,是父类型的,它就调用父类型的方法。

原文地址:https://www.cnblogs.com/YXSZ/p/9903754.html