如何创建具有不同属性的对象 java class

How to create objects having attributes from differents java class

我有一个 class 生物和另一个扩展生物的 class 飞行和不死生物。我怎样才能创建一个同时具有飞行和亡灵属性的对象?

class Creature(){
String name;
public Creature(String name){
    this.name=name;
}
class Flying extends Creature{
    ...
}
class Undead extends Creature{
    ...
}

Object creature = new Object();//Both Flying and Undead

还有其他方法吗?还是我应该使用其他方法?

在 Java 中,您不能拥有从多个超级 class 继承的 class。您最好使用接口,因为您可以使用更多已实现的接口。 接口是类似于 classes 的对象。在接口中你可以有变量和方法,坚果定义和给方法和变量赋值应该在实现接口的 class 中完成。 例如:

     Interface MyInterface 
          {
             int myVar;
             void myMethod (int var);
          }

    class MyClass implements MyInterface // with comma(,) you can separate multiple interfaces
 {
    void myMethod (int var) {//do something}
    int myVar = 1;
    }

接口名为 FlyingUndeadCreature 也许你可以做你想做的事.

here您可以了解有关接口的更多信息。