39.p440集韩老师讲的第10章的homework06题,在
package com.hspedu.homework06Try02;
/**
* @author code
* @version 1.0
*/
public class Homework06 {
public static void main(String[] args){
Person person= new Person("唐僧", new Boat());
person.common();
person.passRiver();
person.passRiver();
person.passRiver();
person.common();
}
}
这里的Person里面的参数 new Boat()改为null或者new Horse()也可以不报错并运行出结果来,这是为什么!
为什么D:\idea_java_projects\chapter11\src\com\hspedu\homework\Person.java中写成
Boat boat=VehiclesFactory.getBoat();会报错;
报的错误:
D:\idea_java_projects\chapter11\src\com\hspedu\homework\Person.java:16:42
java: 不兼容的类型: com.hspedu.homework.Vehicles无法转换为com.hspedu.homework.Boat
public void passRiver(){
Boat boat=VehiclesFactory.getBoat();
boat.work();
}
public void common(){
vehicles=VehiclesFactory.getHorse();
vehicles.work();
}
而在D:\idea_java_projects\chapter11\src\com\hspedu\homework06Try02\Person.java中写成
Boat boat=VehiclesFactory.getBoat();却没有报错!
public void passRiver(){
Boat boat=VehiclesFactory.getBoat();
boat.work();
}
public void common(){
Horse horse=VehiclesFactory.getHorse();
horse.work();
}