JAVA编程经验-多态设计初步

文献:http://www.verydemo.com/demo_c89_i222168.html

 
JAVA编程心得-多态设计初步

 

 

 

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(this,new Address("武汉"));
	}
	
}

 

 

public abstract class Vihecle {
	public abstract void go(Driver d,Address dest);
}

 

 

 

 

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;
	}
	
}

 

 

public class Car extends Vihecle{
	public void go(Driver d,Address dest) {
		System.out.println(d.getName()+"开车去" + dest.getName());
	}
}

public class Plane extends Vihecle{
	@Override
	public void go(Driver d, Address dest) {
		// TODO Auto-generated method stub
		System.out.println(d.getName()+"坐飞机去" + dest.getName());
	}
}

 

 

public class Test {
	public static void main(String[] args) {
		Driver d = new Driver();
		d.setName("杨小聪");
		d.drive(new Car());
		d.drive(new Plane());
	}
}

 

 

 

 

 

 


你可能感兴趣的:(java编程)