継承と方法書き換え
2382 ワード
要約:1.継承の文法形式
2.方法書き換え
3.自動変換
1.継承の構文形式:
1.1継承のキーワード:extens
書式:
public classクラス名(サブクラス、超クラス、派生クラス)extensクラス名(親類、基種){
)
1.2サブクラスが親クラスの属性と方法を継承する場合
a.サブクラスと親類は同じパッケージで、呼び出すことができる方法と属性:
プライベートの属性と方法だけがサブクラスとサブクラスのオブジェクトでは呼び出せません。
b.サブクラスと親タイプの異なるパッケージで、呼び出すことができる属性と方法:
サブクラス: 共有する、保護された属性と方法
サブクラスのオブジェクト:共有の属性と方法
そのサブクラス:
2.1条件:
1.継承関係が必要です。
2.戻り値データの種類、方法名、パラメータの個数、パラメータの種類、パラメータの順序は親と完全に一致していなければなりません。 。
3.サブクラスの書き換え方法のアクセス修飾子は、親の方法のアクセス修飾子よりも大きいかまたは等しいことができる。
2.2書き換えの方法はどうやって呼び出しますか?
サブクラスを優先的に呼び出す方法は、サブクラスがない場合は、親クラスを呼び出します。
3.自動変換:
3.1条件:自動転換を実現するには継承関係が必要です。
3.2フォーマット一:
親類名の対象名=new子類の構造方法();
Student stu=new UNStudent()
フォーマット2:
public戻り値データタイプ メソッド名(親クラス名のオブジェクト名){
オブジェクト名.親クラスで定義されたメソッド()を呼び出します。
)
親クラス名の対象名A=newサブ構造方法()
サブクラス名の対象名B=newサブクラスの構造方法();
メソッド名(オブジェクト名A)
メソッド名(オブジェクト名B)
2.方法書き換え
3.自動変換
1.継承の構文形式:
1.1継承のキーワード:extens
書式:
public classクラス名(サブクラス、超クラス、派生クラス)extensクラス名(親類、基種){
)
1.2サブクラスが親クラスの属性と方法を継承する場合
a.サブクラスと親類は同じパッケージで、呼び出すことができる方法と属性:
プライベートの属性と方法だけがサブクラスとサブクラスのオブジェクトでは呼び出せません。
b.サブクラスと親タイプの異なるパッケージで、呼び出すことができる属性と方法:
サブクラス: 共有する、保護された属性と方法
サブクラスのオブジェクト:共有の属性と方法
packge stu;
public class Student {
//
public String name;
private int score;
/**
*
*/
public Student(){
this(" ",5)
/**
*
*/
public Student(String name,int score){
//
this.name = name;
this.score = score;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setScore(int score){
this.score = score;
}
public int getScore(){
return score;
}
/**
*
*/
public void study(){
score++;
System.out.println(name+" , "+score);
}
}
そのサブクラス:
/**
* UNStudent , Student
*/
public class UNStudent extends Student {
public void test(){
System.out.println("UNStudent Test");
}
}
2.方法書き換え:(方法と重載の区別に注意)2.1条件:
1.継承関係が必要です。
2.戻り値データの種類、方法名、パラメータの個数、パラメータの種類、パラメータの順序は親と完全に一致していなければなりません。 。
3.サブクラスの書き換え方法のアクセス修飾子は、親の方法のアクセス修飾子よりも大きいかまたは等しいことができる。
2.2書き換えの方法はどうやって呼び出しますか?
サブクラスを優先的に呼び出す方法は、サブクラスがない場合は、親クラスを呼び出します。
3.自動変換:
3.1条件:自動転換を実現するには継承関係が必要です。
3.2フォーマット一:
親類名の対象名=new子類の構造方法();
Student stu=new UNStudent()
フォーマット2:
public戻り値データタイプ メソッド名(親クラス名のオブジェクト名){
オブジェクト名.親クラスで定義されたメソッド()を呼び出します。
)
親クラス名の対象名A=newサブ構造方法()
サブクラス名の対象名B=newサブクラスの構造方法();
メソッド名(オブジェクト名A)
メソッド名(オブジェクト名B)