Java继承覆盖实例变量 [英] Java inheritance overriding instance variable

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

问题描述

我正在学习java。我对遗产有疑问。当子类扩展父类时,父类有一个引用父类中声明的实例变量的方法。但是,子类dint会覆盖此方法,并声明了与父类同名的实例变量。在这种情况下,将引用来自child的实例变量或将引用parent。以下是代码段

  class parent {
int a;
parent(){
System.out.println(在父母中);
a = 10;
}
void method(){
System.out.println(a);
}
}
class child extends parent {
int a;
child(){
System.out.println(in child);
a = 11;
}
}

公共类测试{
public static void main(String args [])抛出IOException {
parent p1 = new child() ;
p1.method();
}
}

我得到的输出是

$ b父母的
$ b

为子女的
b

可以有人请让我理解为什么它的引用父类的实例变量 a 而不是子类的 a



另一个疑问是,我理解隐藏方法,当父类中有静态方法时,子类也声明了具有相同签名的静态方法。这里隐藏的意思?什么方法被隐藏了?如果它的父方法可以请你解释一下吗?

提前致谢。

解决方案


  1. Java实例变量不能在子类中重写。 Java继承不起作用。


  2. 在您的示例中,没有隐藏(或覆盖或重载)的方法。


  3. 虽然隐藏了实例变量。在类中,声明 a 隐藏 a 中,以及中对 a 的所有引用c> class引用 child.a 而不是 parent.a


为了更清楚地说明这一点,请尝试运行:

  public static void main(String args [])抛出IOException {
child c1 = new child();
父p1 = c1;

System.out.println(p1.a is+ p1.a);
System.out.println(c1.a is+ c1.a);
System.out.println(p1 == c1 is+(p1 == c1));
}

它应输出:

  p1.a是10 
c1.a是11
p1 == c1是真

这表明有一个对象有两个不同的字段,叫做 a ...你可以抓住两个值,如果访问允许它。






最后,您应该学会遵循标准的Java标识符约定。类名应始终以大写字母开头。


I am learning java. I have a doubt in inheritance. When a child class extends parent class and parent class has a method which refers to a instance variable declared in parent. But the child class dint override this method and has declared instance variable with same name as the parent. In this case instance variable from child will be referred or parent will be referred. Below is the code snippet

class parent {
    int a;
    parent() {
        System.out.println("in parent");
        a = 10;
    }
   void method() {
        System.out.println(a);
    }
}
class child extends parent {
    int a;
    child() {
        System.out.println("in child");
        a = 11;
    }
}

public class Test {
    public static void main(String args[]) throws IOException {
        parent p1 = new child();
        p1.method();
    }
}

The output I get is

in parent
in child
10

Can someone please make me understand why its referring parent class's instance variable a and not child class's a.

Another doubt is, I understood hiding the method, when there is a static method in parent and child class also has declared a static method with same signature. Here hiding means ? what method is getting hidden ? If its the parent method can you please explain me ?
Thanks in advance.

解决方案

  1. Java instance variables cannot be overridden in a subclass. Java inheritance doesn't work that way.

  2. In your example, there is no method hiding (or overriding or overloading) going on.

  3. There is hiding of instance variables though. In class child, the declaration of a hides the declaration of a in parent, and all references to a in the child class refer to the child.a not the parent.a.

To illustrate this more plainly, try running this:

public static void main(String args[]) throws IOException {
    child c1 = new child();
    parent p1 = c1;

    System.out.println("p1.a is " + p1.a);
    System.out.println("c1.a is " + c1.a);
    System.out.println("p1 == c1 is " + (p1 == c1));
}

It should output:

    p1.a is 10
    c1.a is 11
    p1 == c1 is true

This demonstrates that there is one object with two distinct fields called a ... and you can get hold of both of their values, if the access permits it.


Finally, you should learn to follow the standard Java identifier conventions. A class name should ALWAYS start with a capital letter.

这篇关于Java继承覆盖实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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