【前言】
在Java面向对象编程中,
继承
是一个非常重要的概念。它允许我们创建一个新类,这个新类可以继承现有类(称为父类或超类)的属性和方法,同时还可以添加自己特有的属性和方法。通过继承,我们可以实现代码的复用,减少重复代码,提高开发效率。本文将详细介绍Java继承的相关知识,并通过代码示例和图表进行深入讲解。
举个例子吧,相信大家都看过西虹市首富吧,沈腾演的王多鱼通过他二爷的考验,最终继承
了他二爷的财产。
继承是面向对象程序设计使代码重复利用
的重要手段。
看下面两段代码,猫和狗都有名字,年龄和行为。但我们这样写就存在重复,这时候继承就体现出作用了。
public class Cat {
String name;
int age;
public void eat(){
System.out.println("吃饭");
}
}
public class Dog {
String name;
int age;
public void eat(){
System.out.println("吃饭");
}
}
我们可以继承原有类的属性,还可以进行扩展。原有类就是父类,而我们新定义的就是子类。
Dog和Cat都继承了Animal类,其中:Animal类称为⽗类/基类或超类,Dog和Cat可以称为Animal的⼦类/派⽣类,继承之后,⼦类可以复⽤⽗类中成员,⼦类在实现时只需关⼼⾃⼰新增加的成员即可。
在Java中如果要表⽰类之间的继承关系,需要借助extends
关键字,具体如下:
修饰符 class ⼦类 extends ⽗类 {
// ...
}
还是用上面哪个例子:
public class Animal {
String name;
int age;
public void eat(){
System.out.println("吃饭");
}
}
public class Cat extends Animal {
void show(){
System.out.println("喵喵喵");
}
}
public class Dog extends Animal {
void show(){
System.out.println("汪汪汪");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
System.out.println(dog.name);
System.out.println(dog.age);
dog.eat();
dog.show();
}
}
执行结果:
【注意】
public class Base {
public int a;
public int b;
}
public class Derived extends Base{
public int c;
public void method(){
this.a = 10;
this.b = 20;
this.c = 30;
}
}
this
关键字来访问public class Test {
public static void main(String[] args) {
Derived derived = new Derived();
derived.method();
}
}
public class Derived extends Base{
public int c;
public int a=100;
public void method(){
System.out.println(this.a);
System.out.println(this.b);
System.out.println(this.c);
}
}
public class Base {
public int a=10;
public int b;
}
同名时,访问子类自己的,父类和子类都有一个a,父类是10,子类是100,他打印出来就是100
这里与成员变量同理,就不多做解释了
当父类与子类的成员名称相同时,我们又需要在子类中访问父类的成员,这时候就要用到super
关键字
public class Base {
public int a=10;
}
public class Derived extends Base{
public int a=100;
public void method(){
System.out.println(this.a);
System.out.println(super.a);
}
}
执行结果:
【总结】
static
我们要构造子类对象时,需要先调用父类的构造方法,然后执行子类的构造方法,所以,需要先对父类进行构造,如下代码:
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
构造完成后,他还会报错,这时我们需要初始化自己的
public Dog(){
super("旺财",10);
}
如果父类构造方法没有参数,子类会自动生成不带参数的构造方法,就不会报错,但如果带参数,我们就需要自己构造,初始化 【注意事项】
【相同点】
【不同点】
这里还是举上面那个例子 父类Animal
public class Animal {
public String name;
public int age;
static {
System.out.println("Animal的静态代码块 static{}");
}
{
System.out.println("Animal的实例代码块{}");
}
public Animal(String name, int age) {
this.name = name;
this.age = age;
System.out.println("执行Animal的2个参数的构造方法");
}
子类Dog
public class Dog extends Animal{
static {
System.out.println("Dog的静态代码块 static{}");
}
{
System.out.println("Dog的实例代码块{}");
}
public Dog(String name,int age){
super("旺财",10);
System.out.println("Dog类带两个参数的构造方法执行了");
}
子类Cat
public class Cat extends Animal{
static {
System.out.println("Cat的静态代码块 static{}");
}
{
System.out.println("Cat的实例代码块{}");
}
public Cat(){
super("咪咪",10);
System.out.println("Cat类带两个参数的构造方法执行了");
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog("旺财",10);
}
}
执行结果:
【总结】
在类和对象章节中,为了实现封装特性,Java中引⼊了访问限定符,主要限定:类或者类中成员能否在类外或者其他包中被访问。
final关键字可以⽤来修饰变量、成员⽅法以及类。 • 修饰变量或字段,表⽰常量(即不能修改)
final int a = 10;
a = 20;//报错
• 修饰类:表⽰此类不能被继承
final public class Animal {
...
}
public class Bird extends Animal {
...
}
• 修饰⽅法:表⽰该⽅法不能被重写
和继承类似, 组合也是⼀种表达类之间关系的⽅式, 也是能够达到代码重⽤的效果。组合并没有涉及到特殊的语法(诸如 extends 这样的关键字), 仅仅是将⼀个类的实例作为另外⼀个类的字段。
// 轮胎类
class Tire{
// ...
}
// 发动机类
class Engine{
// ...
}
// ⻋载系统类
class VehicleSystem{
// ...
}
class Car{
private Tire tire;
// 可以复⽤轮胎中的属性和⽅法
private Engine engine;
// 可以复⽤发动机中的属性和⽅法
private VehicleSystem vs;
// 可以复⽤⻋载系统中的属性和⽅法
// ...
}
// 奔驰是汽⻋
class Benz extend Car{
// 将汽⻋中包含的:轮胎、发送机、⻋载系统全部继承下来
}
组合和继承都可以实现代码复⽤,应该使⽤继承还是组合,需要根据应⽤场景来选择,⼀般建议:能 ⽤组合尽量⽤组合。 【总结】
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有