Java三大特性之继承

2022-04-06  本文已影响0人  Java编程日记

1. 继承

众所周知,我们Java语言是一种面向对象的编程语言,每当我们提到Java的特性,大家一定会在脑海里浮现出Java中的 继承、多态以及封装 。 我们在日常的开发中经常会用到这三种特性,本文首先给大家介绍这三大特性之一——继承。

1.1 继承的实现

继承是面向对象三大特征之一,可以使得子类具有父类的属性和方法,还可以在子类中重新定义,以及追加属性和方法。

<pre class="hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">class Dog extends Animal { }
</pre>

我们为了让大家更好的掌握其格式,现在给出一个简单的案例加以说明:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Fu {
public void show() {
System.out.println("show方法被调用");
}
}
public class Zi extends Fu {
public void method() {
System.out.println("method方法被调用");
}
}
public class Demo {
public static void main(String[] args) {
//创建对象,调用方法
Fu f = new Fu();
f.show();

    Zi z = new Zi();
    z.method();
    z.show();
}

}</pre>

该案例执行的结果如下:

[图片上传失败...(image-a4b144-1649227330310)]

1.2 继承的好处和弊端

2. 继承中的成员访问特点

2.1 继承中变量的访问特点

在子类方法中访问一个变量,采用的是 **就近原则 **。

  1. 子类局部范围找
  2. 子类成员范围找
  3. 父类成员范围找
  4. 如果都没有就报错(不考虑父亲的父亲…)

我们还是以上面的案例加以说明:

<pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">class Fu {
int num = 10;
}
class Zi {
int num = 20;
public void show(){
int num = 30;
System.out.println(num);
}
}
public class Demo1 {
public static void main(String[] args) {
Zi z = new Zi();
z.show(); // 输出show方法中的局部变量30
}
}</pre>

具体该案例执行的结果如下:

[图片上传失败...(image-8a55f0-1649227330310)]

2.2 super

2.3 继承中构造方法的访问特点

注意:子类中所有的构造方法默认都会访问父类中无参的构造方法

子类会继承父类中的数据,可能还会使用父类的数据。所以,子类初始化之前,一定要先完成父类数据的初始化,原因在于,每一个子类构造方法的第一条语句默认都是: super()

问题:如果父类中没有无参构造方法,只有带参构造方法,该怎么办呢?

  1. 过使用super关键字去显示的调用父类的带参构造方法

  2. 父类中自己提供一个无参构造方法

推荐方案:

自己给出无参构造方法

2.4 继承中成员方法的访问特点

通过子类对象访问一个方法

  1. 子类成员范围找

  2. 父类成员范围找

  3. 如果都没有就报错(不考虑父亲的父亲…)

2.5 super内存图

[图片上传失败...(image-4c0874-1649227330310)]

2.6 方法重写

2.7 方法重写的注意事项

  1. 私有方法不能被重写( 父类私有成员子类是不能继承的 )
  2. 子类方法访问权限不能更低( public > 默认 > 私有 )

我们仍是通过前面的案例加以说明这个问题:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Fu {
private void show() {
System.out.println("Fu中show()方法被调用");
}

void method() {
    System.out.println("Fu中method()方法被调用");
}

}

public class Zi extends Fu {

/* 编译【出错】,子类不能重写父类私有的方法*/
@Override
private void show() {
    System.out.println("Zi中show()方法被调用");
}

/* 编译【出错】,子类重写父类方法的时候,访问权限需要大于等于父类 */
@Override
private void method() {
    System.out.println("Zi中method()方法被调用");
}

/* 编译【通过】,子类重写父类方法的时候,访问权限需要大于等于父类 */
@Override
public void method() {
    System.out.println("Zi中method()方法被调用");
}

}</pre>

[图片上传失败...(image-e856d3-1649227330310)]

与我们给出的注释是一致的;因此在我们日常的编程中一定要注意。

2.8. Java中继承的注意事项

  1. Java中类只支持单继承,不支持多继承
  1. Java中类支持多层继承

为了让大家更好的理解该知识点,我们给出以下的代码:

<pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Granddad {

public void drink() {
    System.out.println("爷爷爱喝酒");
}

}

public class Father extends Granddad {

public void smoke() {
    System.out.println("爸爸爱抽烟");
}

}

public class Mother {

public void dance() {
    System.out.println("妈妈爱跳舞");
}

}
public class Son extends Father {
// 此时,Son类中就同时拥有drink方法以及smoke方法
}</pre>

3. 继承练习

①定义老师类(姓名,年龄,教书())

②定义学生类(姓名,年龄,学习())

③定义测试类,写代码测试

④共性抽取父类,定义人类(姓名,年龄)

⑤定义老师类,继承人类,并给出自己特有方法:教书()

⑥定义学生类,继承人类,并给出自己特有方法:学习()

⑦定义测试类,写代码测试

我们通过以上的需求以及思路实现如下的代码:

<pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">package inheritExample;
class Person {
private String name;
private int age;

public Person() {
}

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

class Teacher extends Person {

public Teacher() {}

public Teacher(String name,int age) {
    super(name,age);
}

public void teach() {
    System.out.println("用心去撰写每一篇博客");
}

}

class Student extends Person{
public Student() {}

public Student(String name, int age) {
    super(name,age);
}

public void study(){
    System.out.println("读者阅读");
}

}

class PersonDemo {
public static void main(String[] args){
//创建老师类对象并进行测试
Teacher t = new Teacher();
t.setName("一计之长");
t.setAge(28);
System.out.println(t.getName() + "," + t.getAge());
t.teach();

 // 创建学生类对象测试
    Student s1 = new Student("readA",18);
    System.out.println(s1.getName() + "," + s1.getAge());
    s1.study();

    Student s2 = new Student("readB",23);
    System.out.println(s2.getName() + "," + s2.getAge());
    s2.study();
}

}</pre>

具体执行的结果如下:

[图片上传失败...(image-a2aecd-1649227330310)]

总结

本文主要是详细给大家介绍了java中的三大特性中的继承,包括继承的概念、继承的实现以及优缺点,另外还介绍了继承中成员访问特点,接着给大家介绍了 super和this这两个关键字的相关知识, 最后给大家介绍了方法的重写以及在使用继承中需要注意的事项。与此同时,我们在介绍相关知识的同时给出了相应的案例帮助大家更好的理解相应的知识点。

上一篇 下一篇

猜你喜欢

热点阅读