继承可以定义为一个类获取另一个类的属性(方法和字段)的过程。
extends是用于继承类属性的关键字。以下是extends关键字的语法。
class Super { ..... ..... } class Sub extends Super { ..... ..... }
下面是一个演示Java继承的示例。在本示例中,您可以观察到两个类,即Calculation和my_Calculation。
使用extends关键字,My_Calculation继承Calculation类的Add()和Subtract()方法。
将以下程序复制并粘贴到名为my_Calculation.java的文件中
链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-inheritance.html
来源:LearnFk无涯教程网
class Calculation { int z; public void addition(int x, int y) { z = x + y; System.out.println("The sum of the given numbers:"+z); } public void Subtraction(int x, int y) { z = x - y; System.out.println("The difference between the given numbers:"+z); } } public class My_Calculation extends Calculation { public void multiplication(int x, int y) { z = x * y; System.out.println("The product of the given numbers:"+z); } public static void main(String args[]) { int a = 20, b = 10; My_Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b); demo.multiplication(a, b); } }
编译并执行上述代码,如下所示。
javac My_Calculation.java java My_Calculation
执行程序后,将生成以下输出-
The sum of the given numbers:30 The difference between the given numbers:10 The product of the given numbers:200
类的成员。
Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b);
super关键字类似于this关键字。以下是使用super关键字的场景。
它用于区分父类的成员和子类的成员。
它用于从子类调用父类构造函数。
如果一个类正在继承另一个类的属性。如果父类的成员与子类的名称相同,为了区分这些变量,无涯教程使用super关键字,如下所示。
super.variable super.method();
本节提供了一个演示super关键字用法的程序。
在给定的程序中,您有两个类,即Sub_class和Super_class,它们都有一个名为display()的方法,具有不同的实现,以及一个名为num的变量,具有不同的值。无涯教程调用这两个类的display()方法,并打印这两个类的变量num的值。
将程序复制并粘贴到名为Sub_class.java的文件中。
class Super_class { int num = 20; //父类的显示方法 public void display() { System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; //子类的显示方法 public void display() { System.out.println("This is the display method of subclass"); } public void my_method() { //实例化子类 Sub_class sub = new Sub_class(); //调用子类的 display() 方法 sub.display(); //调用父类的 display() 方法 super.display(); //打印子类的变量 num 的值 System.out.println("value of the variable named num in sub class:"+ sub.num); //打印父类的变量 num 的值 System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]) { Sub_class obj = new Sub_class(); obj.my_method(); } }
使用以下语法编译并执行上述代码。
javac Super_Demo java Super
在执行程序时,您将得到以下输出-
This is the display method of subclass This is the display method of superclass value of the variable named num in sub class:10 value of the variable named num in super class:20
的参数化构造函数,则需要使用super关键字
super(values);
将以下程序复制并粘贴到名为Subclass.java的文件中
class Superclass { int age; Superclass(int age) { this.age = age; } public void getAge() { System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age) { super(age); } public static void main(String argd[]) { Subclass s = new Subclass(24); s.getAge(); } }
使用以下语法编译并执行上述代码。
javac Subclass java Subclass
在执行程序时,您将得到以下输出-
The value of the variable named age in super class is: 24
IS-A是否是继承关系,让无涯教程看看扩展关键字是如何用来实现继承的。
public class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }
通过使用操作符,可以确保Mammal实际上是Animal。
class Animal { } class Mammal extends Animal { } class Reptile extends Animal { } public class Dog extends Mammal { public static void main(String args[]) { Animal a = new Animal(); Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } }
这将产生以下输出-
true true true
由于无涯教程已经很好地理解了extends关键字,来看看Implements关键字是如何用来获取IS-A关系的。
通常,implements关键字与类一起使用以继承接口的属性。
public interface Animal { } public class Mammal implements Animal { } public class Dog extends Mammal { }
使用instanceof运算符检查确定Mammal是否实际上是Animal,而dog实际上是Animal。
interface Animal{} class Mammal implements Animal{} public class Dog extends Mammal { public static void main(String args[]) { Mammal m = new Mammal(); Dog d = new Dog(); System.out.println(m instanceof Animal); System.out.println(d instanceof Mammal); System.out.println(d instanceof Animal); } }
这将产生以下输出-
true true true
有各种类型的继承,如下所示。
需要记住的一个非常重要的事实是Java不支持多重继承。这意味着一个类不能扩展多个类。因此以下是非法的-
public class extends Animal, Mammal{}
然而,一个类可以实现一个或多个接口,这帮助Java摆脱了多重继承的不可能。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)