接口的目的(续) [英] The purpose of interfaces continued

查看:7
本文介绍了接口的目的(续)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我认为接口是一种强制对象实现一定数量功能的方法,而不必使用继承.有点像合同.我半明白他们的意思.

OK so I gather that Interfaces are a way to enforce that an object implements a certain amount of functionality, without having to use inheritance. Kind of like a contract. And I semi see the point of them.

但是如果你在界面中只有:

But if all you have in the interface is:

 public interface animal{
  void eat(object food);
}

而且它没有这样的实现,那么使用你的接口的人每次都必须从头开始编写.

and it has no implementation as such, then whoever uses your interface has to write it from scratch, every time.

如果您要创建许多类都实现了这些功能,而实现只是略有不同,这将是一项艰巨的工作.

If you are creating a number of classes all implementing such features and the implementation is only slightly different, this is going to be a lot of hard work.

感谢任何帮助我解决这个问题的人,因为我知道这非常重要.

Any help to get my head around this is appreciated because I know it's really important.

推荐答案

接口是在 Java 中创建多重继承的唯一途径.

Interfaces are the only way to create multiple inheritance in Java.

假设您创建了一个类 Animal.包括人类在内的所有动物都扩展了这一点.而这些动物中的每一个都继承了共同的方法,比如吃、呼吸等.

Say you create a class Animal. And all animals, including humans extend that. And each of those animals inherits common methods like eat, breathe, etc.

但现在假设您有一个 MathProblem 类.并且您希望某些类可以通过将问题传递给 solve(MathProblem question) 方法来解决该问题.您知道,HumanComputer 都可以解决数学问题.所以他们都需要能够解决这个问题.您也许可以让计算机扩展一些具有该方法的 MathSolver 类,但是 Human 已经扩展了 Animal,并且不能扩展任何其他类.所以更好的方法是让 MathSolver 成为一个接口,并让 HumanComputer 和任何其他需要解决问题的类都实现它.

But now let's say you have a MathProblem class. And you want to have certain classes that can solve that problem by passing the problem to a solve(MathProblem problem) method. And you know that a Human, but also a Computer might solve the math problem. So they both need to be able to solve that problem. You might be able to get the Computer to extend some MathSolver class that has the method, but Human already extends Animal, and can't extends anything else. So a better way is to make MathSolver an interface and have both Human, Computer, and any other classes that need to solve problems implement that.

另请注意,HumanComputer 可能以完全不同的方式解决问题,因为它们的对象不同.这就是接口最适合的地方.定义某些跨越多个继承层次结构的能力,可以有非常不同的实现,但都可以传递给接受其中任何一个的方法.想想 Comparable 接口;它不是某类对象所具有的东西,所有类型的东西都可以进行比较,并且通常以非常不同的方式进行比较.但是你总是可以对 Comparable 对象的 List 调用 sort,因为你知道它们有一定的顺序,不管它们是否是 NumbersAnimalsComputers 或其他任何东西(只要它们实现 Comparable 并定义它们的排序).

Also note that a Human and a Computer might solve the problems in completely different ways, since their such different objects. That's what interfaces are best for. Defining certain abilities that cut across multiple inheritance hierarchies, and can have very different implementations, but can all be passed to a method that accepts any of them. Think of the Comparable interface; it's not something a certain class of objects has, all sort of things can be compared, and usually in very different ways. But you can always call sort on a List of Comparable objects since you know they have a certain order, no matter if they're Numbers, Animals, Computers or anything else (as long as they implement Comparable and define their ordering).

这篇关于接口的目的(续)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆