2. 里氏代换原则
1.1 任何父类能够出现的地方,子类一定能够出现1.2 实例(人养宠物)
package com.geek99.demo;
public class Test {
public static void main(String[] args) {
Pet cat = new Cat();
Pet dog = new Dog();
Person per = new Person();
per.care(cat);
per.care(dog);
}
}
interface Pet{
public void run();
class Person {
// 注意,这里就是李氏代换,其实就是向上类型转换
public void care(Pet p){
p.run();
class Cat implements Pet{
@Override
public void run() {
System.out.println("cat run...");
class Dog implements Pet{
System.out.println("dog run...");
}
该博客教程视频地址:http://geek99.com/node/1628
原文链接:https://www.f2er.com/javaschema/285577.html