java中接口的实际用途是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4436087/
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-14 17:27:10  来源:igfitidea点击:

what is the actual use of interface in java?

javainterfaceabstract-class

提问by kandarp

Possible Duplicates:
Abstract class and Interface class?
Java: interface / abstract classes / abstract method

可能的重复项:
抽象类和接口类?
Java:接口/抽象类/抽象方法

In Java, whatever use of interface is fulfilled by abstract class. I know one advantage of interfaces is that if we implement an interface then we can also extend another class. Is there any other use or advantage of interface in Java?

在 Java 中,接口的任何使用都由抽象类来完成。我知道接口的一个优点是,如果我们实现一个接口,那么我们还可以扩展另一个类。Java 中的接口还有其他用途或优势吗?

采纳答案by FosterZ

What you like : thousands of abstract methods in one Abstract Class and inherit this class OR make as many interfaces for specific abstract methods and use those only you want by inheriting as many interfaces as needed...

你喜欢什么:一个抽象类中有数千个抽象方法并继承这个类,或者为特定的抽象方法制作尽可能多的接口,并通过根据需要继承尽可能多的接口来只使用你想要的那些......

abstract class A
{
 //thousands of abstract method();
 abstract methodA();
 abstract methodB();
 abstract methodC();
}

//OR
interface ForOnlymethodA
{
 void methodA();
}
interface FormethodBandmethodC
{
 void methodB();
 void methodC();
}

So, use that method only what you just need by inheriting particular interface, if you are inheriting Abstractclasses then you are unnecessarily inheriting all methods that you don't need in one class and may be needed in some other classes..

因此,通过继承特定接口仅使用您只需要的方法,如果您正在继承Abstract类,那么您将不必要地继承在一个类中不需要的所有方法,而在其他一些类中可能需要..

回答by Robert

Multiple interfaces can be implemented, but only one class can be extended. A completely abstract class is a lot like an interface, except that an abstract class can contain variables.

可以实现多个接口,但只能扩展一个类。完全抽象的类很像接口,只是抽象类可以包含变量。

It really depends on what you need. C++ allows you to extend as many classes you want, and it turns into a bit of a disaster. The nice thing about having only one superclass is that there's only ever one other set of implementations that you have to worry about (even if the parent has a parent, the parent's particular combination becomes your parent...)

这真的取决于你需要什么。C++ 允许您扩展任意数量的类,但它变成了一场灾难。只有一个超类的好处是,您只需要担心另一组实现(即使父类有父类,父类的特定组合也会成为您的父类......)

Interfaces allow one object to play many roles, but they don't allow code reuse.

接口允许一个对象扮演多种角色,但不允许代码重用。

It's really to simplify thinking about inheritance. On the balance, I think they got it right.

真的是为了简化对继承的思考。总的来说,我认为他们做对了。

回答by Robert

Interfaces allow the nominative typingin Java to work acrossdisjoint class hierarchies.

接口允许主格打字用Java工作跨越不相交的类层次结构

This is due to the "single inheritance" limitation on a class hierarchy and "closed types". I will hold my tongue on subtype polymorphismand Java's implementation of it ;-)

这是由于类层次结构和“封闭类型”的“单一继承”限制。我将对子类型多态性和 Java 对它的实现保持沉默;-)

There are other solutions to this problem such as dynamic typing, structural typing, multiple inheritance, and traits, etc. Each approach has advantages and dis-advantages. Interfaces were just the approach that Java took.

这个问题还有其他解决方案,例如动态类型、结构类型、多重继承和特征等。每种方法都有优点和缺点。接口只是 Java 采用的方法。

回答by Mohamed Saligh

Java interface - provides the data encapsulationwhich means, implementation of the methods can not be seen. The class which extends this interface must implement all the methods declared in it.

Java 接口——提供了data encapsulation哪些手段,方法的实现是看不到的。扩展此接口的类必须实现其中声明的所有方法。

more info: wiki answer

更多信息:维基答案

回答by Eran Medan

Advantages over an abstract class? except the fact you can implement multiple interfaces but extend only one (abstract or not) class, it's the same as an abstract class that all of it's methods are abstract and public

与抽象类相比的优势?除了您可以实现多个接口但仅扩展一个(抽象或非抽象)类这一事实之外,它与抽象类相同,它的所有方法都是抽象和公共的

回答by Todd

Interfaces allow you to use classes in different hierarchies, polymorphically.

接口允许您多态地使用不同层次结构中的类。

For example, say you have the following interface:

例如,假设您有以下接口:

public interface Movable {
    void move();
}

Any number of classes, across class hierarchiescould implement Movablein their own specific way, yet still be used by some caller in a uniform way.

任何数量的跨类层次结构的类都可以Movable以它们自己的特定方式实现,但仍然被某些调用者以统一的方式使用。

So if you have the following two classes:

因此,如果您有以下两个类:

public class Car extends Vehicle implements Movable {
    public void move() {
       //implement move, vroom, vroom!
    }
}

public class Horse extends Animal implements Movable {
    public void move() {
       //implement move, neigh!
    }
}

From the perspective of the caller, it's just a Movable

从调用者的角度来看,这只是一个 Movable

Movable movable = ...;
movable.move();  //who am I?

I hope this helps.

我希望这有帮助。