各位大神好,我嘗試用繼承和泛型的方式做一個等比數列,但是,綠色線旁框住的代碼卻一直顯示和Long不符,我有改過 protected long nextValue()方法中的long改成Long,但是這樣會變成型態不符,邏輯上也有些問題,有改和不改都會出問題,想請問大神們,到底是哪裡出錯了?需要您幫忙提點QAQ謝謝
import java.util.*;
import java.lang.*;
public class ProgressionGeneric <k> {
protected k a;
protected k b;
ProgressionGeneric(){a=b;};
protected k firstValue (){b=a;
return b;};
protected k nextValue(){return b;};
protected void printProgression(int n){
k cc=nextValue();
System.out.print(cc+" ");
for(int i=1;i<n;i++){
k c=nextValue();
System.out.print(c+" ");
}
};
public class GeomProgression extends ProgressionGeneric<Long>{
protected long r;
GeomProgression(){ //first =1, r =1 by default
this(1,1); //first = 1; r = 1;
}
GeomProgression(long a, long base) { //Set r to base
a = a;
r = base;
}
protected long nextValue(){ long b=a; b *= r; //cur = cur*r; return b; }
}
public class Main {
public static void main(String[] args) {
GeomProgression gogo =new GeomProgression(1,4);
gogo.printProgression(3);
}
精慕HU
侃侃无极
随时随地看视频慕课网APP
相关分类