简体   繁体   English

使用super作为引用类型时,无法访问子类中的方法

[英]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类型可用的所有方法。

Think like the compiler and JVM 像编译器和JVM一样思考

Example 1 例子1

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)将查找实例以对方法进行虚拟调用

Example 2 例子2

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. 现在我们再次可以访问FooBar类中定义的所有成员。

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.

相关问题 当引用类型为超类时,子类无法访问超类的受保护方法 - Sub-class not able to access the protected method of super-class when the reference is of type super-class 在可以确保类型与子 class 相同的情况下,如何在不强制转换的情况下访问超级 class 字段? - How to access super class field without cast when it can be ensured that the type is the same as the sub class? 在Java中,为什么超类方法不能从子类实例访问受保护的或私有的方法/变量? - In Java, why can't a super-class method access protected or private methods/variables from a sub-class instance? 是否可以创建一个“超级”变量,然后将其更改为子 class 类型并能够访问子类方法 - Is it possible to Create a 'super' variable and then change it to a sub class type and be able to access the subclass methods 为什么引用子类对象的父类类型引用变量无法访问子类的方法 - Why parent class type reference variable having reference to child class object can't access child class's Methods 为什么我们不能使用父类的引用变量来访问其子类的方法(方法在父类中不可用)? - why cant we use a reference variable of super class to access methods of its subclass(methods not available in super class)? 在Java中,无法从子类访问父类的受保护成员 - In Java , Can't access protected members of super-class from sub-class 无法使用Composition访问类方法? - Can't Access class methods using Composition? 您可以通过 Java 中的超级 class 访问子 class 变量吗 - Can you access sub class variable via super class in Java 使用超类引用调用重载的继承方法 - Calling overloaded inherited methods using super class reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM