[英]Creating and array of objects in java
嗨,大家好,我想知道您是否可以為我當前的項目提供一些思路和想法。 我希望創建術語和術語數組,其中術語包含兩個或三個值/變量。
讓我嘗試盡我所能來解決這個問題。
class Term(double e,double c){
coeff=c;
exp=e;
}
Polynomial poly1[]=[degree]//where degree is some make value of polynomial
poly1[0]= new Term(1,2);
我也想知道以后是否可以在poly1中調用Term的值:
poly1[0].getExp
//where this would give me the exp value of
//Term in the first entry in the poly1 array
對不起,如果沒有什么意義。 請讓我知道,我將嘗試清除它。
提前謝謝。
我認為這是基本的OOP問題:多項式應該是Term的父類(或Polynomial是一個接口)
interface Polynomial {
double getExp();
}
class Term implements Polynomial {
private double coeff;
private double exp;
public Term (double e,double c){
coeff=c; exp=e;
}
@overide
public double getExp(){
return exp;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.