Java 如何借助设计模式使用继承

Java 如何借助设计模式使用继承,java,inheritance,Java,Inheritance,我将如何推广打斗的实施,使之适用于老虎体内的任何动物? 作为a.打架(动物)不仅接受狗型,也接受动物型。 如果不使用狗的实例和狮子的实例我将如何确保它对任何动物都有效 这个问题是不现实的(实际上是在一次采访中问我的),因为你不会通过输入cat(假设我们还有一个接口)来输入dog 但如果我们这样做呢: a.战斗(b)并希望它正常工作 采访者暗示这与设计模式有关。 我真的不确定这是否是一个建设性的问题。我不确定采访你的人暗示这与设计模式有关是什么意思。如果我理解正确,这与设计模式无关。另外,您的代码

我将如何推广打斗的实施,使之适用于老虎体内的任何动物? 作为a.打架(动物)不仅接受狗型,也接受动物型。 如果不使用狗的实例和狮子的实例我将如何确保它对任何动物都有效

这个问题是不现实的(实际上是在一次采访中问我的),因为你不会通过输入cat(假设我们还有一个接口)来输入dog

但如果我们这样做呢:
a.战斗(b)并希望它正常工作

采访者暗示这与设计模式有关。
我真的不确定这是否是一个建设性的问题。

我不确定采访你的人暗示这与设计模式有关是什么意思。如果我理解正确,这与设计模式无关。另外,您的代码已经在做您希望它做的事情了:任何实现animal的东西都将能够与实现animal的任何其他类型的东西对抗。这就是你想要的,对吗?你已经到了。您所要做的就是进行一些更正以使其可编译(fight方法需要一个返回类型,如void)

因为
Animal
中的
fight
方法将另一个
Animal
作为一个参数,所以扩展
Animal
的所有东西都将作为一个参数(
Cat
Dog
Duck
Goose
狮子
老虎
,噢,天啊!)——你写的方式,它已经达到了你的目标。战斗方法不关心它是什么样的
动物
,只要它是
动物

public interface Animal{
    public fight(Animal a){
    }  
}

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location 
public class Tiger implements Animal{
    public fight(Animal a){
    } 
} 

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location
public class Lion implements Animal{
    public fight(Animal a){
    }
} 

public testClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();//because we don't know how to create object directly.
        Animal b = factoryMethodToReturnLionObject();
    }
}
事实上,如果你想让它以另一种方式出现,让动物只能与同一类型的其他动物搏斗,你就必须走自己的路,并使用如下泛型:

public interface Animal {
    public <T extends Animal> fight(T a);
}
动物:

public interface Animal {
    void fight(Animal otherAnimal);
}

public class Tiger implements Animal {
    @Override
    public void fight(Animal otherAnimal) {
        //Do Stuff
    } 
} 

public class Lion implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class Dog implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class TestClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();
        Animal b = factoryMethodToReturnLionObject();
        a.fight(b); // This works
        b.fight(a); // This also works

        Dog dog = factoryMethodToReturnDogObject();
        Tiger tiger = factoryMethodToReturnTigerObject();
        dog.fight(tiger); // This works.
        tiger.fight(dog); // This also works.  You're good to go!

    }
}
公共界面动物{
无效战斗(另一种动物);
}
老虎:

public interface Animal<A extends Animal<A>> {
    void fight(A otherAnimal);
}
公共类老虎实现动物{
@凌驾
公开无效搏斗(老虎或其他动物){
//做事
} 
}
狮子:

公共类Lion实现动物{
@凌驾
公众虚空之战(狮子){
//做事
}
}

我不确定面试你的人暗示这与设计模式有关是什么意思。如果我理解正确,这与设计模式无关。另外,您的代码已经在做您希望它做的事情了:任何实现animal的东西都将能够与实现animal的任何其他类型的东西对抗。这就是你想要的,对吗?你已经到了。您所要做的就是进行一些更正以使其可编译(fight方法需要一个返回类型,如void)

因为
Animal
中的
fight
方法将另一个
Animal
作为一个参数,所以扩展
Animal
的所有东西都将作为一个参数(
Cat
Dog
Duck
Goose
狮子
老虎
,噢,天啊!)——你写的方式,它已经达到了你的目标。战斗方法不关心它是什么样的
动物
,只要它是
动物

public interface Animal{
    public fight(Animal a){
    }  
}

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location 
public class Tiger implements Animal{
    public fight(Animal a){
    } 
} 

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location
public class Lion implements Animal{
    public fight(Animal a){
    }
} 

public testClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();//because we don't know how to create object directly.
        Animal b = factoryMethodToReturnLionObject();
    }
}
事实上,如果你想让它以另一种方式出现,让动物只能与同一类型的其他动物搏斗,你就必须走自己的路,并使用如下泛型:

public interface Animal {
    public <T extends Animal> fight(T a);
}
动物:

public interface Animal {
    void fight(Animal otherAnimal);
}

public class Tiger implements Animal {
    @Override
    public void fight(Animal otherAnimal) {
        //Do Stuff
    } 
} 

public class Lion implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class Dog implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class TestClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();
        Animal b = factoryMethodToReturnLionObject();
        a.fight(b); // This works
        b.fight(a); // This also works

        Dog dog = factoryMethodToReturnDogObject();
        Tiger tiger = factoryMethodToReturnTigerObject();
        dog.fight(tiger); // This works.
        tiger.fight(dog); // This also works.  You're good to go!

    }
}
公共界面动物{
无效战斗(另一种动物);
}
老虎:

public interface Animal<A extends Animal<A>> {
    void fight(A otherAnimal);
}
公共类老虎实现动物{
@凌驾
公开无效搏斗(老虎或其他动物){
//做事
} 
}
狮子:

公共类Lion实现动物{
@凌驾
公众虚空之战(狮子){
//做事
}
}

我不确定面试你的人暗示这与设计模式有关是什么意思。如果我理解正确,这与设计模式无关。另外,您的代码已经在做您希望它做的事情了:任何实现animal的东西都将能够与实现animal的任何其他类型的东西对抗。这就是你想要的,对吗?你已经到了。您所要做的就是进行一些更正以使其可编译(fight方法需要一个返回类型,如void)

因为
Animal
中的
fight
方法将另一个
Animal
作为一个参数,所以扩展
Animal
的所有东西都将作为一个参数(
Cat
Dog
Duck
Goose
狮子
老虎
,噢,天啊!)——你写的方式,它已经达到了你的目标。战斗方法不关心它是什么样的
动物
,只要它是
动物

public interface Animal{
    public fight(Animal a){
    }  
}

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location 
public class Tiger implements Animal{
    public fight(Animal a){
    } 
} 

//this interface is not accessible but exists somewhere in some other package and we are unaware about its exact location
public class Lion implements Animal{
    public fight(Animal a){
    }
} 

public testClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();//because we don't know how to create object directly.
        Animal b = factoryMethodToReturnLionObject();
    }
}
事实上,如果你想让它以另一种方式出现,让动物只能与同一类型的其他动物搏斗,你就必须走自己的路,并使用如下泛型:

public interface Animal {
    public <T extends Animal> fight(T a);
}
动物:

public interface Animal {
    void fight(Animal otherAnimal);
}

public class Tiger implements Animal {
    @Override
    public void fight(Animal otherAnimal) {
        //Do Stuff
    } 
} 

public class Lion implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class Dog implements Animal {
    @Override
    public fight(Animal otherAnimal) {
       //Do Stuff
    }
} 

public class TestClass{
    public static void main(String[] args){
        Animal a = factoryMethodToReturnTigerObject();
        Animal b = factoryMethodToReturnLionObject();
        a.fight(b); // This works
        b.fight(a); // This also works

        Dog dog = factoryMethodToReturnDogObject();
        Tiger tiger = factoryMethodToReturnTigerObject();
        dog.fight(tiger); // This works.
        tiger.fight(dog); // This also works.  You're good to go!

    }
}
公共界面动物{
无效战斗(另一种动物);
}
老虎:

public interface Animal<A extends Animal<A>> {
    void fight(A otherAnimal);
}
公共类老虎实现动物{
@凌驾
公开无效搏斗(老虎或其他动物){
//做事
} 
}
狮子:

公共类Lion实现动物{
@凌驾
公众虚空之战(狮子){
//做事
}
}

我不确定面试你的人暗示这与设计模式有关是什么意思。如果我理解正确,这与设计模式无关。另外,您的代码已经在做您希望它做的事情了:任何实现animal的东西都将能够与实现animal的任何其他类型的东西对抗。这就是你想要的,对吗?你是