3

I am trying to migrate my Objective C code into Java to learn this programming language.

I would like to "convert" the following ObjectiveC structure into Java, but I couldn't find the equivalent Java structure:

g_vo2MaxFemales = @[
                      @{
                          @"fromAge": @(13),
                          @"tillAge": @(19),
                          @"beginner": @(29.95),
                          @"regular": @(36.95),
                          @"pro": @(40.5)
                      },
                      @{
                          @"fromAge": @(20),
                          @"tillAge": @(29),
                          @"beginner": @(28.25),
                          @"regular": @(35),
                          @"pro": @(39)
                      }
                      ...
                ];

Which is the similar Java "object"?

4

5 に答える 5

3

YourClassクラスとすべてのインスタンス フィールドを持つコンストラクターがあれば簡単です。の配列を作成するだけですYourClass

YourClass[] array = {
    new YourClass(13, 19, 29.95, 36.95, 40.5),
    new YourClass(...),
    ...
};

クラスは次のようになります

class YourClass {
    private int fromAge;
    private int tillAge;
    private double beginner;
    private double regular;
    private double pro;

    public YourClass(int fromAge, int tillAge, double beginner, double regular, double pro) {
         this.fromAge = fromAge;
         this.tillAge = tillAge;
         this.beginner = beginner;
         this.regular = regular;
         this.pro = pro;
    }
 }

フィールドの名前はきれいではありません.OPの名前を理解のために使用しました。


Project Lombokを使用する@AllArgsConstructorと、クラスの上に注釈を記述でき、この大規模なコンストラクターを記述する必要はありません。コンパイル段階で生成されます。

于 2016-05-19T07:44:28.310 に答える
2

クラスはこのようになります。

public class MaxFemales {

private int fromAge;
private int tillAge;
private double beginner;
private double regular;
private double pro;

public MaxFemales(int fromAge, int tillAge, double beginner, double regular, double pro) {
    this.fromAge = fromAge;
    this.tillAge = tillAge;
    this.beginner = beginner;
    this.regular = regular;
    this.pro = pro;
}

public int getFromAge() {
    return fromAge;
}

public void setFromAge(int fromAge) {
    this.fromAge = fromAge;
}

public int getTillAge() {
    return tillAge;
}

public void setTillAge(int tillAge) {
    this.tillAge = tillAge;
}

public double getBeginner() {
    return beginner;
}

public void setBeginner(double beginner) {
    this.beginner = beginner;
}

public double getRegular() {
    return regular;
}

public void setRegular(double regular) {
    this.regular = regular;
}

public double getPro() {
    return pro;
}

public void setPro(double pro) {
    this.pro = pro;
}
}

使用したい場所で、ArrayList<MaxFemales> mFemaleList = new ArrayList<MaxFemales>();

すべての引数を指定してクラスを構築し、単純に配列リストに追加します。

お役に立てれば。

于 2016-05-19T07:56:04.830 に答える
0

You can implement this in Java with an ArrayList.

Example code:

class ModelObject {
  private Integer fromAge;
  private Integer tillAge;

  // followed by the other attributes

  public ModelObject(Integer fromAge, Integer tillAge, ...){
    this.fromAge = fromAge;
    this.tillAge = tillAge;
  }

  public Integer getFromAge(){
    return this.fromAge;
  }

  public Integer getTillAge(){
    return this.tillAge;
  }

  //getter for the outher attributes

  public void setFromAge(Integer fromAge){
    this.fromAge = fromAge;
  }

  public void setTillAge(Integer tillAge){
    this.tillAge = tillAge;
  }

  //setter for the outher attributes

  public static void main(String[] args) {
      List<ModelObject> list = new ArrayList<ModelObject>();
      list.add(new ModelObject(fromAge, tillAge, ...));
  }
}
于 2016-05-19T07:47:57.107 に答える