day10--多态的概念与格式
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package com.inmind.poly08;
|
||||
|
||||
public class Animal {
|
||||
|
||||
public void run(){
|
||||
System.out.println("动物跑");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.inmind.poly08;
|
||||
|
||||
public class Cat extends Animal{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("猫闪着跑,闪身步");
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.inmind.poly08;
|
||||
|
||||
public class Dog extends Animal{
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("狗哆嗦地跑");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user