day09-多态的概述和格式

This commit is contained in:
2026-01-23 16:13:48 +08:00
parent dbb890e4fd
commit 4627b26498
3 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package com.inmind.duotai08;
/*
多态的格式和使用
多态的前提:【重点】
继承或者实现【二选一】 类与类的继承 实现类与接口的实现
方法的重写【意义体现:不重写,无意义】
父类引用指向子类对象【格式体现】
*/
public class Demo08 {
public static void main(String[] args) {
Zi zi = new Zi();//不是多态,只是简单的面向对象
Fu fu = new Zi();//父类引用指向子类对象【格式体现】
fu.methodFu();
fu.method();//多态写法
//多态是为了更好的封装,是动态的框架设计技术,扩展
}
}

View File

@@ -0,0 +1,14 @@
package com.inmind.duotai08;
public class Fu {
int numFu = 10;
int num = 20;
public void methodFu(){
System.out.println("父类的methodFu方法");
}
public void method(){
System.out.println("父类的method方法");
}
}

View File

@@ -0,0 +1,15 @@
package com.inmind.duotai08;
public class Zi extends Fu{
int numZi = 30;
int num = 40;
public void methodZi(){
System.out.println("子类的methodZi方法");
}
@Override
public void method(){
System.out.println("子类的method方法");
}
}