了解Java中的继承和抽象类 [英] Understanding inheritance and abstract classes in Java

查看:124
本文介绍了了解Java中的继承和抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧所以我通过谷歌搜索文档,但是我没有发现任何真正描述我想要回答的内容,所以我在这里问你们。

Alright so I've looked for documentation by googleing, however I haven't found any that really describes what I'm looking to answer, so here I am asking you guys.

所以我得到了继承,以及它是如何工作的。我遇到问题的有时候我看到一个最初定义为一种类型的对象,并设置为不同的类型,我不明白究竟发生了什么。这是一个例子:

So I get inheritance, and how it works. What I'm having a problem with is sometimes I see a object originally defined as one type, and set to a different type, and I don't understand exactly what's happening. Here's an example:

假设我有一个类动物,并且可以扩展动物的猫和狗类。猫,动物和狗都有方法说(),对于猫打印喵和狗打印woof和动物不能说话。

Say I have a class animal, and classes cat and dog which extend animal. Cat, animal and dog all have a method speak() which for cat prints "meow" and for dog prints "woof" and for animal "can't speak".

好吧,所以最后这是我的问题。如果制作一只猫(c)然后运行Animal a = c,究竟会发生什么?如果我运行a.speak()会发生什么?哪种说法叫做?当我改变那样的类型时究竟发生了什么?我是否有任何真正的理由使用它?

Alright so finally here's my question. What exactly happens if a make a cat (c) and then run Animal a = c;? What happens if I run a.speak();? Which speak method is called? What exactly has happened when I change types like that? Will I ever have any real reason to use this?

就抽象方法而言,我的问题是拥有它们究竟是什么意思?在示例中,我已经看到它们已被放入超类中,并且其下的类定义了确切的行为。通过在超类中放置一个抽象方法,需要它下面的所有类来实现它吗?

As far as abstract methods go, my question is what exactly is the point of having them? In the examples I've seen they've been put in super classes and the classes under them define the exact behavior. By putting an abstract method in a super class is one requiring all the classes under it to implement it?

感谢您的帮助!

推荐答案


如果制作一只猫
(c)然后运行Animal a = c,会发生什么?如果我运行a.speak(),会发生
;调用哪个
发言方法?当我改变像
这样的类型时,究竟
究竟发生了什么?我是否有任何真正的理由
来使用它?

What exactly happens if a make a cat (c) and then run Animal a = c;? What happens if I run a.speak();? Which speak method is called? What exactly has happened when I change types like that? Will I ever have any real reason to use this?

始终是真实类的方法,例如在此例如,猫的 speak()方法。

Always the method of the real class, for example in this case, the speak() method of the cat.


就抽象而言方法去,我的
问题究竟是什么点
有它们?

As far as abstract methods go, my question is what exactly is the point of having them?

他们确保例如,每只动物都有一个方法 walk(),你可以调用每只动物。这是一个保证,说每个 Animal 对象都有这种方法,你不必关心它。

They make sure that, for example, every animal has a method walk() that you can call on every animal. It's a warranty that says "every Animal object has this method, you don't have to care about it".

在示例中,我看到
已被放入超类
,其下的类定义了
的确切行为。通过在超类中放置一个抽象的
方法是一个
,要求其下的所有类到
实现它?

In the examples I've seen they've been put in super classes and the classes under them define the exact behavior. By putting an abstract method in a super class is one requiring all the classes under it to implement it?

实现它或者是抽象的,是的。

To implement it or to be abstract, too, yes.

这篇关于了解Java中的继承和抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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