3

我正在尝试将我的 Objective C 代码迁移到 Java 以学习这种编程语言。

我想将以下 ObjectiveC 结构“转换”为 Java,但找不到等效的 Java 结构:

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)
                      }
                      ...
                ];

哪个是类似的 Java“对象”?

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 回答
1

试试这个代码:

Descriptor.java

public class Descriptor {

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

    // CONSTRUCTORS

    public Descriptor() {}

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

    // SETTER

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

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

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

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

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

    // GETTER

    public int getTillAge() {
        return tillAge;
    }

    public int getFromAge() {
        return fromAge;
    }

    public float getBeginner() {
        return beginner;
    }

    public float getRegular() {
        return regular;
    }

    public float getPro() {
        return pro;
    }

}

然后,在您的活动课程中,MainActivity.java您可以为男性声明一个结构,为女性声明另一个结构

    Descriptor[] descriptorMale = {
            new Descriptor(13,  19, 5f, 7f, 9f),
            new Descriptor(20,  29, 6f, 13f, 16f),
            new Descriptor(30,  39, 7f, 18f, 19f),
            new Descriptor(40,  49, 8f, 29f, 33f),
            new Descriptor(50,  59, 9f, 30f, 37f),
            new Descriptor(60, 120, 10f, 41f, 44f)
    };

    Descriptor[] descriptorFemale = {
            new Descriptor(13,  19, 5f, 7f, 9f),
            new Descriptor(20,  29, 6f, 13f, 16f),
            new Descriptor(30,  39, 7f, 18f, 19f),
            new Descriptor(40,  49, 8f, 29f, 33f),
            new Descriptor(50,  59, 9f, 30f, 37f),
            new Descriptor(60, 120, 10f, 41f, 44f)
    };

最后,您可以通过以下方式使用 getter/setter 方法:

descriptorMale[1].getBeginner();
descriptorFemale[3].setFromAge(18);
于 2016-05-20T09:07:01.177 回答
0

字典在 Java 中称为地图。我建议不要像这样复制和粘贴代码,而是阅读一下不同类型的 Java 映射。如果您理解字典并且功能几乎相同,它们在概念上并不难理解。

http://www.tutorialspoint.com/java/java_map_interface.htm

于 2016-05-19T08:05:43.413 回答
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 回答