简体   繁体   English

在Java中扩展类时重载方法

[英]Overloading of methods when extending classes in Java

If I have this code: 如果我有这个代码:

interface Tre{
    public void f();
}
public class TreA implements Tre{
    int x = 1;
    public void f(){
        x++; 
        System.out.println("Y"+x);
    }
    public static void main(String[] a){
        Tre a3 = new TreB(); a3.f();
    }
}
class TreB extends TreA{
    public void f(){
        x--;         
        super.f();
        System.out.println("X"+x);}
}

I do not understand why the output is Y1 X1 . 我不明白为什么输出是Y1 X1 If I extend the class TreA , I'll overwrite its f() method and get a copy of int x=1 . 如果我扩展了TreA类,我将覆盖它的f()方法并获得int x=1的副本。 Calling super.f() should call f() belonging to the class TreA but what about x ? 调用super.f()应该调用属于TreA类的f()但是x呢? I have never instantiated TreA . 我从未实例化过TreA Why does it use the x belonging to TreB ? 为什么它使用属于TreBx What about the scope in this case? 在这种情况下,范围怎么样?

Extending a class means that your child class include everything from the parent class. 扩展类意味着您的子类包含来自父类的所有内容。

I'll overwrite it's f() method and get a copy of int x=1 我将覆盖它的f()方法并获得int x = 1的副本

You don't get a copy, you simply have it (you inherited it) and can use it. 你没有得到一份副本,你只是拥有它(你继承了它)并且可以使用它。

Doesn't matter where you use x , it is always the same variable inherited from TreA , so when TreA.f() modify it, it is modifying the only instance of x you have. 无论你在哪里使用x ,它总是从TreA继承的相同变量,所以当TreA.f()修改它时,它正在修改你拥有的唯一x实例。

I have never instantiated TreA 我从未实例化过TreA

Creating a child class means you are also creating the parent class. 创建子类意味着您也在创建父类。 Its constructor gets automatically called when you create the child class. 在创建子类时,会自动调用其构造函数。

You have to think like that TreB inherently contains TreA and then you can add things to it and make overrides. 您必须认为TreB固有地包含 TreA ,然后您可以添加内容并进行覆盖。

To better visualize the situation, this is your class A: 为了更好地可视化情况,这是你的A类:

父母班

and this is its child class B: 这是它的子类B:

儿童班

as you can see the x variable in B is the variable from class A, not a copy. 正如您所看到的,B中的x变量 A类中的变量,而不是副本。

When you instantiate TreB, a TreA is constructed (more specifically this is a TreB). 当您实例化TreB时,会构建TreA(更具体地说,这是TreB)。 TreB has no field x hiding the field from TreA, so this object has only one field x, defined in TreA. TreB没有字段x将字段隐藏在TreA中,因此该对象只有一个字段x,在TreA中定义。 You can access this from TreB because you did not declare it private. 您可以从TreB访问此内容,因为您没有将其声明为私有。

This is bad practice btw, non-final fields should always be private to avoid problems like this. 这是不好的做法btw,非final字段应该始终是私有的,以避免这样的问题。 Use a getter to get the value from TreA for use in TreB. 使用getter从TreA获取值以用于TreB。

whenever you called the class from anywhere the variable is instantiated it self. 无论何时从任何地方调用该类,变量都会自我实例化。 The default constructor is called and set the values of variables. 调用默认构造函数并设置变量的值。

You can see this example. 你可以看到这个例子。 The variable value is set when you called that class. 调用该类时设置变量值。

public class TreA {
    int x = 1;
    public static void main(String[] a){
        TreA b = new TreA();
       System.out.println(b.x);
    }
}

output is 1 输出为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM