天天看點

this指針多态性

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

運作結果:

this指針多态性

1、 this指針具有多态性,對象類型由對象執行個體本身決定,this本身的類型由的所在類的類型決定

2、成員屬性不具有多态性,屬性在構造之前值為0或者是null。

3、super函數調用所在類的父類函數