简体   繁体   English

如何从Java中的超类访问私有值?

[英]how to access a private values from the super class in Java?

I want to access two private variables from the superclass MathProblem , but I don't know how to access them. 我想从超类MathProblem访问两个私有变量,但是我不知道如何访问它们。 I know that the private things are not inherited, but I need those two variable to access them and add them and return the value. 我知道私有事物不会被继承,但是我需要那两个变量来访问它们并添加它们并返回值。

abstract public class MathProblem {
    private int operand1;
    private int operand2;
    private int userAnswer;

    public MathProblem() {
        operand1 = RandomUtil.nextInt(99);
        operand2 = RandomUtil.nextInt(99);
    }

    public int getOperand1() {
        return operand1;
    }

    public void setOperand1(int operand1) {
        this.operand1 = operand1;
    }

    public int getOperand2() {
        return operand2;
    }

    public void setOperand2(int operand2) {
        this.operand2 = operand2;
    }

    public int getUserAnswer() {
        return userAnswer;
    }

    public void setUserAnswer(int userAnswer) {
        this.userAnswer = userAnswer;
    }
}

And the sub class is like this: 子类是这样的:

public class AdditionMathProblem extends MathProblem {
    StringBuilder sb = new StringBuilder();

    public AdditionMathProblem() {
        super();
        // can't access the private values.
    }

    @Override
    public int getAnswer() {    
        int result = getOperand1() + getOperand2();
        return result;
    }
}   

So in class AdditionMathProblem , I used the getOperand1() + getOperand2() to add the two values, but I need those two values letter to access them. 因此,在类AdditionMathProblem ,我使用了getOperand1() + getOperand2()来添加两个值,但是我需要使用这两个值来访问它们。 So my question is: Is there any way I can access them? 所以我的问题是: 有什么办法可以访问它们?

Use the protected access modifier instead of private . 使用protected访问修饰符,而不是private This will allow subclasses to access the members of their base class. 这将允许子类访问其基类的成员。

You cant access private variables directly from outside its class. 您不能直接从其类外部访问私有变量。 If you would like that variable to be accessible by its parent then you can: 如果希望该变量可由其父级访问,则可以:

  1. Change modifier from private to protected. 将修饰符从私有更改为受保护。 Or 要么
  2. If you want the variable to remain private then you can create a publec method that returns that variable. 如果希望变量保持私有状态,则可以创建一个返回该变量的publec方法。 Then from the child class just call that method. 然后从子类中调用该方法。

暂无
暂无

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

相关问题 如何通过Java中的反射访问超级class的超级class的私有字段? - How to access a private field of the super class of the super class with reflection in Java? 有没有办法从Java的派生类访问嵌套在超类中的私有内部类? - Is there any way to access a private inner class that is nested in a super class from a derived class in Java? 子类对象如何访问父类的私有变量,就像java中一样,除了类本身可以访问私有变量之外,否? - How does the subclass object access the private variable of super class,as in java no except the class itself can access the private variable? Java:如何从超类访问子类的方法的实现 - Java: How to access implementation of a method of Sub class from Super class 访问Java中的super()类的私有变量--JChart2D - Access a private variable of the super() class in Java - JChart2D 访问超类的私有字段 - Access to private field of a super class 如何从子类访问Java中超类的受保护字段? - How to access protected field of a super class in java from subclasses? 使用Java从外部访问超类 - Access Super Class from Outside in Java 在Java中,为什么超类方法不能从子类实例访问受保护的或私有的方法/变量? - In Java, why can't a super-class method access protected or private methods/variables from a sub-class instance? 如何使用 lombok SuperBuilder 访问超级 class 的私有变量 - How to access private variable of super class by using lombok SuperBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM