java语言编写程序

2024-11-07 20:45:44
推荐回答(1个)
回答1:

第一题:
public class Cuboid {
double length;
double width;
double height;
}

public class CuboidWeight extends Cuboid {

double weight;
}

public class CuobidValue extends CuboidWeight {
double value;
}

第二题:
public class Animal implements Eatable {

@Override
public void haoToEat() {

}

@Override
public String toString() {
return super.toString();
}

}

public class Apple extends Fruit {

@Override
public String toString() {
return super.toString();
}
}

public class Chicken extends Animal {

@Override
public String toString() {
return super.toString();
}
}

public interface Eatable {

public abstract void haoToEat();

}

public class Elephant extends Animal {

@Override
public String toString() {
return super.toString();
}

}

public class Fruit implements Eatable {

@Override
public void haoToEat() {

}

}

public class Orange extends Fruit {

@Override
public String toString() {
return super.toString();
}

}

public class Tiger extends Animal {

@Override
public String toString() {
return super.toString();
}

}

测试程序:
public class Test {

public static void showObject(Object o){

System.out.println(o);

if(o instanceof Eatable)

((Eatable) o).haoToEat();

}
}