Java 用相同的方法实现两个接口的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18511736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:40:48  来源:igfitidea点击:

Class implementing two interfaces with same method?

javamethodsinterfaceabstract

提问by Solomon Bindavid

/* Class name : Fish.java */
interface Fish 
{
   public void eat();
   public void travel();
}

/* Class name : Mammals.java */
interface Mammals 
{public void eat();
   public void travel();}
/*Amphibians mean living two lives (on land as well as on water). */

/Main Class/

/主类/

public class Amphibians implements Mammals,Fish
 {
     public void eat()
   {          
   System.out.println("Amphibians eating");
   }
       public void travel()
   {
      System.out.println("Amphibians traveling");
   } 

/*Main Method*/
   public static void main(String args[])
 {
      Amphibians a = new Amphibians();
      a.eat();
      a.travel();
   }

}

}

Here interface implements in this class.Basically interface inheritances two or more class but here both different class in same method use then both method which way implement in one class. Please check error correct code.

这里接口在这个类中实现。基本上接口继承了两个或多个类,但是这里两个不同的类在同一个方法中使用然后两种方法在一个类中实现。请检查错误正确的代码。

回答by Fritz

If you want to create interface inheritance, you can do it like this:

如果你想创建接口继承,你可以这样做:

public interface Birds extends Animal

Now, by implementing Birdsyou'll have all of Bird's methods plus the Animalones. It shouldn't actually matter if a class implements Animalor Birdif the methods are the same, one particular class defines one behaviour.

现在,通过实施,Birds您将拥有所有Bird的方法以及这些Animal方法。一个类是否实现AnimalBird方法是否相同实际上并不重要,一个特定的类定义了一种行为。

For example, if ,say, a Parrotimplements travelas an animal or as a bird, shouldn't it flyin both cases?

例如,如果,比如说,一个Parrot工具travel作为一个动物或作为一个bird,它不应该在这两种情况下飞行吗?

回答by Suresh Atta

I guess you failed to describe properly

我猜你没有正确描述

You may want to write

你可能想写

public class MammalAni implements Animal,Birds{

Now your doubt is both interfaceshave the same name method eat()and travel().So you confused,how java executes them.

现在您的疑问是interfacesmethodeat()travel().So都具有相同的名称。所以您很困惑,java 是如何执行它们的。

If you have two methods with same name in two interfaces, and Some Class implemented with both the interfaces then that one implementation acts for both the interface..

如果在两个接口中有两个同名的方法,并且 Some Class 用这两个接口实现,那么一个实现对两个接口都起作用。

回答by Agyant Tiwari

Well, I am unsure about what you want to know. But if you want to know, can the class MammalAni implement both the interfaces. Then yes, but calling eat() or travel() for any of the interfaces will give the same result which is defined in the MammalAni class. I hope this helps.

好吧,我不确定你想知道什么。但是,如果您想知道,MammalAni 类是否可以实现这两个接口。那么是的,但是为任何接口调用eat() 或travel() 将给出在MammalAni 类中定义的相同结果。我希望这有帮助。

回答by Narendra Pathai

Interfaces methods must be implemented by the concrete class that implements them.

接口方法必须由实现它们的具体类实现。

Now suppose there are two interfaces then both say the same thing that concrete class must implement the method eat().

现在假设有两个接口,那么它们都说具体类必须实现该方法eat()

Now in your case both interfacesay that concrete class must implement methods eat()and travel(). So when you implement both interfaces you only need a single implementation.

现在,在您的情况下,interface都说具体类必须实现方法eat()travel(). 因此,当您实现这两个接口时,您只需要一个实现

Suggestion:

建议:

public interface CanEat{
    public void eat();
}

public interface CanTravel{
    public void travel();
}

public interface Animal extends CanEat,CanTravel{
    //only methods specific for animal will be here
}

public interface Birds extends CanEat,CanTravel{
    //only methods specific to birds will be here like flying
}

So that tomorrow if you create a robotic Animal
public interface RoboticAnimal extends CanTravel{
   //no need for using CanEat interface as robot does not eat
}