Java中的继承和转换 [英] Inheritance and casting in Java

查看:36
本文介绍了Java中的继承和转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的继承和转换有疑问.我有以下两个示例类和一个测试类,并在这些类之后陈述我的问题:

I have a question about inheritance and casting in Java. I have the following two example classes and a test class, and I state my question after the classes:

public class Automobile {
    public int var;

    public Automobile () {
        var = 1;
    }

    public String toString () {
        return "AUTOMOBILE: " + var;
    }
}


public class Porsche extends Automobile {
    public int var;

    public Porsche () {
        var = 2;
    }

    public String toString () {
        return "PORSCHE: " + var;
    }
}

public class Test {
    public static void main (String [] args) {
        Porsche p = new Porsche();
        Automobile a = new Automobile();

        System.out.println ("(Automobile) p = " + (Automobile)p);

        System.out.println ("(Automobile) p.var = " + ((Automobile)p).var); 
    }
}

输出为:

(Automobile) p = PORSCHE: 2
(Automobile) p.var = 1

我不明白为什么在第二个语句中我们有1.不应该是2吗?因为在第一条语句中将p转换为Automobile后,我仍然得到 PORSCHE:2 作为 p 的表示形式-我可以通过以下方式理解这一点:

I don't understand why in the second statement we have 1. Shouldn't it be 2? Because after I cast p to Automobile in the first statement I still get PORSCHE: 2 as the representation of p - I understand this in the following way:

尽管如此,我还是将 p 投放到汽车上 p 保持其原始性质"- p 是保时捷类的对象,但是因为保时捷扩展汽车,我们可以说p也是汽车.因此,当我们将其显式转换为Automobile时,它将继续使用其方法-在我们的案例是保时捷中定义的方法 toString().

nevertheless I have casted p to Automobile p keeps its "original nature" - p is an object of class Porsche, but because Porsche extends Automobile, we could say that p is also an Automobile. And therefore when we cast it explicitly to Automobile, it continues using its methods - in our case the method toString() defined in Porsche.

另一方面,如果我写的是正确的,则第二个打印语句应给出2而不是1.

On the other hand, if what I write is correct, then the second print statement should give 2 and not 1.

现在,这对我来说似乎是一个矛盾,但是由于这在Java中有效,因此我似乎不了解在转换和继承过程中发生了什么.

Now this seems as a contradiction to me, but since this works in Java, it seems that I don't understand what's going on during the casting and the inheritance process.

推荐答案

我相信您实际上无法覆盖Java中的变量.子类实际上隐藏"了变量 var .

I believe you can't actually override variables in Java. The subclass actually 'hides' the variable var.

当您强制转换为 Automobile 时,您将获得 var 变量的超类版本.但是, toString()方法仍在查看 var 变量的实例版本.

When you cast to Automobile, you're getting the superclass' version of the var variable. However, the toString() method is still looking at the instance's version of the var variable.

如果您从Porsche子类中删除了 public int var ,它应该可以正常工作.

If you removed the public int var from the Porsche subclass it should work as expected.

请注意,使用公共实例变量不是一种好习惯,您应该始终创建具有特定封装.

It should be noted that it's not good practice to use public instance variables, you should always create getters / setters to have proper encapsulation.

这篇关于Java中的继承和转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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