简体   繁体   English

使用super()或对象访问父类中的变量?

[英]Access variable in parent class using super(), or an object?

If you need any info or have a link to a solved answer feel free to post it and I will adjust/delete my question. 如果您需要任何信息或链接到已解决的答案,请随时发布它,我将调整/删除我的问题。

So for an assignment in comp sci I need to access variables in a parent class. 因此,对于comp sci中的赋值,我需要访问父类中的变量。 I have something like 我有类似的东西

//import anything?


public class HW6Fraction extends Fraction{
  public HW6Fraction(int num, int denom){
    super();
  }

  public Fraction add(Fraction f) { //Add method
    int num = super().getNumerator + f.getNumerator();
    int denom = super().getDenominator + f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction subtract(Fraction f) { //Subtract method
    int num = super().getNumerator - f.getNumerator();
    int denom = super().getDenominator - f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }
}

as the subclass and something like this as the parent class: 作为子类,类似这样的作为父类:

//Assignment 2 - problem 3, p. 125
/*
  Add, subtract, multiply, and divide methods to the Fraction class
  (section 3.8).  Also, add two more constructors.  One of the constructors
  will have no parameters; it initializes the fraction to 0/1.  The other
  constructor will have one parameter, representing the numerator of the
  fraction; the denominator of the fraction will be 1.  Write a TestFraction
  class (similar to the TestAccount class of Section 3.6) that creates two
  Fraction objects and tests the behavior of all constructors and instance
  methods in the Fraction class.

    Logic:
    Addition - ((numerator of fraction 1 * denominator of fraction 2) +
                (numerator of fraction 2 * denominator of fraction 1)) /
                (denominator of fraction 1 * denominator of fraction 2)

    Subtraction - ((numerator of fraction 1 * denominator of fraction 2) -
                  (numerator of fraction 2 * denominator of fraction 1)) /
                  (denominator of fraction 1 * denominator of fraction 2)

    Multiplication - (numerator of fraction 1 * numerator of fraction 2) /
                     (denominator of fraction 1 * denominator of fraction 2)

    Division - (numerator of fraction 1 * denominator of fraction 2) /
               (denominator of fraction 2 * numerator of fraction 1)
*/


public class Fraction {
  private int numerator; //Numerator of fraction
  private int denominator; //Denominator of fraction

  public Fraction(int num, int denom) { //Constructor
    numerator = num;
    denominator = denom;
  }

  public Fraction() { //Constructor w/ no parameters
    new Fraction(0, 1);
  }

  public Fraction(int num) { //Constructor w/ numerator parameter
    numerator = num;
    int denom = 1;
  }

  public int getNumerator() { //getNumerator method
    return numerator;
  }

  public int getDenominator() { //getDenominator method
    return denominator;
  }

  public void setNumerator(int num) { //setNumerator method
    numerator = num;
  }

  public void setDenominator(int denom) { //setDenominator method
    denominator = denom;
  }

  public Fraction add(Fraction f) { //Add method
    int num = numerator * f.getDenominator() + f.getNumerator() *
              denominator;
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction subtract(Fraction f) { //Subtract method
    int num = numerator * f.getDenominator() - f.getNumerator() *
              denominator;
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction multiply(Fraction f) { //Multiply method
    int num = numerator * f.getNumerator();
    int denom = denominator * f.getDenominator();
    Fraction result = new Fraction(num, denom);
    return result;
  }

  public Fraction divide(Fraction f) { //Divide method
    int num = numerator * f.getDenominator();
    int denom = denominator * f.getNumerator();
    Fraction result = new Fraction(num, denom);
    return result;
  }
}

These lines are where I am having a problem: 这些行是我遇到问题的地方:

int num = super().getNumerator + f.getNumerator();

I wonder if there is proper terminology for this? 我想知道是否有适当的术语?

Your HW6Fraction class inherits the method from its parent class 您的HW6Fraction类从其父类继承该方法

So you can just write: 所以你可以这样写:

int num = getNumerator() + f.getNumerator();

If your HW6Fraction also declares a getNumerator() method, and you want to rather call the getNumerator() defined in the parent class you do: 如果您的HW6Fraction也声明了getNumerator()方法,而您想调用父类中定义的getNumerator() ,则可以:

int num = super.getNumerator() + f.getNumerator();

getNumerator is a public function, so anything that has a reference to your class should be able to call it, including all child classes. getNumerator是一个公共函数,因此任何引用您的类的东西都应该能够调用它,包括所有子类。

When you inherit from a class you inherit all of its public and protected members, so they should all be part of the child class that you're working with. 当您从类继承时,您将继承其所有公共成员和受保护成员,因此它们都应属于您正在使用的子类的一部分。 Technically you don't need to put anything special in front just calling getNumerator() should work fine. 从技术上讲,您只需要调用getNumerator()就可以了,不需要在前面放任何特别的内容。

Alternatively, this.getNumerator() will explicitly state that you want the getNumerator function associated with the object that is currently executing. 或者, this.getNumerator()将明确声明您希望getNumerator函数与当前正在执行的对象相关联。

暂无
暂无

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

相关问题 如何在不使用 super() 的情况下访问父 class 的重写方法? - How to access overridden method of parent class without using super()? 如何使用 lombok SuperBuilder 访问超级 class 的私有变量 - How to access private variable of super class by using lombok SuperBuilder 不使用 super 访问超类属性 - Access super class property without using super 如何使用子类引用使用父关键字调用父类方法以创建对象? - How to call a parent class method using child class reference using super keyword for object creation? 在 Java 中,是否可以使用超级关键字从子 ZA2F2ED4F8EBC2CBB4C21A2 引用父 class 的 static 变量? - In Java , is it possible to refer static variable of parent class from child class using super keyword? 当对象被超类引用时,有没有办法访问子对象的变量? - Is there any way to access child object's variable when the object is referred by its super class? 无法从超级类中“访问”变量 - Can't “access” variable from super,super class 子类对象如何访问父类的私有变量,就像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? 使用泛型将super()调用到父类 - Calling super() to parent class using generics Java:使用父类方法访问子类变量 - Java : Using parent class method to access child class variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM