day09-多态的概述和格式
This commit is contained in:
18
day10/src/com/inmind/duotai08/Demo08.java
Normal file
18
day10/src/com/inmind/duotai08/Demo08.java
Normal 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();//多态写法
|
||||
//多态是为了更好的封装,是动态的框架设计技术,扩展
|
||||
}
|
||||
}
|
||||
14
day10/src/com/inmind/duotai08/Fu.java
Normal file
14
day10/src/com/inmind/duotai08/Fu.java
Normal 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方法");
|
||||
}
|
||||
}
|
||||
15
day10/src/com/inmind/duotai08/Zi.java
Normal file
15
day10/src/com/inmind/duotai08/Zi.java
Normal 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方法");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user