サブクラスのインスタンスを作成する際に、親クラスのコンストラクタを呼び出すときJavaはこの点を学ぶの1-
Javaの長い戦争は、マルチラーニングを活用するために外出することはできません、以前にほとんど忘れ基本を発生した問題の多くをピックアップします。
新しい形態によるサブクラスのインスタンスが作成、それは、この時間は、親クラスでこのキーポイントは、それが誰であるか、親クラスのコンストラクタを呼び出すのだろうか?実際のコードを見てみましょう。
動物のスーパークラス
public class Animal {
private String name;
private Integer age;
public void print(){
System.out.println("super class");
}
public Animal(String name, Integer age) {
this.name = name;
this.age = age;
this.print();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
猫のサブクラス
public class Cat extends Animal{
private String color;
public void print(){
System.out.println("sub class");
}
public Cat(String name,Integer age,String color) {
super(name,age);
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
テストクラスMainTest
public class MainTest {
public static void main(String[] args) {
Cat cat = new Cat("aa",12,"red");
}
}
出力構造
sub class
この構造は、このキーワードの親クラスのコンストラクタの例から見ることができサブクラスにポイントを行います。
この場合、親クラスのコンストラクタの種類を決定します
public Animal(String name, Integer age) {
this.name = name;
this.age = age;
this.print();
if(this instanceof Cat){
System.out.println("just is a cat !");
}
}
再度実行します
sub class
just is a cat !
実際、サブクラスを指します。
より深い原則は、私が関係を見ているときに、このの利用と継承クラスローダの存在。