多态练习题(员工使用不同的交通工具回家)

package com.Summer_0427.cn;
/**
 * @author Summer
 * 员工使用不同的交通工具回家
 *
 */
abstract class Transport{
    public abstract void run();
}

class Car1 extends Transport{

    @Override
    public void run() {
        System.out.println("汽车行驶中....");
        
    }
    
}

class Subway extends Transport{

    @Override
    public void run() {
        System.out.println("地铁行驶中......");
        
    }
    
}

class Employee{
    private String name;
    public Employee(String name) {
        this.name = name;
    }
    void goHome(Transport trans){
        System.out.println("员工"+name+"回家了!");
        trans.run();
    }
}

public class TestEmployeeHome {
    
    public static void main(String[] args) {
        Employee a = new Employee("艾编程");
        Car1 car = new Car1();
        a.goHome(car);
        Employee b = new Employee("艾学习");
        Subway sub = new Subway();
        a.goHome(car);

    }

}

 

posted @ 2019-04-28 00:19  Geek张东坡  阅读(491)  评论(0编辑  收藏  举报