Java - 继承
继承可以定义为一个类获取另一个类的属性(方法和字段)的过程。通过使用继承,信息可以按层次顺序进行管理。
继承其他属性的类称为子类(派生类、子类),继承属性的类称为超类(基类、父类)。
扩展关键字
扩展 是用于继承类属性的关键字。下面是extends关键字的语法。
语法
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
示例代码
以下是演示 Java 继承的示例。在此示例中,您可以观察到两个类,即 Calculation 和 My_Calculation。
My_Calculation 使用extends 关键字继承Calculation 类的addition() 和Subtraction() 方法。
将以下程序复制并粘贴到名为 My_Calculation.java 的文件中
示例
现场演示
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
在给定的程序中,当一个对象 My_Calculation 在创建类时,会在其中制作超类内容的副本。这就是为什么使用子类的对象可以访问超类的成员。
超类引用变量可以保存子类对象,但使用该变量只能访问超类的成员,因此要访问两个类的成员,建议始终为子类创建引用变量。
如果你考虑上面的程序,你可以实例化下面给出的类。但是使用超类引用变量( cal 在这种情况下)你不能调用方法 multiplication() ,属于 My_Calculation 的子类。
Calculation demo = new My_Calculation(); demo.addition(a, b); demo.Subtraction(a, b);
注意 − 子类从其超类继承所有成员(字段、方法和嵌套类)。构造函数不是成员,所以不被子类继承,但是子类可以调用超类的构造函数。
超级关键字
超级 关键字类似于 this 关键词。以下是使用 super 关键字的场景。
-
它用于区分成员 如果子类的成员具有相同的名称,则从子类的成员中获取父类的名称。
-
它用于调用超类 子类的构造函数。
区分成员
如果一个类继承了另一个类的属性。而如果超类的成员与子类的名字相同,为了区分这些变量,我们使用 super 关键字,如下所示。
super.variable super.method();
示例代码
本节为您提供一个演示 super 用法的程序 关键字。
在给定的程序中,您有两个类,即 Sub_class 和 Super_class , 两者都有一个名为 display() 的方法具有不同的实现,以及一个名为 num 的变量具有不同的值。我们正在调用两个类的 display() 方法并打印两个类的变量 num 的值。在这里您可以观察到我们使用 super 关键字来区分超类和子类的成员。
将程序复制并粘贴到名为 Sub_class.java 的文件中。
示例
现场演示
class Super_class {
int num = 20;
// display method of superclass
public void display() {
System.out.println("This is the display method of superclass");
}
}
public class Sub_class extends Super_class {
int num = 10;
// display method of sub class
public void display() {
System.out.println("This is the display method of subclass");
}
public void my_method() {
// Instantiating subclass
Sub_class sub = new Sub_class();
// Invoking the display() method of sub class
sub.display();
// Invoking the display() method of superclass
super.display();
// printing the value of variable num of subclass
System.out.println("value of the variable named num in sub class:"+ sub.num);
// printing the value of variable num of superclass
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);
示例代码
本节给出的程序演示了如何使用 super 关键字来调用超类的参数化构造函数。该程序包含一个超类和一个子类,其中超类包含一个接受整数值的参数化构造函数,我们使用 super 关键字来调用超类的参数化构造函数。
将以下程序复制并粘贴到名为 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 args[]) {
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 关系
IS-A 是一种说法:这个对象是那个对象的一个类型。让我们看看如何扩展 关键字用于实现继承。
public class Animal {
}
public class Mammal extends Animal {
}
public class Reptile extends Animal {
}
public class Dog extends Mammal {
}
现在,基于上面的例子,在面向对象的术语中,以下是正确的 -
- Animal 是 Mammal 类的超类。
- Animal 是 Reptile 类的超类。
- 哺乳动物和爬行动物是动物类的子类。
- Dog 是 Mammal 和 Animal 类的子类。
现在,如果我们考虑 IS-A 关系,我们可以说 -
- 哺乳动物 IS-A 动物
- 爬行动物是一种动物
- 狗是哺乳动物
- 因此:狗也是动物
通过使用extends关键字,子类将能够继承父类的所有属性,但超类的私有属性除外。
使用实例操作符我们可以确定 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 有很好的理解 关键字,让我们看看实现 关键字用于获取IS-A关系。
通常,实现 关键字与类一起使用以继承接口的属性。接口永远不能被类扩展。
示例
public interface Animal {
}
public class Mammal implements Animal {
}
public class Dog extends Mammal {
}
instanceof 关键字
让我们使用 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
HAS-A 关系
这些关系主要基于使用情况。这决定了某个类是否HAS-A 某件事。这种关系有助于减少代码的重复和错误。
让我们看一个例子 -
示例
public class Vehicle{}
public class Speed{}
public class Van extends Vehicle {
private Speed sp;
}
这表明范有-A级速度。通过单独的 Speed 类,我们不必将属于 speed 的整个代码放在 Van 类中,这样就可以在多个应用程序中重用 Speed 类。
在面向对象的特性中,用户不需要关心哪个对象在做真正的工作。为了实现这一点,Van 类对 Van 类的用户隐藏了实现细节。所以,基本上发生的情况是用户会要求 Van 类执行某个动作,而 Van 类要么自己完成工作,要么要求另一个类执行该动作。
继承类型
有多种类型的继承,如下所示。
要记住的一个非常重要的事实是 Java 不支持多重继承。这意味着一个类不能扩展多个类。因此以下是非法的 -
示例
public class extends Animal, Mammal{}
但是,一个类可以实现一个或多个接口,这帮助Java摆脱了多重继承的不可能性。
java