超类引用子类对象,显示与子类对象的子类引用相同的行为 [英] Superclass reference to subclass object showing same behaviour as subclass reference to subclass object

查看:113
本文介绍了超类引用子类对象,显示与子类对象的子类引用相同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java中的以下代码,当在elipse上运行时,即使我们替换也会提供相同的输出

The following code in java, when run on elipse, gives same output even if we replace

superclass s=new sub();

with,

sub s= new sub();

请注意,我们已经覆盖了方法。

Note that we have overridden methods.

输出为:

changed supermethod in sub class
num is sub class 5

代码:

public class superclass {
    int num=2;
    public static void main(String str[]){
        superclass s=new sub();
        //HERE: nothing changes if we write, sub s=new sub();
        s.supermethod();
        s.method();
    }
    void supermethod(){
        System.out.println("supermethod as in superclass");
    }
    void method(){
        System.out.println("num in superclass "+num);
    }
}
class sub extends superclass{
    int num=5;
    void method(){
        System.out.println("num is sub class "+num);
    }
    void supermethod(){
        System.out.println("changed supermethod in sub class");
    }
}

请指出,创建一个有什么区别这两种方式的子类对象。访问方法和变量会有什么不同吗?
(我们的java老师说,访问方法和变量在两种情况下都会有所不同)

Please point out, what are the differences in creating a sub class object in these two ways. And will there be any difference in accessing methods and variables? (our java teacher says, accessing method and variables will be different in both cases)

此外,静态方法会发生什么,例如main。我知道它是可继承的,但有人可以突出它在子类中的行为吗?

Also, what happens to the static methods, like main. Tough i know it is inheritable, but can someone highlight its behavior in sub classes?

推荐答案

在Java中,所有非静态方法是虚拟,这意味着它们基于底层对象的运行时类型,而不是指向该对象的引用类型。因此,在对象的声明中使用哪种类型无关紧要,行为将是相同的。

In Java, all non-static methods are "virtual", meaning that they are based on the runtime type of the underlying object rather than the type of the reference that points to that object. Therefore, it doesn't matter which type you use in the declaration of the object, the behavior will be the same.

声明的作用是什么,是方法在编译时可见。如果 SubClass 有一个 SuperClass 的方法没有(我们称之为 subMethod()),你将对象构造为

What the declaration does affect, is the methods that are visible at compile-time. If SubClass has a method that SuperClass does not (let's call it subMethod()), and you construct your object as

SuperClass s = new SubClass();

然后你只能调用上可用的方法父类。也就是说,尝试调用 s.subMethod()将给出编译时错误。但是,正如您所发现的,如果 SuperClass 中存在方法,但是被 SubClass 覆盖,则它将是已执行的重写方法。

Then you will only be able to call methods on it that are available on SuperClass. That is, attempting to call s.subMethod() will give you a compile time error. But, as you have discovered, if there methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.

另一方面,静态方法不是虚拟方法。运行下面的代码

Static methods, on the other hand, are not virtual. Running the code below

public class StaticTest {
    public static void main(String[] args) {
        SuperClass s = new SubClass();
        s.method();  // bad idea - calling static method via an object reference
    }

    public static class SuperClass {
        public static void method() {
            System.out.println("SuperMethod");
        }
    }

    public static class SubClass extends SuperClass {
        public static void method() {
            System.out.println("SubMethod");
        }
    }
}

打印出SuperMethod。但是,您应该很少关心 static 方法是非虚拟的,因为您不应该像上面那样通过对象引用来调用它们。你应该通过类名称来调用它们:

prints out "SuperMethod". You should rarely care, however, that static methods are non-virtual because you should never call them via an object reference as I have done above. You should call them via the class name:

SuperClass.method();

这篇关于超类引用子类对象,显示与子类对象的子类引用相同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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