关于多态的用法测试

实验代码是关于多态的用法创建了两个抽象父类Animal类和Transport类Animal的子类中Bird类实现了飞翔接口Transport类中的Airport类实现了飞翔接口
多态

// 在容器的泛型中使用父类或接口,完成多态匹配
// 在遍历容器的方法中使用父类或接口作为参数,完成多态匹配
import java.util.ArrayList;
import java.util.List;

interface Iflyable{
	void fly();
}

abstract class Transport{
	String name;
	public Transport(String name){
		this.name=name;
	}
	void addOil(){
		System.out.println("我是给交通工具加油的方法");
	}
	public abstract String toString();
}

class Airport extends Transport implements Iflyable{
	public Airport(String name) {
		super(name);
	}

	public void fly(){
		System.out.println("我是飞机的飞行方法");
	}
	// 已经继承addOil()方法
	
	public String toString(){
		return "airport"+name;
	}
}

abstract class Animal{
	String name;
	int height;
	Animal(){
	}
	Animal(String name,int height){
		this.name=name;
		this.height=height;
	}
	abstract void eat();
	public abstract String toString();
}

class Cat extends Animal{
	int weight;// 比父类多一个属性weight
	Cat(){
	}
	Cat(String name, int height, int weight) {
		super(name, height);
		this.weight=weight;
	}

	public void eat(){
		System.out.println("我是cat的eat方法");
	}
	
	public String toString(){
		return "我是动物"+name+":"+height+":"+weight;
	}
}

class Bird extends Animal implements Iflyable{
	Bird(){
	}
	Bird(String name) {// 比父类少一个属性
		this.name=name;
	}

	public void eat(){
		System.out.println("我是bird的eat方法");
	}
	
	public void fly(){
		System.out.println("我是bird的飞翔方法");
	}
	
	public String toString(){
		return "bird"+name;
	}
}

public class testInterface {

	public static void main(String[] args) {
		Iflyable bird1 = new Bird();// 接口体现的多态
		bird1.fly();
		
		Animal bird2 = new Bird();// 继承体现的多态
		bird2.eat();
		
		// 将Animal的所有子类放入链表
		List<Animal> objectList = new ArrayList<Animal>(10);
		objectList.add(new Cat("cat1",12,13));
		objectList.add(new Bird("bird1"));
		System.out.println("用多态遍历链表<Animal>的结果为");
		takeAnimals(objectList);
		
		// 将Animal的所有子类放入数组
		Animal[] objectArr = new Animal[5];
		objectArr[0]=new Cat("cat2",14,15);
		objectArr[1]=new Bird("bird2");
		System.out.println("用多态遍历数组<Animal>的结果为");
		takeAnimalsArr(objectArr);
		
		// 将所有实现了飞翔接口的类放入链表
		List<Iflyable> objectList2 = new ArrayList<Iflyable>(10);
		objectList2.add(new Airport("boyin747"));
		objectList2.add(new Bird("bird3"));
		System.out.println("用多态遍历链表<Iflyable>的结果为");
		takeIflable(objectList2);
		
		Iflyable[] objectArr2 = new Iflyable[4];
		objectArr2[0] = new Airport("boyin748");
		objectArr2[1] = new Bird("bird4");
		System.out.println("用多态遍历数组<Iflyable>的结果为");
		takeIflableArr(objectArr2);
	}
	
	// 考虑用迭代器模式将对容器的遍历做成统一的
	// 用多态遍历链表<Animal>
	public static void takeAnimals(List<Animal> objectList){
		for(Animal a:objectList){
			System.out.println(a);
		}
	}
	// 用多态遍历链表<Iflyable>
	public static void takeIflable(List<Iflyable> objectList){
		for(Iflyable a:objectList){
			System.out.println(a);// 打印对象名就是调用toString()
		}
	}
	
	// 用多态遍历数组<Animal>
	public static void takeAnimalsArr(Animal[] objectList){
		for(Animal a:objectList){
			System.out.println(a);
		}
	}
	// 用多态遍历数组<Iflyable>
	public static void takeIflableArr(Iflyable[] objectList){
		for(Iflyable a:objectList){
			System.out.println(a);
		}
	}

}
Java中进行多态测试可从继承接口两种多态实现方式入手。 ### 继承实现多态测试方法 继承实现多态主要通过子方法来体现,不同子以不同方式实现相同方法,在同一接口下展现不同行为,但继承会带来较高耦合性,父变化可能影响所有子 [^3]。以下是示例代码: ```java // 父 class Animal { public void makeSound() { System.out.println("动物发出声音"); } } // 子 class Dog extends Animal { @Override public void makeSound() { System.out.println("汪汪汪"); } } class Cat extends Animal { @Override public void makeSound() { System.out.println("喵喵喵"); } } public class InheritancePolymorphismTest { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.makeSound(); cat.makeSound(); } } ``` 在上述代码中,`Animal` 是父,`Dog` 和 `Cat` 是子,它们都重了 `makeSound` 方法。在 `main` 方法中,通过父引用指向子对象,调用 `makeSound` 方法时,会根据实际对象型调用相应子方法,这就是继承实现多态的体现。 ### 接口实现多态测试方法 接口实现多态是在接口中定义方法,在实现该接口中实现这些方法,使用接口实现多态更加灵活 [^1]。以下是示例代码: ```java // 接口 interface Shape { double area(); } // 实现 class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; } } class Rectangle implements Shape { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } @Override public double area() { return length * width; } } public class InterfacePolymorphismTest { public static void main(String[] args) { Shape circle = new Circle(5); Shape rectangle = new Rectangle(4, 6); System.out.println("圆的面积: " + circle.area()); System.out.println("矩形的面积: " + rectangle.area()); } } ``` 在上述代码中,`Shape` 是接口,`Circle` 和 `Rectangle` 是实现,它们都实现了 `area` 方法。在 `main` 方法中,通过接口引用指向实现对象,调用 `area` 方法时,会根据实际对象型调用相应实现方法,这就是接口实现多态的体现。 ### 相关技术 - **动态绑定**:在运行时根据对象的实际型来确定调用哪个方法,而不是在编译时根据引用型确定。上述示例中,父引用或接口引用在运行时会根据实际指向的子对象或实现对象来调用相应的方法,体现了动态绑定 [^1]。 - **方法**:子方法或实现实现接口方法,是多态实现的关键。如 `Dog` 和 `Cat` `Animal` 的 `makeSound` 方法,`Circle` 和 `Rectangle` 实现 `Shape` 接口的 `area` 方法 [^1][^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值