用Java创建对象并分配项目

时间:2019-01-27 10:40:37

标签: java object

Javascript中,可以创建一个对象,例如:

       newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
        }

只需调用this.zero_to_one.self =2

即可轻松地在javascript中修改数据

我如何在Java中实现相同目标

2 个答案:

答案 0 :(得分:1)

使用Java声明和创建对象

简短版本

JS到Java的转换如下:

JS

newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
}

JAVA

// ZeroToOne.java
public class ZeroToOne {

    int self; // Type self
    int bulk; // Type bulk
    int norm; // Type norm

    /**
     * GETTERS AND SETTERS
     */

    public int getSelf() {
        return self;
    }

    public void setSelf(int self) {
        this.self = self;
    }

    public int getBulk() {
        return bulk;
    }

    public void setBulk(int bulk) {
        this.bulk = bulk;
    }

    public int getNorm() {
        return norm;
    }

    public void setNorm(int norm) {
        this.norm = norm;
    }

}

以同样的方式,您可以使用one_to_2two_to_4over_four来做到这一点。

这称为简单对象创建,这就是我们在Java中称为POJO

  

ℹ️更多信息:
  Plain_old_Java_object

大版本

按照上一个示例:

   public class ZeroToOne {

    // Attributes of the ZeroToOne class
    private int self; // Type self
    private int bulk; // Type bulk
    private int norm; // Type norm

    // Methods of the ZeroToOne class

    /**
     * GETTERS AND SETTERS
     */
    public int getSelf() {
        return self;
    }

    public void setSelf(int s) {
        this.self = s;
    }

    public int getBulk() {
        return bulk;
    }

    public void setBulk(int b) {
        this.bulk = b;
    }

    public int getNorm() {
        return norm;
    }

    public void setNorm(int norm) {
        this.norm = norm;
    }
}

请注意,在类正文中,已定义了{} 键之间的

  • 三个attributes(也称为private fields):selfbulknorm

  • 六个公共方法(public):getSelfsetSelfgetBulksetBulkgetNorm和{{1 }}。

以这种方式,所有从setNorm类创建的对象将具有一个ZeroToOneselfbulk,它们将能够存储不同的值,可以在调用其定义的方法时进行修改或查阅:

  • norm / setSelf / setBulk允许您将 set 分配为setNorm / {{1} } / self(int)到bulk 类的对象。

  • norm / ZeroToOne / getSelf允许您咨询 get getBulk / {{1} } / getNorm属于self类的对象

声明并创建类bulk 的对象:

norm

此外,可以在一行中显示相同的内容:

ZeroToOne

修改值

在哪里可以直接修改ZeroToOne的值,您将必须按以下方式进行操作:

    ZeroToOne callZeroToOne; // Declaration of variable p1 of type Person
    ZeroToOne zOne = new ZeroToOne (); // Create an object of the Person class

我们会得到什么

ZeroToOne callZeroToOne = new ZeroToOne();

答案 1 :(得分:1)

newdata: {
            zero_to_one: {self: 0, bulk: 0, norm: 0},
            one_to_2: {self: 0, bulk: 0, norm: 0},
            two_to_4: {self: 0, bulk: 0, norm: 0},
            over_four: {self: 0, bulk: 0, norm: 0},
}

如果您查看结构, 它是对象中的对象。 因此,您可以做的是创建4个子对象zero_to_one, one_to_2,two_to_4,over_four。

所有四个子对象都具有相同的三个字段self,bulk,norm。 如果需要,也可以将所有四个合为一体。

一个简单的对象创建(我们在Java中称为POJO)需要以下条件:

public class ZeroToOne{
        String self;
        String bulk;
        String norm;

        public String getSelf() {
            return self;
        }

        public void setSelf(String self) {
            this.self= self;
        }

        public String getBulk() {
            return bulk;
        }

        public void setBulk(String bulk) {
            this.bulk = bulk;
        }

        public String getNorm() {
            return norm;
        }

        public void setNorm(String norm) {
            this.norm = norm;
        }

    }

之后,您可以使用这些getter / setter方法获取并设置值。