提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
继承是OOP(面向对象编程)的重要支柱。这是Java中允许一个类继承另一类的功能(属性和方法)的机制。
提示:以下是本篇文章正文内容,下面案例可供参考
一、重要术语
父类(super class):继承其功能的类称为父类(国外也称作super class/parent class/base class)。
子类(child class):继承另一个类的类称为子类(国外也称作 derived class/extended class/sub class)。除了父类的属性和方法之外,子类还可以添加自己的属性和方法。
可重用性(Reusability):继承支持“可重用性”的概念,即,当我们要创建一个新类并且已经有一个包含所需代码的类时,我们可以从现有类中派生新类。通过这样做,我们可以重用现有类的属性和方法。
二、如何使用继承
1.继承的使用方法
用于继承的关键字是:extends。
语法格式如下:
class derived-class extends base-class
{
//属性和方法
}
接下来用了一个例子来解释一下如何正确地在JAVA里面进行继承操作。
在该例中,Bicycle是父类,MountainBike类是继承了Bicycle的子类,Test类是要运行程序的驱动程序类。
//Java program to illustrate the
// concept of inheritance
// base class
class Bicycle
{
// the Bicycle class has two fields
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int gear, int speed)
{
this.gear = gear;
this.speed = speed;
}
// the Bicycle class has three methods
public void applyBrake(int decrement)
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
}
// toString() method to print info of Bicycle
public String toString()
{
return("齿轮数是 "+gear
+"\n"
+ "自行车的速度是 "+speed);
}
}
// derived class
class MountainBike extends Bicycle
{
// the MountainBike subclass adds one more field
public int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int gear,int speed,
int startHeight)
{
// invoking base-class(Bicycle) constructor
super(gear, speed);
seatHeight = startHeight;
}
// the MountainBike subclass adds one more method
public void setHeight(int newValue)
{
seatHeight = newValue;
}
// overriding toString() method
// of Bicycle to print more info
@Override
public String toString()
{
return (super.toString()+
"\n座椅高度是 "+seatHeight);
}
}
// driver class
public class Test
{
public static void main(String args[])
{
MountainBike mb = new MountainBike(3, 100, 25);
System.out.println(mb.toString());
}
}
输出结果:
齿轮数是3
自行车的速度是100
座椅高度是25
在上面的程序中,创建MountainBike类的对象时,父类的所有方法和属性的副本将在该对象中获取内存。这就是为什么通过使用子类的对象,我们还可以访问父类的方法和属性。
请注意,在继承期间,仅创建子类的对象,而不创建父类的对象。
程序的说明图:
2.继承的类型
以下是JAVA支持的不同类型的继承:
1.单继承:在单一继承中,子类继承一个父类的功能。在下图中,类A用作子类B的父类。
//Java program to illustrate the
// concept of single inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
// Driver class
public class Main
{
public static void main(String[] args)
{
two g = new two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
2.多重继承: 在多重继承中,子类将继承父类,并且子类还充当其他类的父类。在下图中,类A充当子类B的父类,后者又充当子类C的父类。在Java中,类无法直接访问父类的父类。
// Java program to illustrate the
// concept of Multilevel inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
class three extends two
{
public void print_geek()
{
System.out.println("Geeks");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
g.print_for();
g.print_geek();
}
}
3.层次继承:在层次继承中,一个类充当多个子类的父类(基类)。在下图中,类A充当父类B,C和D的基类。
// Java program to illustrate the
// concept of Hierarchical inheritance
import java.util.*;
import java.lang.*;
import java.io.*;
class one
{
public void print_geek()
{
System.out.println("Geeks");
}
}
class two extends one
{
public void print_for()
{
System.out.println("for");
}
}
class three extends one
{
/*............*/
}
// Drived class
public class Main
{
public static void main(String[] args)
{
three g = new three();
g.print_geek();
two t = new two();
t.print_for();
g.print_geek();
}
}
4.多重继承(通过接口):在多重继承中,一个类可以具有多个父类,并且可以从所有父类继承功能。请注意,Java并没有支持多重继承与类。在Java中,我们只能通过Interfaces实现多重继承。在下图中,类C从接口A和B派生。
// Java program to illustrate the
// concept of Multiple inheritance
import java.util.;
import java.lang.;
import java.io.*;
interface one
{
public void print_geek();
}
interface two
{
public void print_for();
}
interface three extends one,two
{
public void print_geek();
}
class child implements three
{
@Override
public void print_geek() {
System.out.println(“Geeks”);
}
public void print_for()
{
System.out.println("for");
}
}
// Drived class
public class Main
{
public static void main(String[] args)
{
child c = new child();
c.print_geek();
c.print_for();
c.print_geek();
}
}
5.混合继承(通过接口):它是两种或多种上述继承类型的混合。由于Java不支持类的多重继承,因此混合继承也不能用于类。在Java中,我们只能通过Interfaces实现混合继承。
三、总结
有关JAVA继承的重要事实
- 默认父类:Object类除外,它没有父类,每个类只有一个,并且只有一个直接父类(单继承)。在没有其他任何显式父类的情况下,每个类都隐式为Object类的子类。
- 父类只能是一个:一个父类可以具有任意数量的子类。但是一个子类只能有一个父类。这是因为Java不支持类的多重继承。尽管通过接口可以实现,但Java不支持多重继承。
- 继承构造函数:子类从其父类继承所有成员(字段,方法和嵌套类)。构造函数不是成员,因此它们不会被子类继承,但是可以从子类中调用超类的构造函数。
- Private member继承:子类不继承其父类的Private member。但是,如果超类具有用于访问其私有字段的公共或受保护的方法(如getter和setter),则这些也可以由子类使用。
在子类中可以干什么?
在子类中,我们可以按原样继承父类,替换它们,隐藏它们或用新成员补充它们:
- 继承的字段可以像其他任何字段一样直接使用。
- 我们可以在子类中声明不在超类中的新字段。
- 继承的方法可以直接使用。
- 我们可以在子类中编写一个新的实例方法,该方法具有与父类中的签名相同的签名,从而将其覆盖(如上面的示例中,toString()方法被覆盖)。
- 我们可以在子类中编写一个新的方法。
- 我们可以在子类中声明不在父类中的新方法。
- 我们可以编写一个通过使用关键字super调用父类的构造函数的子类构造函数。