扩展对象与实现接口 [英] Extending an object vs Implementing an interface

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

问题描述

试图理解一个我在考试中错了的问题:

Trying to understand a question I got wrong on a test:

继承与实现接口有何不同?

How does inheritance differ from implementing interfaces?

  1. 通过继承,一个类将从其超类中获得行为.
  2. 对于接口,类从其实现的接口中获取行为. (这是我选择的那个)
  3. 通过继承,类必须实现其超类定义的方法.
  4. 通过接口,类从其实现的接口中获取实例变量和行为.
  1. With inheritance, a class gains behavior from its superclass.
  2. With interfaces, a class gains behavior from the interface it implements. (this is the one I chose)
  3. With inheritance, a class must implement the methods defined by its superclass.
  4. With interfaces, a class gains both instance variables and behaviors from the interface it implements.

我的想法是接口定义行为,而超类定义特征...或者它们是相同的?还是我完全不了解我的理解?

The way I was thinking is that interfaces define behavior, while superclasses define characteristics... or are they the same? Or am I completely backwards in my understanding?

我想我应该指定我要做知道接口和继承之间的区别.我只是想知道使用术语行为的两个选项.我不知道教授是否对术语term之以鼻,还是他问的问题很不好.

I guess I should specify that I do know the difference between interfaces and inheritance. I'm just wondering about the two options which use the term behavior. I don't know if the prof was nitpicking about terminology, or if he asked the question poorly.

我知道实现接口时,必须实现接口中定义的所有方法.因此,我要说的是,该接口定义了一个类必须的行为,但是扩展了另一个超类(尽管它也定义了某些行为(可以给子类更多的行为),但它并没有这样做.似乎与定义行为的接口一样强.如果该类实现了接口,则可以确保它具有某些行为.

I know that when you implement an interface, you have to implement all the methods as defined in the interface. As such, I would say that the interface defines the behavior that a class must have, but extending another superclass (although it does also define some behaviors (more can be given to the subclass), it doesn't seem to fit as strongly as the interface defining behaviors. If the class implements an interface, you can be sure that it has certain behaviors.

也许这个问题是要问接口本身是否具有行为代码,或者仅仅是定义-如果以这种方式措辞,我会知道答案的.

Maybe the question was meant to ask whether or not the interface itself has the code for the behavior, or if it's just the definition - which if worded that way, I would have known the answer.

推荐答案

我认为您的某些误解可能完全源于语义.也许描述接口的更直接的方法是它定义了API,但没有提供该API的实现.一个警告是,我将使用Java作为示例,但是使用C ++之类的语言,实现接口 is 的一种特殊类型的继承-即从包含纯虚函数的类中继承.

I think some of your misunderstanding might stem purely from semantics. Perhaps a more straightforward way of describing an interface is that it defines an API but does not provide an implementation of that API. One caveat is that I will use Java for my example but in a language like C++, implementing an interface is inheritance of a special sort - namely inheriting from a class consisting of pure virtual functions.

例如,在Java中,您可能将EventListener接口定义为:

In Java, for instance, you might have an EventListener interface defined as:

public interface IEventListener {
    public void handleEvent(Event event);
}

要使用问题的说法,该接口不会说任何实现IEventListener接口的类在收到事件时的行为,它只会确保实现此接口的任何类都具有能够接收的特性事件类型的事件.

The interface does not, to use the question's verbiage, say anything about how a class that implements the IEventListener interface will behave when it receives an event it only ensures that any class implementing this interface will have the characteristic of being able to receive an event of type Event.

另一方面,继承允许超类也继承行为(实现).例如,考虑以下Java基类:

Inheritance, on the other hand, allows super classes to also inherit behavior (implementation). For instance, consider the following Java base class:

public abstract BaseClass {
    public void baseMethod(int value) {
        System.out.println(int);
}

public class SubClass extends BaseClass {
}

任何从BaseClass继承的类都可以获得BaseClass的API(特性)和实现(行为).换句话说,您不仅可以调用instanceOfSubClass.baseMethod(1)这种特性,而且还会导致在BaseClass中定义的行为,即将1打印到控制台.

Any class that inherits from BaseClass gains both the API (characteristics) of BaseClass and also the implementation (behavior). In other words not only can you invoke instanceOfSubClass.baseMethod(1), a characteristic, doing so will result in the behavior defined in the BaseClass, namely 1 being printed to the console.

因此,您的答案(2)不正确,因为接口不仅指定行为(实现)API(特征).继承可以同时处理.

So your answer (2) is incorrect because interfaces do not specify behavior (implementation) only API (characteristics). Inheritance can handle both.

我认为问题的重点是解释当您要共享行为而不仅仅是API时,继承特别有用.也就是说,实现(行为)也可以通过组合共享,并且这样的策略通常更好-有关出色的讨论,请参见Bloch的Effective Java中的第16项.

I think the point of the question is to explain that inheritance is specifically useful when you want to share behavior and not just API. That said, implementation (behavior) can also be shared via composition and such a strategy is often better - see Item 16 in Bloch's Effective Java for an excellent discussion.

这篇关于扩展对象与实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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