声明的类型和实际类型 [英] Declared types and Actual types
问题描述
我理解在创建这样的新对象时:
I understand that when creating a new object such as this:
GeomObject tri = new Triangle();
更通用,允许更多的可恢复性,但是当像这样创建tri时会发生什么:
is more general and allows for more resuability, but what happens when tri is created like this:
Triangle tri = new Triangle();
由于Triangle是GeomObject的子类,还不是GeomObject吗?声明的类型如何影响编译?谢谢
Since Triangle is a subclass of GeomObject, isn't tri still a GeomObject? How does the declared type affect compilation? Thanks
*添加:另一个问题:说我有
*add: An additional question: say I have
Integer n1 = new Integer(3);
Object n2 = new Integer(4);
System.out.println(n1.compareTo(n2));
我在Eclipse上尝试了这个,即使用n2反转n1,我也遇到了错误。我认为n2.compareTo(n1)会起作用,因为它会调用Object compareTo方法,因为Integer是一个object的实例,所以它是可以通过的,但事实并非如此。你能解释一下吗?
I tried this out on Eclipse and I got errors even if I reversed n1 with n2. I thought that n2.compareTo(n1) would work because it would call the Object compareTo method and since Integer is an instance of object, it would be passable, but this is not the case. Could you explain this?
推荐答案
由于
Triangle
是GeomObject
的子类,不是tri
仍然是GeomObject
?
Since
Triangle
is a subclass ofGeomObject
, isn'ttri
still aGeomObject
?
是的。使用 instanceof
运算符来测试:
Yes it is. Use the instanceof
operator to test this:
System.out.println( (tri instanceof Triangl) ); //prints true
System.out.println( (tri instanceof GeomObject) ); //prints true
System.out.println( (tri instanceof Object) ); //prints true because every class extends from Object
如何声明类型影响编译?
How does the declared type affect compilation?
它不会影响任何事情,只会使你的代码不灵活 in你需要使用 GeomObject
的另一个实现,它不是 Triangle
。
It won't affect in any matter, just will make your code inflexible in case you need to use another implementation of GeomObject
that is not a Triangle
.
更多信息:
- What does it mean to "program to an interface"?
我认为 n2.compareTo(n1)
会起作用,因为它会调用 Object#compareTo
方法
I thought that
n2.compareTo(n1)
would work because it would callObject#compareTo
method
这是不正确的,因为 Object
class没有 compareTo
方法。
This is incorrect since Object
class doesn't have a compareTo
method.
另一方面, n1.compareTo(n2)
将无效,因为您传递的是对象$ c当到
compareTo
方法。 html#compareTo(java.lang.Integer)rel =nofollow noreferrer> 整数#compareTo
收到另一个整数
班级类型。
On the other hand, n1.compareTo(n2)
won't work since you're passing an Object
to the compareTo
method when Integer#compareTo
receives another Integer
class type.
请注意,在声明时:
Object n2 = new Integer(4);
- 变量类型为
对象
,无论您将其初始化为Integer
还是其他类,例如String
。 - 只有被覆盖的方法才会按照对象引用运行时类型的定义运行,这意味着你的
n2
变量包含Integer
,只有在类Integer
中重写的方法来自类Object
将按照Integer
类中的定义运行,所有其他方法,字段,甚至变量本身都将表现为对象
。在Integer
类的情况下,这些方法是等于
,hashCode
和< a href =http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString() =nofollow noreferrer>toString
。 - 上面提供的链接:编程到界面是什么意思?解释了使用的好处接口(或抽象类或泛型类)通过通用接口/类而不是直接实现来概括工作。请注意,在某些情况下,此方法将不适用,例如当你应该使用
Integer
时,使用Object
的当前示例。请注意Object
类方式泛型(至少在这种情况下),所以我不建议使用对象
直接至少你明白你在做什么。 - The variable type will be
Object
, no matter if you initialize it asInteger
or another class e.g.String
. - Only the overridden methods will behave as defined in the object reference run time type, this means if your
n2
variable holds anInteger
, only the methods overridden in classInteger
from classObject
will behave as defined in theInteger
class, all the other methods, fields, even the variable itself will behave asObject
. In case ofInteger
class, these methods areequals
,hashCode
andtoString
. - The link provided above: What does it mean to "program to an interface"? explains the benefits of using an interface (or abstract class or a generic class) to generalize the work through generic interfaces/classes instead of direct implementation. Note that there are cases where this approach won't apply e.g. your current example using
Object
when you should useInteger
. Note thatObject
class is way too generic (at least for this case), so I won't recommend usingObject
directly at least that you understand what you're really doing.
这篇关于声明的类型和实际类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!