当前位置: 首页 > news >正文

8继承多态

3为什么需要继承,继承的意义是什么
![[Pasted image 20251210212105.png]]
所以想说明什么
![[Pasted image 20251210212458.png]]

```
public class Dog {
public String name;
public int age;
public void eat() {
System.out.println(this.name+"正在吃饭");

}
public void bark(){
System.out.println(this.name+"正在狗叫");
}
}
```

```
public class Cat {
public String name;
public int age;
public void eat(){
System.out.println(this.name+"正在吃饭");
}
public void mimi(){
System.out.println(this.name+"正在猫叫");
}//对于相同的部分(共性)进行抽取
//继承的好处就是可以达到代码的复用
}
```


```
public class Test {
static void main(String[] args) {
Dog d = new Dog();
d.name="dahuang";
d.eat();
d.bark();
System.out.println("=============================");
Cat c = new Cat();
c.name="mimi";
c.eat();
c.mimi();
}
}
```

```
public class Dog(子类) extends Animal(父类)

//子类继承父类,有什么就继承什么
![[Pasted image 20251210213604.png]
```
![[Pasted image 20251210213615.png]]
注意
子类会将父类的成员变量或方法继承到子类
子类要添加点自己特有的成员,不然就没必要继承


**当父类,子类同名的方法,成员变量,都怎么去访问,怎么去区分**
![[Pasted image 20251210224029.png]]
当父类和子类有同名的成员变量的时候,优先访问子类的
要访问父类用super
![[Pasted image 20251210224218.png]]
`System.out.println(super.a);`
只能通过super访问
```
public void func() {
System.out.println(this.a);
System.out.println(this.b);
System.out.println(c);
System.out.println(super.a);

Base base = new Base();
System.out.println(base.b);//这就是从根本

}
```
![[Pasted image 20251210224511.png]]
可以


回顾
```
class Base{
public int a=1;
public int b=2;
}

class Derived extends Base{
int a=11;
int b=22;
public int c=3;
public void test(){
System.out.println(super.a);
System.out.println(this.b);
System.out.println(c);
}

}
public class Test {
public static void main(String[] args) {
Derived derived = new Derived();
derived.test();
}
}
```
当前类用this 或者不用,父类用super
k可以用来访问类的成员或者方法
、![[Pasted image 20260103171520.png]]
![[Pasted image 20260103171625.png]]
![[Pasted image 20260103172021.png]]
![[Pasted image 20260103172221.png]]

```
class Base{
public int a=1;
public int b=2;
public void testa(){
System.out.println("这是父类的方法");
}

}

class Derived extends Base{
int a=11;
int b=22;
public int c=3;

public void testa(){//这和父类的叫重写,就是一模一样
System.out.println("这是子类的方法");
}


public void testa(char a){//这和父类的叫重载,有参数不一样
System.out.println("这是子类的方法");
}

public void test(){
testa();
super.testa();
}

public void test1(){
System.out.println(super.a);
System.out.println(this.b);
System.out.println(c);


}

// public void testb(){
//
// }

}
```
super和this只能在非静态成员使用,因为static的东西不属于对象,归属类,使用不需要super,this这种


关于super()
![[Pasted image 20260103174011.png]]
![[Pasted image 20260103174130.png]]
![[Pasted image 20260103174231.png]]
所以先帮父类进行初始化,即super(),就是调用了父类的构造方法
super()必须在第一行
this()也必须在第一行
那么这两个是不能共存的
![[Pasted image 20260103212338.png]]
有即不生成,没有即当能用的
![[Pasted image 20260103214227.png]]
![[Pasted image 20260103214400.png]]
不管实例化多少遍,静态的只执行一次,也就是new对象后这一次执行的就没有static


protected
(不同包当中的子类)

![[Pasted image 20260103222439.png]]

![[Pasted image 20260103224007.png]]
![[Pasted image 20260103224044.png]]
继承方式
![[Pasted image 20260104103826.png]]
不支持多继承,即第三种箭头相反,多层继承最好不要超出三层
#final关键字
用来控制它不改变
专业名称叫密封
![[Pasted image 20260104104157.png]]
可以密封方法,密封类
![[Pasted image 20260104104233.png]]
#组合

![[Pasted image 20260104112209.png]]
![[Pasted image 20260104104313.png]]
学生和老师都是学校的一部分
学生是类,老师也是类,学校也是类(同一个包里)
```
|public class School {|
|||
|||
||public Student[] students = new Student[10];|
|||
||public Teacher[] teachers = new Teacher[10];|
|||
|||

| |
|---|
|public class Student {|
||}|


| |
|---|
|public class Teacher {|
||}|
```


```
// 轮胎类
class Tire{
// ... }


// 发动机类
class Engine{
// ... }


// 车载系统类
class VehicleSystem{
// ... }

class Car{
private Tire tire;
// 可以复用轮胎中的属性和方法
private Engine engine;
// 可以复用发动机中的属性和方法
private VehicleSystem vs;
// 可以复用车载系统中的属性和方法
// ... }



// 奔驰是汽车
class Benz extend Car{
// 将汽车中包含的:轮胎、发送机、车载系统全部继承下来
}
```

#多态
理解向上转型和方法的重写
![[Pasted image 20260104105313.png]]
向上转型就是
将子类向上转型到父类,可以
直接赋值
可以通过方法的传参,在形参处
可以通过返回值,接受返回值

![[Pasted image 20260104114551.png]]

父类不能调用子类的方法
![[Pasted image 20260104113901.png]]

但是当方法重写时,可以调用子类的

![[Pasted image 20260104114105.png]]
![[Pasted image 20260104110059.png]]
方法的重写
要求
![[Pasted image 20260104110133.png]]
父类与子类的方法一样,名字,参数列表,返回值,但是内容可以不一样,就是方法的重写
![[Pasted image 20260104110306.png]]
重载


向上转型
直接赋值
方法的传参(用传参演示)
方法的返回值
缺点
只能调用父类自己的方法
```
public class Animal {
public String name;
public int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(this.name + " is eating");
}
}

public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
public void bark() {
System.out.println(this.name + " 在汪汪叫");
}

@Override
public void eat() {
System.out.println(this.name + " 正在吃狗粮");
}
}

public class Test {
public static void func(Animal animal){
animal.eat();//向上转型
}

public static void main1(String[] args) {
Dog dog=new Dog("旺财",2);
func(dog);//这就是存在重写时,用的是“旺财正在吃狗粮”
}

static void main(String[] args) {
Animal animal=new Dog("wangcai",3);
animal.eat();//向上转型后可以用这个调用子类的方法“wangcai正在吃狗粮”
}
}
```
![[Pasted image 20260106161146.png]]
程序在编译的时候,调用的是父类的方法
当运行代码的时候,通过父类的引用,调用了父类和子类重写的那个方法,结果实际调用的是子类的方法,这种情况叫“动态绑定”
(大前提:一定是在继承的情况下啊)

注解
注解起到提示作用@Override,注解有很多种

![[Pasted image 20260106162553.png]]
animal引用的对象不一样,但是调用的是同一个方法,此时表象出来的现象不一样,这种思想就叫做多态


重写的注意事项
1. 不能是一个静态方法
2. 不能被final修饰
3. 子类权限要大于等于父类
4. 被private修饰的方法,是不能被重写
5. 构成父子类关系也是重写
![[Pasted image 20260106162944.png]]
```
public class Animal {
public String name;
public int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public Animal eat() {
System.out.println(this.name + " is eating");
return null;
}
}

public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
public void bark() {
System.out.println(this.name + " 在汪汪叫");
}

public Dog eat(){
System.out.println(this.name+"正在吃狗粮");
return null;
}
}

```
所以这也是重写
向下转型
,这很危险
向上转型是子类到父类,子类一定属于父类,但父类不一定属于子类,所以需要强转,但强转不一定成功
![[Pasted image 20260106182749.png]]

![[Pasted image 20260106183022.png]]

![[Pasted image 20260106183117.png]]


```
public class Animal {
public String name;
public int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(this.name + " is eating");
// return null;
}
}


public class Bird extends Animal {
public Bird(String name, int age) {
super(name, age);
}
public void eat(){
System.out.println(this.name+"正在吃鸟粮");
}

public void fly() {
System.out.println(this.name + " is flying");
}
}


public class Dog extends Animal {
public Dog(String name, int age) {
super(name, age);
}
public void bark() {
System.out.println(this.name + " 在汪汪叫");
}

public void eat(){
System.out.println(this.name+"正在吃狗粮");
//return null;
}
}

public class Test {

static void main(String[] args) {
Animal animal=new Bird("菠萝包",1);
Bird bird=(Bird)animal;
bird.fly();//向下转型


Animal animal1=new Dog("小黑",2);
Dog dog=(Dog)animal1;
Bird bird1=(Bird)animal1;
bird1.fly();

}



其实写成分支语句
Animal animal2=new Dog("xiaohuang ",3);
if(animal2 instanceof Bird){
Bird bird2=(Bird)animal2;
bird2.fly();
}else {
Dog dog2=(Dog)animal2;
}

```
![[Pasted image 20260106163236.png]]


理解:当在父类的构造方法中调用子类和父类的重写的方法,会发生动态绑定
![[Pasted image 20260106163702.png]]
![[Pasted image 20260106163910.png]]
![[Pasted image 20260106164002.png]]


![[Pasted image 20260106190300.png]]
![[Pasted image 20260106190810.png]]
```
package demo2;

/**
* Created with IntelliJ IDEA. * Description: * User: suki * Deate: 2026-01-06 * Time:18:57 */

public class Test {

static void main(String[] args) {
Rect rect = new Rect();
Cycle cycle = new Cycle();
Flower flower = new Flower();

Shape[] shapes = {rect,cycle,flower};
for (Shape shape : shapes) {
shape.draw();
}
}



public static void main2(String[] args) {
Rect rect = new Rect();
Cycle cycle = new Cycle();


String[] strings = {"cycle","rect","cycle","rect","triangle"};

for(String s :strings) {
if(s.equals("cycle")) {
cycle.draw();
}else if(s.equals("rect")) {
rect.draw();
}
}
}


public static void drawMap(Shape shape){//向上转型
shape.draw();
}
static void main1(String[] args) {
Rect rect=new Rect();
Cycle cycle=new Cycle();
Flower flower=new Flower();
drawMap(rect);
drawMap(cycle);
drawMap(flower);
}
}
```

http://www.cnnetsun.cn/news/616806.html

相关文章:

  • Oracle 19c入门学习教程,从入门到精通,Oracle体系结构 —— 知识点详解(2)
  • 大家一直催更的Agent学习路线来喽!
  • 如何使用`typeid`判断指针或引用所指对象的实际类型?
  • 【Python库和代码案例:第一课】Python 标准库与第三方库实战指南:从日期处理到 Excel 操作
  • AI原生应用云端推理的故障排查与恢复
  • 大数据领域数据产品的安全保障策略
  • 数独优化求解C库tdoku-lib的使用
  • VibeThinker-1.5B值得部署吗?数学与编程双项评测教程
  • 隐数守护者-第2集:无声的目击者
  • AnimeGANv2如何实现私有化部署?内网隔离配置指南
  • 从批处理到流处理:Kappa架构的转型之路
  • 【毕业设计】基于python-CNN深度学习训练识别夏冬季节风景
  • 深度学习毕设选题推荐:基于python-CNN机器学习训练识别夏冬季节风景
  • AnimeGANv2从零开始:环境部署到风格转换全流程
  • VibeVoice-TTS开源优势解析:自主部署与数据安全实战落地
  • AnimeGANv2教程:将静物照片转换成动漫风格的技巧
  • AnimeGANv2部署指南:超轻量级动漫AI模型使用手册
  • 深度学习计算机毕设之基于python_CNN机器学习卷积神经网络识别花卉是否枯萎
  • 开源模型AnimeGANv2实战:WebUI界面一键转动漫风格
  • 通信原理篇---部分响应
  • AnimeGANv2性能测试:不同分辨率图片处理速度
  • 计算机深度学习毕设实战-基于卷积神经网络识别花卉是否枯萎
  • 深度学习毕设选题推荐:基于python_CNN深度学习机器学习卷积神经网络识别花卉是否枯萎
  • Multisim下载(Windows版)操作指南:从获取到运行一文说清
  • 小白也能懂:AI智能文档扫描仪快速入门手册
  • Node.js Array.from轻松转换流数据
  • 深度学习计算机毕设之基于python_CNN训练蔬菜识别基于python深度学习 卷积神经网络训练蔬菜识别
  • 小白必看!用AI智能文档扫描仪3步搞定证件扫描
  • VibeVoice-TTS部署教程:微软开源大模型网页推理实战指南
  • 小白也能用!通义千问2.5-7B-Instruct在Ollama上的快速体验