简体   繁体   English

在Java中创建对象和数组

[英]Creating and array of objects in java

Hi guys I was wondering if you could shed some light on and idea I was thinking of for my current project. 嗨,大家好,我想知道您是否可以为我当前的项目提供一些思路和想法。 I looking to create say and array of terms, where terms contains two or three values/variables. 我希望创建术语和术语数组,其中术语包含两个或三个值/变量。

Let me try to clear this up as best I can right now. 让我尝试尽我所能来解决这个问题。

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);

i would also like to know if i would be able to call the values of the Term in the poly1 later like: 我也想知道以后是否可以在poly1中调用Term的值:

poly1[0].getExp
//where this would give me the exp value of 
//Term in the first entry in the poly1 array

sorry if doesn't make much sense. 对不起,如果没有什么意义。 Just let me know and I'll try to clear it up. 请让我知道,我将尝试清除它。

Thanks ahead of time. 提前谢谢。

I think this is basic OOP problem: Polynomial should be parent class of Term (or Polynomial is an interface) 我认为这是基本的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