简体   繁体   English

如何从Java中的超类方法获取子类值?

[英]How can I get subclass value from super class method in Java?

I have these two classes: 我有这两节课:

Class A 
{
  public int max = 100;

  public int getMax()
  {
     return max;
  }
}



Class B extends A
{
  public int max = 200;
}

I write the following code: 我编写以下代码:

A clsA = new A();
B clsB = new B();


valA = clsA.getMax();
valB = clsB.getMax();

valA is 100 valB is 100 again valA是100 valB又是100

How can I write code that clsB.getMax(); 我怎么写这样的代码clsB.getMax(); returns 200? 返回200?

You can override getMax in class B: 您可以在B类中覆盖getMax:

  @Override
  public int getMax()
  {
     return max;
  }

Though, in general, it's a bad idea to have variables with the same name in the super class and the sub-class. 虽然,总的来说,在超类和子类中具有相同名称的变量是一个坏主意。

Note that after the change, the following will also return 200 : 请注意,更改后,以下内容也会返回200:

A clsB = new B();
valB = clsB.getMax();

Override the getMax method in B : 覆盖BgetMax方法:

class B extends A
{
    public int max = 200;

    public int getMax()
    {
       return max;
    }
}

In the getter method, return max; 在getter方法中, return max; is equivalent to return this.max where this references the current instance of B . 等价于return this.max ,其中this引用了B的当前实例。 On the other hand, calling return super.max returns the max field value referenced in the parent subclass. 另一方面,调用return super.max将返回父子类中引用的max字段值。

the short answer is that you can't override members in subclasses like you are trying. 简短的答案是,您不能像尝试的那样覆盖子类中的成员。 What you instead should do is make B change the value of max directly. 相反,您应该做的是使B直接更改max的值。 Something like this: 像这样:

class B extends A
{
    public B() 
    {
        max = 200;
    }
}

You have to add method getMax() to class B . 您必须将方法getMax()添加到类B The implementation might be the same as in Class A 实现方式可能与A类相同

@Overwrite
public int getMax()
{
   return max;
}

When you DO NOT overwrite the method one from B calls parent's method getMax() . 当您不覆盖方法时, B调用父方法的方法getMax() And parent metod getMax() returns 150 父方法getMax()返回150

Try Method Overriding 尝试方法覆盖

In Java, the "closest method" will be called. 在Java中,将调用“最接近的方法”。 Because clsB extends A, but doesn't have it's own definition of getMax(), A's version of getMax() will be called. 由于clsB扩展了A,但没有它自己的getMax()定义,因此将调用A的getMax()版本。 However, because clsB also shares the variables of A, getMax (called from class A) will return class A's number. 但是,由于clsB也共享A的变量,因此getMax(从A类调用)将返回A类的编号。

The solution? 解决方案? add

public int getMax()
{
     return max;
}

to class B. 到B级。

Implement Method override concept 实施方法覆盖概念

 @Override
  public int getMax()
  {
     return max;
  }

Overriding happens at run time 覆盖在运行时发生

A clsB = new B();
valB = clsB.getMax();

Hope this should work. 希望这应该工作。

Java does not support override any variables in a class. Java不支持覆盖类中的任何变量。

You can have two ways to do it. 您可以通过两种方法来实现。

  1. Pass the variable through a constructor . 通过构造函数传递变量 I suggest doing this way because, if the method do the same action. 我建议这样做,因为如果该方法执行相同的操作。 By overriding it and doing it again is a bad practice. 覆盖它并再次执行它是一个不好的做法。

    Class A { public int max = 100; public A(int maxVal) { this.max=maxVal; } public int getMax(){ return max; } } B clsB = new B(); A clsA = new A(clsB.max);

  2. Override the method 覆盖方法

    @override public int getMax(){ max=200; //do the job. }

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

相关问题 我可以在不重写的情况下使用子类中超类的方法吗? - Can I use a method from a super class in a subclass without overriding it? Java:如何使用超类的静态方法从其创建子类的实例 - Java: How to Create an instance of a subclass from with a Static method of the super class Java:从子类获取超类的实例 - Java: Get instance of super class from subclass 超级 class 方法的 Inheritance 方法无法获取子类中的详细信息? - Inheritance method of super class method can't get the details in subclass? Java:从子类调用超类的受保护方法-不可见? - Java: calling a super class' protected method from a subclass - not visible? Java多态性如何调用子类对象的超类方法 - Java Polymorphism How to call to super class method for subclass object Java-您可以从父类访问在子类中声明的枚举吗? - Java - Can you access an Enum declared in Subclass from Super class? 我如何在超类方法中使用子类的静态字段 - How do I use static field of subclass in super class method 如何从 super 的关联 class 调用子类方法 - How to call a subclass method from super's associated class 使用子类中的“super”和超类中的“this”选择Java运行时方法 - Java runtime method selection using “super” in the subclass and “this” in the super class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM