day10--多态的概念与格式

This commit is contained in:
xx
2026-10-08 15:33:11 +08:00
parent 4416664f45
commit 43cf613fd1
4 changed files with 46 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
package com.inmind.poly08;
public class Animal {
public void run(){
System.out.println("动物跑");
}
}
+8
View File
@@ -0,0 +1,8 @@
package com.inmind.poly08;
public class Cat extends Animal{
@Override
public void run() {
System.out.println("猫闪着跑,闪身步");
}
}
+22
View File
@@ -0,0 +1,22 @@
package com.inmind.poly08;
/*
多态的格式和使用
多态的前提:【重点】
继承或者实现【二选一】
方法的重写【意义体现:不重写,无意义】
父类引用指向子类对象【格式体现】
*/
public class Demo08 {
public static void main(String[] args) {
//普通的面向对象的写法
Dog dog = new Dog();
Cat cat = new Cat();
//多态写法:父类引用指向子类对象
Animal animal1 = new Dog();
Animal animal2 = new Cat();
animal1.run();
animal2.run();
}
}
+8
View File
@@ -0,0 +1,8 @@
package com.inmind.poly08;
public class Dog extends Animal{
@Override
public void run() {
System.out.println("狗哆嗦地跑");
}
}