![](/img/trans.png)
[英]How to access overridden method of parent class without using super()?
[英]Access variable in parent class using super(), or an object?
如果您需要任何信息或链接到已解决的答案,请随时发布它,我将调整/删除我的问题。
因此,对于comp sci中的赋值,我需要访问父类中的变量。 我有类似的东西
//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;
}
}
作为子类,类似这样的作为父类:
//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;
}
}
这些行是我遇到问题的地方:
int num = super().getNumerator + f.getNumerator();
我想知道是否有适当的术语?
您的HW6Fraction类从其父类继承该方法
所以你可以这样写:
int num = getNumerator() + f.getNumerator();
如果您的HW6Fraction也声明了getNumerator()
方法,而您想调用父类中定义的getNumerator()
,则可以:
int num = super.getNumerator() + f.getNumerator();
getNumerator
是一个公共函数,因此任何引用您的类的东西都应该能够调用它,包括所有子类。
当您从类继承时,您将继承其所有公共成员和受保护成员,因此它们都应属于您正在使用的子类的一部分。 从技术上讲,您只需要调用getNumerator()
就可以了,不需要在前面放任何特别的内容。
或者, this.getNumerator()
将明确声明您希望getNumerator
函数与当前正在执行的对象相关联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.