为什么在实例化对象时需要列出两种对象类型? [英] Why do you need to list two object types when you instantiate an object?

查看:47
本文介绍了为什么在实例化对象时需要列出两种对象类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将 JungleCat 作为 Cat 的子类(JungleCat extends Cat),然后我说:

If I have JungleCat as a subclass of Cat (JungleCat extends Cat), and then I say:

JungleCat cat1 = new JungleCat();
Cat cat2 = new Cat();
Cat cat3 = new JungleCat();
JungleCat cat4 = new Cat(); //this one is illegal, right?
JungleCat cat5;

我想知道cat1cat2cat3cat4cat4的对象类型是什么cat5?我也想知道为什么实例化一个对象会有冗余:为什么你在实例化一个对象时需要列出两种对象类型.

I'm wondering what are the object types of cat1, cat2, cat3, cat4, and cat5? I'm also wondering why there's redundancy in instantiating an object: why do you need to list two object types when you instantiate an object.

如果这是一个非常基本的问题,我很抱歉,但我只想知道一劳永逸,并以合理的推理得到一个很好的答案,我知道我可以在这里期待(而不是雅虎答案等):P

I'm sorry if this is a really basic question, but it's the sort of thing where I just want to know once and for all and with a good answer with good reasoning, which I know I can expect here (and not Yahoo Answers, etc) :P

推荐答案

在下面的声明中:-

JungleCat cat1 = new JungleCat();

你可以把它分成两部分:-

You can break it up in two parts: -

JungleCat cat1;  // This creates a reference of type `JungleCat`
cat1 = new JungleCat();   // This creates an object of type `JungleCat`.

现在,您正在创建 cat1 引用,指向 JungleCat 对象.引用只不过是指向所创建对象的链接,以便您可以访问它们.

Now, you are making the cat1 reference, point to the JungleCat object. References are nothing but a link to the object that is created, so that you can access them.

您也可以像这样创建对象:-

You can also create object just like this: -

new JungleCat();   // Will create an unnamed object

但是,在上述情况下,您将只能在实例化的地方使用方法和属性.但是,稍后,由于您没有访问该对象的引用,因此您也无法访问它的属性.

But, in the above case, you will only be able to use the methods and properties at the place where you have instantiated. But, later on, since you have no reference to access that object, you can't access it's properties either.

现在,让我们继续第二条语句:-

Now, let's move ahead to the 2nd statement: -

Cat cat = new JungleCat();

在这里,您可以猜到,您有 Cat - Super Class 类型的引用和 JungleCat 类型的对象.这就是我们所说的多态.

Here, as you can guess, you have reference of type Cat - Super Class, and an object of type JungleCat. This is what we call Polymorphism.

所以,基本上,您可以创建任何超类型的引用,并使其指向任何子类型的对象.这很容易理解 - 因为 JungleCat 只是一只猫.因此,您始终可以拥有一个指向 JungleCat 的 Cat 参考点".

So, basically, you can create a reference of any super type, and make it point to an object of any subtype. This is pretty simple to understand - "Since a JungleCat is a Cat only. So, you can always have a Cat reference point to JungleCat".

反之则不然.例如:-

JungleCat ref = new Cat();

现在这是无效的.因为Cat 不一定是JungleCat.它可以是任何其他猫.因此,您不能将 JungleCat 引用指向 Cat 对象.

Now this is not valid. Because a Cat is not neccessarily a JungleCat. It can be any other Cat. So, you can't have your JungleCat reference point to a Cat object.

现在是您真正关心的问题:-

我想知道cat1、cat2、cat3、cat4和cat4的对象类型是什么猫5

I'm wondering what are the object types of cat1, cat2, cat3, cat4, and cat5

好吧,cat1cat2.. 不是对象,而是指向某些对象的引用.并且你可以从上面的解释中推断出它们各自的引用类型.

Well, cat1, cat2.. are not objects, but are references pointing to some objects. And you can infer from the above explanation, the reference type of each of them.

对象类型是在对象创建语句的 RHS 上使用的类型.与new 关键字一起使用的类型是Object 的类型.您可以拥有指向同一对象类型的不同类型的引用.

Object types are the type used on the RHS of the object creation statement. The type used with new keyword, is the type of the Object. You can have different types of reference pointing to the same object type.

因此,您可以让 cat1cat2 引用指向相同的对象类型.

So, you can have both cat1, and cat2 references pointing to the same object type.

这篇关于为什么在实例化对象时需要列出两种对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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