English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
このチュートリアルでは、Javaでの継承を例を用いて学びます。
継承はOOP(面向对象编程)の重要な機能の一つであり、既存のクラスから新しいクラスを定義できるようにします。例えば、
class Animal { // eat() メソッド // sleep() メソッド } class Dog extends Animal { // bark() メソッド }
Javaでは、extendsキーワードを使用してクラスを継承します。ここでは、AnimalクラスからDogクラスを継承しています。
Animalは超クラス(親クラスまたは基底クラス)であり、Dogはサブクラス(サブクラスまたは派生クラス)です。サブクラスは超クラスのフィールドとメソッドを継承します。
継承はis-a関係がある場合に、継承を使用します。-関係がある場合に、継承を使用します。
以下にいくつかの例があります:
車は乗り物です。
オレンジは果物です。
外科医は医師です。
犬は動物です。
class Animal { public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } } class Dog extends Animal { public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); } }
输出结果
I can eat I can sleep I can bark
ここでは、私たちはAnimalクラスのサブクラスとしてDogを継承しました。DogクラスはAnimalクラスからeat()とsleep()メソッドを継承しました。
したがって、DogクラスのオブジェクトはDogクラスとAnimalクラスのメンバーにアクセスできます。
私たちは前のチュートリアルでprivateとpublicアクセス修飾子について学びました。
privateメンバーはクラス内でのみアクセスできます
publicメンバーはどこからでもアクセスできます
さらに、メソッドやフィールドをprotectedに設定することもできます。protectedメンバーはアクセスできます
クラス内部
そのサブクラス内
同じパッケージ内
これはアクセス修飾子をアクセスできる概要です。
修飾子 | クラス | パッケージ | サブクラス | グローバル |
---|---|---|---|---|
public | はい | はい | はい | はい |
private | はい | いいえ | いいえ | いいえ |
protected | はい | はい | はい | いいえ |
class Animal { protected String type; private String color; public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } public String getColor(){ return color; } public void setColor(String col){ color = col; } } class Dog extends Animal { public void displayInfo(String c){ System.out.println("私は " + type); System.out.println("私の色は " + c); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); dog1.type = "mammal"; dog1.setColor("black"); dog1.displayInfo(dog1.getColor()); } }
输出结果
I can eat I can sleep I can bark 私は哺乳類です 私の色は黒です
ここでは、Animalクラスのtypeフィールドが保護されています。私たちは既にMainクラスからこのフィールドにアクセスしました
dog1.type = "mammal";
これは実行可能です。なぜなら、AnimalとMainクラスが同じパッケージ(同じファイル)にあるからです。
From the above example, we know that the object of the subclass can also access the methods of its superclass.
What happens if the same method is defined in both the superclass and the subclass?
Okay, in this case, the method in the subclass will override the method in the superclass. For example,
class Animal { protected String type = "animal"; public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } } class Dog extends Animal { @Override public void eat() { System.out.println("I eat dog food"); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); } }
输出结果
I eat dog food I can sleep I can bark
In this case, eat() appears in both the superclass Animal and the subclass Dog. We created an object of the subclass Dog called dog1。
When we use dog1When an object calls eat(), it will call the method inside Dog instead of the same method in the superclass. This is called method overriding.
In the above program, we used the @Override annotation to tell the compiler that we are overriding a method. However, this is not mandatory. In the next tutorial, we will learn in detailMethod overriding。
If you need to call the eat() method from the Animal subclass, use the super keyword.
class Animal { public Animal() { System.out.println("I am an Animal"); } public void eat() { System.out.println("I can eat"); } } class Dog extends Animal { public Dog(){ super(); System.out.println("I am a dog"); } @Override public void eat() { super.eat(); System.out.println("I eat dog food"); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.bark(); } }
输出结果
I am an Animal I am a dog I can eat I eat dog food I can bark
ここでは、superキーワードを使用してsuper()を通じてコンストラクタを呼び出します。また、super.eat()を使用してAnimalの超クラスのeat()メソッドを呼び出します。
注意:コンストラクタやsuperメソッドを呼び出す際に使用される違いについて詳しくは、以下を参照してください。Java superキーワード。
継承には5つの種類があります。
単一継承 - クラスBはクラスAのみから継承します。
多級継承 - クラスBはクラスAから継承し、クラスCはクラスBから継承します。
層継承 - クラスAはクラスB、C、Dの親クラスとして機能します。
多重継承 -クラスCはインターフェースAから継承し、クラスBを拡張します。
混合継承 -2つまたはそれ以上継承の混合。
Javaではクラスの多重継承や混合継承をサポートしていません。しかし、Javaではインターフェースを通じて多重継承を実現できます。インターフェースについて後の章で学びます。
最も重要な用途はコードの再利用性です。親クラスに存在するコードは子クラスで再度書き直す必要はありません。
メソッドオーバーライドを通じて実行時の多態性を実現します。後の章で多態性に関する情報をさらに学びます。