繁体   English   中英

在Java中创建对象和数组

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM