public class Driver { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void drive(Vihecle v) { v.go(new Address("东北")); } public void drive(Car c, Address dest) { c.go(dest); } }Address
public class Address { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address(String name) { super(); this.name = name; } }Vihecle
public abstract class Vihecle { public abstract void go(Address dest); } //Movable goCar
public class Car extends Vihecle { public void go(Address dest) { System.out.println("一路哼着歌,冒着烟,去了" + dest.getName()); } }Broom
public class Car extends Vihecle { public void go(Address dest) { System.out.println("一路哼着歌,冒着烟,去了" + dest.getName()); } }Test
public class Test { public static void main(String[] args) { Driver d = new Driver(); d.setName("老张"); d.drive(new Broom()); } }相关输出 :
一路扫着土去了东北one hour to OO 一小时OO教程
Next :
谈谈工具
@1, 思想是战略高度的东西
@2, 工具是战术高度的东西
工具的学习:
(1), 敢用
(2), 勤用
项目背景 :东北人
封装继承多态
* 追求完美是一种缺陷
多态
(1), 核心中的核心
(2), 可扩展性(Extensibility)
注意 :
有这类事物可以作为抽象类。事物的特性设计成接口。