[英]Can't access methods in sub class when using the super as the reference type
I have a problem where I have some class say 我有一个上课的问题
public class Foo {
public Foo() {
// Do stuff
}
public void generalMethod() {
//to general stuff
}
}
public class Bar extends Foo {
public Bar() {
//do stuff
}
public void uniqueMethod() {
//do stuff
}
}
public class Driver() {
public static void main(String[] args) {
Foo test = new Bar();
test.uniqueMethod(); //this causes an error
}
}
So i get an error which says, that the method uniqueMethod()
is undefined for Foo, but when I change the assignment to Bar test = new Bar();
所以我得到一个错误,它说Foo的方法uniqueMethod()
是未定义的,但是当我将赋值更改为Bar test = new Bar();
the problem goes away. 问题消失了。 I dont understand as it should work with Foo test = new Bar();
我不明白,因为它应该与Foo test = new Bar();
Can someone suggest a reason for which why this error occurs? 有人可以提出导致此错误的原因吗?
Thank you in advance. 先感谢您。
Although the method is there at runtime, the compiler only sees that the object test
of type Foo
DOES NOT have the method uniqueMethod. 尽管该方法在运行时存在,但编译器仅看到类型为Foo
的对象test
不具有方法uniqueMethod。 Java works with virtual method, where the method that will be invoked is determined at runtime
. Java使用虚拟方法,其中将在runtime
确定要调用的方法。 So, again, the compiler does not see that this specific instance's type is a Bar
. 因此,再次,编译器看不到该特定实例的类型是Bar
。 If you really need to invoke that method, you could a cast and invoke the method: 如果确实需要调用该方法,则可以强制转换并调用该方法:
((Bar)test).uniqueMethod();
Now, to the compiler, you have the type Bar
, and he can see all the methods that are available at Bar
type 现在,对于编译器,您具有Bar
类型,他可以看到Bar
类型可用的所有方法。
Foo test = new Bar();
So, I have one variable that is of type Foo. 因此,我有一个Foo类型的变量。 I MUST validate that all subsequent calls to members of this class are OK - It will look for all members in Foo
class, the compiler doesnt even look for the instance type. 我必须验证对该类成员的所有后续调用都可以 -它将查找Foo
类中的所有成员,编译器甚至不查找实例类型。
In runtime
, the JVM will know that we have a variable of type Foo
, but with instance type Bar
. 在runtime
,JVM将知道我们有一个类型为Foo
的变量,但实例类型为Bar
。 The only difference here is that it, JVM, will look to the instance to make the virtual calls to the methods. 唯一的区别在于,JVM(JVM)将查找实例以对方法进行虚拟调用 。
Bar test = new Bar();
So, I have one variable that is of type Bar. 因此,我有一个Bar类型的变量。 I MUST validate that all subsequent calls to members of this class are OK - It will look for all members in Foo
and 'Bar' classes, the compiler, again, doesnt even look for the instance type. 我必须验证对该类成员的所有后续调用都可以 -它将查找Foo
和'Bar'类中的所有成员,再次,编译器甚至不查找实例类型。
In runtime
, the JVM will know that we have a variable of type Bar
, and an instance of the same type: Foo
. 在runtime
,JVM将知道我们有一个Bar
类型的变量和一个相同类型的实例: Foo
。 Now we again, have access to all the members defined in Foo
and Bar
classes. 现在我们再次可以访问Foo
和Bar
类中定义的所有成员。
useing late binding you must have the same method in the super class "Fathor" , or it will not work ! 使用后期绑定,您必须在超类“ Fathor”中具有相同的方法,否则将不起作用!
this work with me 与我一起工作
// //
public class foo {
public void print(){
System.out.println("Super,"+2);}
} }
class bo extends foo { bo类扩展foo {
public void print(){
System.out.println("Sup,"+9);
}
} }
Test // 测试//
public abstract class test { 公共抽象类测试{
public static void main(String[] args) {
// TODO Auto-generated method stub
foo t = new bo();
t.print();
}
} /// } ///
output 输出
Sup,9 一口9
This error occurs because the compiler does not know that test
is of type Bar
, it only knows the declared type of Foo
. 发生此错误的原因是,编译器不知道test
的类型是Bar
,而只知道Foo
的声明类型。
public void test(Foo foo) {
// error! this method can accept any variable of type foo, now imagine if you passed it
// another class that extends Foo but didnt have the uniqueMethod. This just wont work
foo.uniqueMethod();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.