JAVA學習(10) 繼承(3) 繼承中的構(gòu)造函數(shù)
父類:
public class Obj {
public Obj()
{
System.out.println("Obj構(gòu)造");
}
public Obj(String s)
{
System.out.println("Obj構(gòu)造"+s);
}
}
子類會調(diào)用父類的無參數(shù)構(gòu)造函數(shù)
public class Test11A extends Obj {
public static void main(String[] args) {
// TODO 自動生成的方法存根
Test11A test11a = new Test11A();
System.out.println("hello");
}
}
子類會調(diào)用父類的無參數(shù)構(gòu)造函數(shù) 然后再調(diào)用自身的構(gòu)造函數(shù)
public class Test11A extends Obj {
public Test11A()
{
System.out.println("Test11構(gòu)造");
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
Test11A test11a = new Test11A();
System.out.println("hello");
}
}
子類會調(diào)用父類的無參數(shù)構(gòu)造函數(shù) 然后再調(diào)用自身的有參數(shù)構(gòu)造函數(shù)
public class Test11A extends Obj {
public Test11A()
{
System.out.println("Test11構(gòu)造");
}
public Test11A(String s)
{
System.out.println("Test11構(gòu)造"+s);
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
Test11A test11a = new Test11A("hello");
System.out.println("hello");
}
}
super關(guān)鍵字返回父類的引用,可以使用引用強制設(shè)置調(diào)用父類的有參數(shù)構(gòu)造函數(shù)
public class Test11A extends Obj {
public Test11A()
{
System.out.println("Test11構(gòu)造");
}
public Test11A(String s)
{
super(s);
System.out.println("Test11構(gòu)造"+s);
}
public static void main(String[] args) {
// TODO 自動生成的方法存根
Test11A test11a = new Test11A("hello");
System.out.println("hello");
}
}