Java中的继承问题
我在继承遗产方面遇到了一些问题。在父类中,数组系数是私有的。我有一些访问方法,但仍然无法让它工作Java中的继承问题,java,Java,我在继承遗产方面遇到了一些问题。在父类中,数组系数是私有的。我有一些访问方法,但仍然无法让它工作 import java.util.ArrayList; public class Poly { private float[] coefficients; public static void main (String[] args){ float[] fa = {3, 2, 4}; Poly test = new Poly(fa); } public Poly() {
import java.util.ArrayList;
public class Poly {
private float[] coefficients;
public static void main (String[] args){
float[] fa = {3, 2, 4};
Poly test = new Poly(fa);
}
public Poly() {
coefficients = new float[1];
coefficients[0] = 0;
}
public Poly(int degree) {
coefficients = new float[degree+1];
for (int i = 0; i <= degree; i++)
coefficients[i] = 0;
}
public Poly(float[] a) {
coefficients = new float[a.length];
for (int i = 0; i < a.length; i++)
coefficients[i] = a[i];
}
public int getDegree() {
return coefficients.length-1;
}
public float getCoefficient(int i) {
return coefficients[i];
}
public void setCoefficient(int i, float value) {
coefficients[i] = value;
}
public Poly add(Poly p) {
int n = getDegree();
int m = p.getDegree();
Poly result = new Poly(Poly.max(n, m));
int i;
for (i = 0; i <= Poly.min(n, m); i++)
result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
if (i <= n) {
//we have to copy the remaining coefficients from this object
for ( ; i <= n; i++)
result.setCoefficient(i, coefficients[i]);
} else {
// we have to copy the remaining coefficients from p
for ( ; i <= m; i++)
result.setCoefficient(i, p.getCoefficient(i));
}
return result;
}
public void displayPoly () {
for (int i=0; i < coefficients.length; i++)
System.out.print(" "+coefficients[i]);
System.out.println();
}
private static int max (int n, int m) {
if (n > m)
return n;
return m;
}
private static int min (int n, int m) {
if (n > m)
return m;
return n;
}
public Poly multiplyCon (double c){
int n = getDegree();
Poly results = new Poly(n);
for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient
results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
}
return results;
}
public Poly multiplyPoly (Poly p){
int n = getDegree();
int m = p.getDegree();
Poly result = null;
for (int i = 0; i <= n; i++){
Poly tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
if (result == null){
result = tmpResult;
} else {
result = result.add(tmpResult);
}
}
return result;
}
public void leadingZero() {
int degree = getDegree();
if ( degree == 0 ) return;
if ( coefficients[degree] != 0 ) return;
// find the last highest degree with non-zero cofficient
int highestDegree = degree;
for ( int i = degree; i <= 0; i--) {
if ( coefficients[i] == 0 ) {
highestDegree = i -1;
} else {
// if the value is non-zero
break;
}
}
float[] newCoefficients = new float[highestDegree + 1];
for ( int i=0; i<= highestDegree; i++ ) {
newCoefficients[i] = coefficients[i];
}
coefficients = newCoefficients;
}
public Poly differentiate(){
int n = getDegree();
Poly newResult = new Poly(n);
if (n>0){ //checking if it has a degree
for (int i = 1; i<= n; i++){
newResult.coefficients[i-1]= coefficients[i] * (i); // shift degree by 1 and multiplies
}
return newResult;
} else {
return new Poly(); //empty
}
}
public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly
int oldPolyDegree = this.getDegree();
int newPolyDegree = oldPolyDegree + degree;
Poly newResult = new Poly(newPolyDegree);
//set all coeff to zero
for (int i = 0; i<= newPolyDegree; i++){
newResult.coefficients[i] = 0;
}
//shift by n degree
for (int j = 0; j <= oldPolyDegree; j++){
newResult.coefficients[j+degree] = coefficients[j] * (float)c;
}
return newResult;
}
}
import java.util.ArrayList;
公共级保利{
私有浮动系数;
公共静态void main(字符串[]args){
float[]fa={3,2,4};
多边形测试=新多边形(fa);
}
公共保利(){
系数=新浮点数[1];
系数[0]=0;
}
公共保利(国际学位){
系数=新浮点数[度数+1];
对于(int i=0;i我将使系数受保护
或使用访问器方法
我不会抛出一个简单的checked异常
。一个IllegalArgumentException将是一个更好的选择
什么是quadCourtificates
?它们似乎不在任何地方设置。您将系数放置在private
中。我不会更改此设置,但我会在Poly
类中添加一个getter方法:
public class Poly {
//somecode here
public float[] getCoefficients(){
return this.coefficients;
}
}
然后我会在其他代码中通过getter方法使用它
public QuadPoly(Poly p){
super(p.getCoefficients);
//some more code here
}
即使您将“系数”设置为“受保护”
,您也在尝试访问另一个对象的“系数”字段,该字段是一个参数。因此,它与继承和问题无关。上述代码的错误是什么?好吧,现在,当我尝试“超级(p.系数)”时,父类中的数组系数是私有的。你能给我一些提示,让第二个类中的加法和乘法方法发挥作用吗?我有点不安,在同一个24小时内看到同一个作业中的4篇文章都在同一个作业上。看,.oops。这不应该在那里。我要去cr只为这个类创建第二个数组,但这样做会破坏继承的目的。有没有一种访问器方法可以让我访问整个数组?我遇到的问题是我不确定如何让add and multiply方法工作。我应该尽可能多地使用父类的方法。我明白了,你应该如果你这样做,他们可以简单地改变。我会强烈考虑发送一个防御性拷贝或一个不可修改的集合。给他们一个私有可变变量的原始访问是一个非常糟糕的决定。
public QuadPoly(Poly p){
super(p.getCoefficients);
//some more code here
}