[英]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.