为什么要使用接口,多重继承与接口,接口的好处? [英] Why to use Interfaces, Multiple Inheritance vs Interfaces, Benefits of Interfaces?

查看:128
本文介绍了为什么要使用接口,多重继承与接口,接口的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这件事情仍然有些困惑。我到目前为止所发现的是

I still have some confusion about this thing. What I have found till now is

(这里已经提出了类似的问题,但我还有其他一些观点。)

(Similar questions have already been asked here but I was having some other points.)



  1. 接口是仅抽象方法和最终字段的集合。

  1. Interface is collection of ONLY abstract methods and final fields.

Java中没有多重继承。

There is no multiple inheritance in Java.

接口可用于在Java中实现多重继承。

Interfaces can be used to achieve multiple inheritance in Java.

继承的一个优点是我们可以在派生类中使用基类代码而无需再次编写它。可能这是继承最重要的事情。

One Strong point of Inheritance is that We can use the code of base class in derived class without writing it again. May be this is the most important thing for inheritance to be there.


现在..


Q1。由于接口只有抽象方法(没有代码)所以我们怎么说如果我们实现任何接口那么它是继承?我们没有使用它的代码。

Q1. As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code.

Q2。如果实现接口不是继承,那么如何使用接口来实现多重继承?

Q2. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ?

Q3。无论如何使用Interfaces有什么好处?他们没有任何代码。我们需要在我们实现它的所有类中反复编写代码。

Q3. Anyhow what is the benefit of using Interfaces ? They are not having any code. We need to write code again and again in all classes we implement it.

那么为什么要建立界面?

注意:我找到了一个接口有用的案例。它的一个例子就像在Runnable接口中我们有public void run()方法,我们在其中定义线程的功能,并且内置编码,该方法将作为单独的线程运行。所以我们只需要编写线程中的代码,Rest是预定义的。但是这个东西也可以用抽象类和所有东西来实现。

NOTE : I have found one case in which interfaces are helpful. One example of it is like in Runnable interface we have public void run() method in which we define functionality of thread and there is built in coding that this method will be run as a separate thread. So we just need to code what to do in thread, Rest is pre-defined. But this thing also can be achieved using abstract classes and all.

那么使用接口的确切好处是什么?我们使用Interfaces实现了多重继承吗?

推荐答案

接口是最终静态字段和抽象的集合方法(Newly Java 8增加了对在接口中使用静态方法的支持)。

Interfaces are collection of final static fields and abstract methods (Newly Java 8 added support of having static methods in an interface).

接口是在我们知道必须完成某项任务的情况下进行的,但它应该如何进行要做的可以有所不同。换句话说,我们可以说我们实现了接口,以便我们的类开始以特定的方式运行。

Interfaces are made in situations when we know that some task must be done, but how it should be done can vary. In other words we can say we implement interfaces so that our class starts behaving in a particular way.

让我用一个例子来解释,我们都知道动物是什么。像狮子是动物,猴子是动物,大象是动物,牛是动物,等等。现在我们知道所有动物都吃东西睡觉了。但每只动物吃东西或睡觉的方式可能有所不同。像狮子一样通过捕猎其他动物来吃东西,就像母牛吃草一样。但两人都吃。所以我们可以有这样的伪代码,

Let me explain with an example, we all know what animals are. Like Lion is an animal, monkey is an animal, elephant is an animal, cow is an animal and so on. Now we know all animals do eat something and sleep. But the way each animal can eat something or sleep may differ. Like Lion eats by hunting other animals where as cow eats grass. But both eat. So we can have some pseudo code like this,

interface Animal {
    public void eat();
    public void sleep();   
}

class Lion implements Animal {
    public void eat() {
        // Lion's way to eat
    }

    public void sleep(){
         // Lion's way to sleep
    }
}

class Monkey implements Animal {
    public void eat() {
        // Monkey's way to eat
    }

    public void sleep() {
        // Monkey's way to sleep
    }
}

根据上面提到的伪代码,任何能够进食或睡觉的东西都会被称为动物或我们可以说所有动物都必须吃饭和睡觉,但吃饭和睡觉的方式取决于动物。

As per the pseudo code mentioned above, anything that is capable of eating or sleeping will be called an animal or we can say it is must for all animals to eat and sleep but the way to eat and sleep depends on the animal.

如果是接口,我们只继承行为,不是类继承的实际代码。

In case of interfaces we inherit only the behaviour, not the actual code as in case of classes' inheritance.


Q1。由于接口只有抽象方法(没有代码)所以我们怎么说如果我们实现任何接口那么它是继承?我们没有使用它的代码。

Q1. As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code.

实现接口是另一种继承。它与类的继承不相似,因为继承子类获得了从基类重用的实际代码。

Implementing interfaces is other kind of inheritance. It is not similar to the inheritance of classes as in that inheritance child class gets the real code to reuse from the base class.


Q2。如果实现接口不是继承,那么如何使用接口来实现多重继承?

Q2. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ?

据说因为一个类可以实现多个一个接口。但是我们需要理解这种继承与类的继承不同。

It is said because one class can implement more than one interfaces. But we need to understand that this inheritance is different than classes' inheritance.


Q3。无论如何使用Interfaces有什么好处?他们没有任何代码。我们需要在我们实现它的所有类中反复编写代码。

Q3. Anyhow what is the benefit of using Interfaces ? They are not having any code. We need to write code again and again in all classes we implement it.

实现一个接口会强制它必须覆盖所有抽象方法的类。

Implementing an interface puts compulsion on the class that it must override its all abstract methods.

在我的书中阅读更多内容此处和< a href =http://www.amazon.in/Way-Core-Java-Gurparsad-Singh/dp/9386035553/ref=sr_1_1?ie=UTF8&qid=1466577389&sr=8-1&keywords=the+方式+到+核心+ + javarel =noreferrer>这里

Read more in my book here and here

这篇关于为什么要使用接口,多重继承与接口,接口的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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