实例方法和变量的情况下java继承解析 [英] java inheritance resolution in case of instance methods and variables

查看:126
本文介绍了实例方法和变量的情况下java继承解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


根据java,实例方法解析基于参数的运行时类型。



但是在解析实例变量时它使用不同的方法如下所示。


节目输出为..

 儿童
父母
ParentNonStatic




这里第一个输出基于参数的运行时类型,但第三个输出不是。



任何人都可以解释一下吗?




  public class Child extends Parent {

public static String foo =Child;
public String hoo =ChildNonStatic;

私人子女(){
super(1);
}

@Override
String please(){
returnChild;
}

public static void main(String [] args){
Parent p = new Parent();
孩子c =新孩子();

//解析方法
System.out.println(((Parent)c).please());

//解析静态变量
System.out.println(((Parent)c).foo);

//解析实例变量
System.out.println(((Parent)c).hoo);
}
}
class Parent {

public static String foo =Parent;
public String hoo =ParentNonStatic;

public Parent(int a){
}

public Parent(){
}

String please() {
返回树;
}
}


解决方案

In你实际上没有施放的Java。当你要求转换时,Java实际上不会任何东西,除了检查对象是否可以转换为该类型,因此转换为什么会抛出ClassCastException。



编译器虽然理解了强制转换,因此使用它们来验证方法调用是否合适。



关于静态字段,编译器实际上删除了任何实例变量,并根据返回类型通过类引用该字段。

 父p = new Parent(); 
孩子c =新孩子();
Parent pc = new Child();

System.out.println(c.foo); //将打印Child
System.out.println(p.foo); //将打印Parent
System.out.println(pc.foo); //将打印Parent
System.out.println(((Child)pc).foo)//将打印Child

字段似乎也以同样的方式工作。



我想简而言之,java对方法和字段上的静态绑定进行动态绑定。 / p>

As per java, instance method resolution is based on runtime types of the arguments.

But while resolving instance variable it uses different approach as shown below.

Output of program is ..

Child
Parent
ParentNonStatic

Here First output is based on runtime types of the argument but third output is not.

can any one explain about this ?

  public class Child extends Parent {

        public static String foo = "Child";
        public String hoo = "ChildNonStatic";

        private Child() {
            super(1);
        }

        @Override
        String please() {
            return "Child";
        }

        public static void main(String[] args) {
            Parent p = new Parent();
            Child c = new Child();

           //Resolving method
            System.out.println(((Parent) c).please());

           //Resolving Static Variable
            System.out.println(((Parent) c).foo);

           //Resolving Instance Variable
            System.out.println(((Parent) c).hoo);
        }
    }
class Parent {

    public static String foo = "Parent";
    public String hoo = "ParentNonStatic";

    public Parent(int a) {
    }

    public Parent() {
    }

    String please() {
        return "Tree";
    }
}

解决方案

In Java you don't actually "cast". Java doesn't actually do anything when you ask for a cast except to check if the object can be cast to that type thus why casts can throw a "ClassCastException".

The compiler though understands casts and thus uses them to validate that method calls are appropriate.

With respect to static fields, the compiler in fact removes any instance variables and references the field via the class according whatever the return type is.

Parent p = new Parent();
Child c = new Child(); 
Parent pc = new Child();

System.out.println(c.foo); // will print Child
System.out.println(p.foo); // will print Parent
System.out.println(pc.foo); // will print Parent
System.out.println(((Child)pc).foo) // will print Child

Fields seem to work the same way.

I think in a nutshell that java does dynamic binding on methods and static binding on fields.

这篇关于实例方法和变量的情况下java继承解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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