this指针多态性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014595589/article/details/82942826
public class Dervied extends Base {
    {
		System.out.println("Dervied Con1 "+ this.name 

);
		
	}
    private String name = "dervied";
    {
		System.out.println("Dervied Con2 "+ this.name 

);
	}
    protected String id="2";
    
    public Dervied() {
        System.out.println(this.getClass().getSimpleName()+" in Dervied() "+name);
        super.tellName();
        System.out.println(super.getClass().getSimpleName());
        tellName();
        printName();
    }

    public void tellName() {
        System.out.println("Dervied tell name: " + name);
    }

    public void printName() {
        System.out.println("Dervied print name: " + name);
    }
    
    public static void main(String[] args){

        new Dervied();
    }
}

class Base {

    private String name = "base";

    public Base() {
        System.out.println(this.getClass().getSimpleName()+" in Base() "+name);
        System.out.println(super.getClass().getSimpleName());
        tellName();
        printName();
    }

    public void tellName() {
        System.out.println("Base tell name: " + name);
    }

    public void printName() {
        System.out.println("Base print name: " + name);
    }
}

运行结果:

1、 this指针具有多态性,对象类型由对象实例本身决定,this本身的类型由的所在类的类型决定

2、成员属性不具有多态性,属性在构造之前值为0或者是null。

3、super函数调用所在类的父类函数

猜你喜欢

转载自blog.csdn.net/u014595589/article/details/82942826