未登录,请登录后再发表信息
最新评论 (0)
播放视频

编程面试题8:如何模拟一个多重继承

IQ 8: How to simulate multiple inheritance?

How are you guys doing today?
大家今天过得好吗?
Today I wanna talk to you guys about multiple inheritance.
今天我想跟大家聊聊多重继承
Here’s the question.
问大家个问题
How do you simulate multiple inheritance in Java?
如何在Java里模拟多重继承呢?
So I got this question today while going on an interview.
今天我在面试里就遇到了这个问题
And I was tongue-tied, you know, didn’t really know what to say
我一时语塞 你懂的 不太知道怎么回答
because I’ve never had to deal with
因为我从来没有在工作中
this concept of multiple inheritance within a workplace.
遇到过多重继承的概念
So, the question is how do you simulate multiple inheritance in Java.
我们的问题是 怎么在Java中模拟多重继承
Alright, so first of all,
行 那么首先
we know that if you are just trying to inherit the properties or methods
我们知道 如果你只是设法从一个类
from one class to another,
继承属性或者方法到另一类
we would just simply use the extends keyword.
那只需简单地使用extends关键字
And, basically,
而且 大体上
this is something that’s pretty common,
这种做法 在你们这么多
for a lot you guys out there who work with Java.
用Java的人中非常普遍
I’m sure you know exactly how to use the extends keyword.
大家肯定知道怎么用extends关键字
It’s basically just used for inheritance.
它基本上就是用于继承的
But when it comes to Java,
但说到Java
you wanna find a way to simulate multiple inheritance.
你得想办法模拟多重继承
So in order to do that, we need two things.
我们需要两样东西来完成任务
Now the two things that we need
我们需要的 就是接口
are interface and implements keyword.
和implements关键字这两样东西
I’m sure you guys know the interfaces,
你们肯定知道啥是接口
and interface is basically a class that consists of implicit abstract methods
接口 总的说来是指一个由隐含的抽象方法组成的类
which are defined by classes that implement that interface.
它由实现该接口的类来定义
Another thing that we need is the implements keyword,
我们需要的另一样东西是implements关键字
and it’s basically a keyword signifying that
总的说来这个关键字标志着
you’re going to implement the methods of an interface.
你将要实现一个接口中的方法
And to explain that a little bit more clear,
为了解释得稍微再清楚些
let’s just get right to the code.
让我们转到代码来看看吧
Ok, so
好的 那么
the first thing I have here is my application object
首先 这是应用程序对象
that consists of a main method here.
它由一个main方法组成
But before we even get into that,
不过在我们真正进入main方法前
we need to create three classes.
我们得先创建三个类
This first class that we create
我们创建的第一个类
is gonna be called Dog.
叫做Dog
Alright, Dog class here.
好的 这就是Dog类
I’m gonna create another class, File, New,
接下来创建另一个类 点击File 选择New
Class, the name of this other class: Poodle,
点击Class 这个类的名字叫Poodle
the best looking dogs in the world.
它们是世界上最好看的狗

Then we’re gonna create one more class.
然后我们再创建一个类
Here.
这里
We’re gonna name this GoldenRetriever.
我们给它取名叫GoldenRetriever
I’m gonna go to Finish.
点击Finish
Alright. So we have our 3 classes here,
好的 所以这里有三个类了
the Dog, the Poodle and the GoldenRetriever.
Dog类 Poodle类和GoldRetriever类
The first thing I wanna do is go to our Poodle class.
首先要到Poodle类这里
Alright. So we wanna turn this Poodle class into an interface,
好的 我们打算把这个Poodle类转换成一个接口
so we’re just simply going to replace the class with interface.
所以只要简单地把这里的class改成interface
As I said before,
正如我之前所说
interfaces are classes that consist of implicit abstract methods
接口是指由隐含抽象方法组成的类
So I’m gonna create two abstract methods right now.
所以现在我要创建两个抽象方法
public abstract
public abstract
void
void
poodleSpeak
poodleSpeak()
And also, I mean you really don’t need this abstract keyword here.
而且我觉得 如果你不想在这里使用abstract关键字
If you don’t want it there,
其实也可以不放
yeah, you really don’t have to have this here if you don’t want it,
嗯 你不想要的话 也不是非得有的
because if you’re in interface,
因为你在一个接口里
you create a method, it’s pretty much understood that
创建了一个方法 这个方法……
the method that you’re
你创建的这个方法
that you’re creating here is going to be abstract
将会是抽象的 这很容易理解
since you’re inside of an interface.
因为你在一个接口里面嘛
It’s pretty much there by default.
默认情况下就差不多是这样
We’re also gonna create another abstract method
我们还要再创建一个抽象方法
public void poodleWalk
public void poodleWalk()
Alright, save.
好的 保存
And we’re gonna do the same thing for our GoldenRetriever class.
在GoldRetriever类也要进行同样的步骤
Change this class to interface,
把class换成interface
and we’re gonna create two methods
以及创建两个方法
public void goldenSpeak
public void goldenSpeak()
goldenRetrieverSpeak. Let’s do that.
哦 这里应该是goldenRetrieverSpeak
goldenRetrieverSpeak
goldenRetrieverSpeak()
and public void goldenRetrieverWalk
以及public void goldenRetrieverWalk()
OK, so we have our two classes,
好 那么我们让Poodle类
the Poodle and GoldenRetriever interface
和GoldenRetriever类 变为了接口
which each contain their own implict abstract methods.
每一个接口里都包含各自的隐含抽象方法
So next thing you wanna do is go to our Dog class.
那么下一步该去Dog类了
Now in our Dog class, what we need just as I say in my presentation,
现在在Dog类里 我刚刚讲过 我们需要的……
in order to incorporate multiple inheritance,
我们需要implements关键字
we need the keyword implements.
来合并多重继承
And what we wanna do is implement
我们想做的是同时实现
both the Poodle and GoldenRetriever class.
Poodle和GoldenRetriever类
Poodle and GoldenRetriever class.
Poodle和GoldRetriever类
When we’re implementing these interfaces, you’re gonna see up here.
大家看啊 在实现这些接口时
We’re gonna have a problem under Dog.
我们会在Dog类中遇到一个问题
Now it’s complaining that we have not… added the unimplemented methods
系统报错了 说是添加了未实现的方法
because these methods need to be implemented.
因为这些方法需要被实现
Pretty much, these methods
大致上 这些方法
these abstract methods and these classes need to be implemented,
这些抽象方法和这些类都需要被实现
so you have to create methods for each
所以你得为每个类
one of these classes.
创建方法
We hover over here, add unimplemented methods
将鼠标悬停在这里 添加未实现的方法
then we just go in and add our code
然后进入内部添加代码
Alright, so for this goldenRetrieverSpeak,
行 对于这个goldenRetrieverSpeak
we just wanna simply
我们只想简单地
print out Golden Bark
输出“Golden Bark”
For goldenRetrieverWalk method, system out
对于goldeRetrieverWalk方法 System.out
Golden Walk
“Golden Walk”
Then we wanna go down to our poodleSpeak
接下来移到下面poodleSpeak这里
and we will print this out to the console
然后我们将把这个内容输出到控制台:
Poodle Walk
“Poodle Walk”
was Speak, actually.
打错了 实际上是Speak
Got a brain fart. My bad.
脑子进水了 抱歉
Then we wanna go Poodle Walk.
然后这里 我们要打出“Poodle Walk”
We save this.
把这个保存
So, what we’ve done here
所以我们在这里做的
is pretty much just implemented each one of the methods from
大抵就是实现了Poodle和GoldRetriever
the Poodle and GoldenRetriever abstract class
抽象类里的每个方法
Now what we wanna do now is make our way back to the object,
现在要做的是回到对象
well, the application object class.
就是这个应用程序对象类
Now, this is what we’re gonna actually start incorporating multiple inheritance.
现在 我们才真正开始多重继承
So, in order to do that,
所以为此
what we need to do is set the base class which is Dog
我们把Dog类设为基类
and assign that to an interface object reference.
并且赋予其接口对象的引用
So this is how we would go about doing that.
这就是我们要做的事
And I would just point out we do have these two interfaces:
我要指出 我们确实是有两个接口的:
Poodle, as well as GoldenRetriever.
Poodle以及GoldRetriever
So we wanna do is actually go Poodle
那么我们要做的其实是输入
myPoodle equals new Dog
Poodle myPoodle = new Dog()
Alright, what’s happening here is that
好的 在这里
we’re assigning this base class as I said before to this interface object reference,
我们把之前说的基类赋给这个接口对象的引用
which is pretty much just a way of inheritting
差不多就是凭借这样一个手段
all of the methods assigned to Poodle
通过这里的Dog类 来继承
through this Dog class here.
Poodle中的所有方法
Alright, so we also wanna do the same for GoldenRetriever.
好的 那么也对GoldenRetriever实施同样的步骤
…Retriever equals new Dog.
…Retriever = new Dog()
Alright.
好的
Now, in order to call these methods,
现在 为了调用这些方法
we simply
我们只需
just call the methods just like this
简单地 就像这样调用方法
myPoodle.poodleSpeak
myPoodle.poodleSpeak()
myPoodle.poodleWalk
myPoodle.poodleWalk()
and to make this a little bit more organized,
然后为了让代码看起来更有条理些
let’s just add both of these up here.
把这两行移到上面去
Alright, and we wanna do the same thing for the GoldenRetriever
行 现在对GoldRetriever也同样做一遍
myGoldenRetriever.goldenRetrieverSpeak
myGoldenRetriever.goldenRetrieverSpeak()
and myGoldenRetriever.goldenRetrieverWalk
然后myGoldenRetriever.goldenRetrieverWalk()
And this is pretty much in
这和我之前讲过的差不多
as I said before, this is a way to incorporate multiple interitance in Java.
是一种在Java中进行多重继承的方式
And, basically, what’s happening here is that
还有 这里
instead of actually using the extends keyword,
我们并没有使用extends关键字
so, for example, if you were extending
关于这个关键字
if you were trying to inherit from a parent class in Java, you’re only allowed to do that once,
在Java中 只能从一个父类继承一次
you cannot inherit from multiple classes in Java
而且不可以从多个类继承
so this is a way to actually go around and get around that.
所以我介绍的实际上是一种绕过它的方式
So let me just give you an example here.
举个例子
So, for example, if you wanna to inherit,
比如 如果你想要
from Dog class, for example,
从Dog类继承
let’s go extends Dog
那就输入extends Dog
and then we wanna inherit from another class,
然后我们还想继承另一个类
we’re not allowed to do that.
就不可以这样做了
So just think of this extends keyword,
思考一下这个关键字
as this here, this polymorphic object right here.
这里 这个多态对象
Poodle myPoodle equals new Dog.
Poodle myPoodle = new Dog()
So we’re inheritting from Poodle,
因此这里是继承了Poodle
we’re also inheritting from this GoldenRetriever.
同时也继承了GoldenRetriever
And that’s pretty much how you go around
这差不多就是在Java中绕开extends
or incorporating multiple inheritance with Java.
进行多重继承的办法
So what I wanna do now is saving this
那么现在 保存文件
and then actually run this application till we get it.
再运行这个应用程序
And as a Java application,
作为一个Java应用程序
if you get exactable, we’re looking for the Poodle Speak,
运行之后 就能找到“Poodle Speak”
we have the Poodle Walk, the
“Poodle Walk”
Golden Speak and Golden Walk.
“Golden Speak” 还有“Golden Walk”
And that’s how you’ll go about incorporating multiple inheritance.
这就是实现多重继承的方法
So if you guys have any questions,
那么如果大家有啥问题
please leave them in the comments below.
请在下方的评论区留言
Don’t forget to like the video if you feel like you’ve learn something here.
如果你想在这儿学点东西 别忘了给视频点赞
Err… also subscribe, and see you in the next video.
呃…… 也订阅下吧 下个视频见

发表评论

译制信息
视频概述

一般我们在Java中继承时,会使用extends关键字,但是这种手段让我们只能继承一个类的方法。本视频介绍了一种使用implements关键字和接口来在Java中模拟多重继承的手段。

听录译者

荼荼

翻译译者

iBRIT

审核员

审核员CH

视频来源

https://www.youtube.com/watch?v=mvUgfRvIIlo

相关推荐