public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//定义 数组
Fruit[] fruits = { new Banana(15), new Apple(7), new Orange(5) };
//循环数组
for (Fruit fruit : fruits) {
//获取对象名称
Class c=fruit.getClass();
System.out.println(c.getName());
//输出水果重量
System.out.println("重量是:"+ fruit.getWeight());
}
}
}
/**
* 水果抽象类
*/
abstract class Fruit {
//获取重量
abstract public double getWeight();
}
/**
* 橙子类
*/
class Orange extends Fruit {
private double weight;//定义 重量
public Orange(double weight) {
this.weight = weight;
}
//重写体重方法
public double getWeight() {
return weight;
}
}
/**
*
*香蕉类
*
*/
class Banana extends Fruit {
private double weight;//定义 重量
public Banana(double weight) {
this.weight = weight;
}
@Override
public double getWeight() {
return weight;
}
}
/**
* 苹果
*/
class Apple extends Fruit {
private double weight;//定义 重量
public Apple(double weight) {
this.weight = weight;
}
//重写体重方法
public double getWeight() {
return weight;
}