package com.zhao.impl;
public inte***ce ridecar
package com.zhao.impl;
public inte***ce rideplane
package com.zhao.impl;
abstract public class person
public person()
public person(string name,int age)
}
package com.zhao.impl;
import com.zhao.impl.person;
import com.zhao.impl.ridecar;
public class father extends person implements ridecar
public int pfi = 3;
public int pfi2 = pmethod();
public int pfi3;
public father()
public father(string name,int age)
public void canridecar()
private static int spmethod()
private int pmethod()
}
package com.zhao.impl;
public class child extends father implements rideplane
public int pci = 4;
public int pci2 = pmethod();
public int pci3;
public child()
public child(string name,int age)
public void canrideplane()
private static int spmethod()
private int pmethod()
}
package com.zhao.test;
import com.zhao.impl.child;
import com.zhao.impl.father;
import com.zhao.impl.person;
import com.zhao.impl.ridecar;
public class testclass
}
program start
person's static block
father's static method
father's static block
child's static method
child's static block
child's spci:2
person's seft construction
father's instance method
father's instance block
father's seft construction
child's instance method
child's instance block
child's self construction
child can ride a plane
program end
second program start
person's default construction
father's instance method
father's instance block
father's default construction
child's instance method
child's instance block
child's default construction
second program end
package com.zhao.test;
import com.zhao.impl.child;
import com.zhao.impl.father;
import com.zhao.impl.person;
import com.zhao.impl.ridecar;
public class testclass2
}
f的class:class com.zhao.impl.child
4zhao,24
adult,18
adult,18
zhao,24
class com.zhao.impl.child
adult
adult
zhao
[size=x-large]
總結:一.
1.有沒有super()都會呼叫父類的預設構造方法
2.super();this();super(name,age);這些呼叫構造器的語句只能用乙個.原因:
constructor call must be the first statement in the constructor
用了super(name,age);就不會呼叫父類的預設構造方法而調父類兩引數的構造方法
3.用abstract 宣告為抽象類的話,好像static塊就不執行.
4.father d = new child(); 和child d = new child();輸出結果是一樣的.
二.1.賦值,子類變數可以賦值給父類變數,反之不行。
2.強制轉換,子類變數可以強制轉換成父類.
3.實際型別,用了什麼類的構造成器構造,實際型別就是什麼,如father d = new child(); ,d的實際上就是child型別.ridecar rc = new child(); ,rc實際上也是child型別.
[/size]
C 父子類之間的轉換
一 常識 隨意開啟一本c 書,基本都會寫著一句話 子物件可以轉化成父物件,父物件不能轉化成子物件。1.1 子物件可以轉化成父物件 很好理解,其實就是c 多型的體現,父指標可以指向子物件位址,當呼叫父物件裡生命的函式時,會預設呼叫子物件的此 重寫函式 巴拉巴拉巴拉.1.2 父物件不可轉化成子物件 存在...
Java面試之父子類(3) 構造方法順序
在檢視答案之前,先猜測一下正確答案吧!package com.company class parentclass public parentclass string name static public class subclass extends parentclass public subcla...
java父子類例項強制轉化後 各例項之間的關係
子類例項轉化為父類例項後 是否還從屬 instanceof 於子類?這個被轉化得來的父類例項是否能訪問子類重寫的方法 子類新新增的方法 子類新新增的成員屬性 是否還能轉化為子類例項?以下測試 建立乙個父類和乙個子類,父類擁有成員變數a 方法fun 子類擁有繼承的成員變數a 新定義的成員變數b 重寫的...