简体   繁体   English

在Java中对对象进行的更改

[英]Changes being made to an object in Java

I have created a Compound class that holds the number of Carbon, Hydrogen, Oxygen, Nitrogen and it's bond count. 我创建了一个复合类,它包含碳,氢,氧,氮的数量和它的键数。 I have a stack that holds these objects. 我有一个包含这些对象的堆栈。

Initially the stack will start off at empty and I will pop that. 最初堆栈将从空开始,我会弹出它。 Then I will apply addHydrogen function to it so it's Hydrogen will = 1, Oxygen=0, Nitrogen=0 and Carbon=0. 然后我将对它应用addHydrogen函数,因此它的氢气将= 1,氧气= 0,氮气= 0且碳= 0。

I then want to take the same object and apply the addCarbon function so that Hydogren will = 0, Oxygen=0, Nitrogren=0 and Carbon=1. 然后我想采用相同的对象并应用addCarbon函数,以便Hydogren将= 0,Oxygen = 0,Nitrogren = 0和Carbon = 1。

How can I write my program so I can use the same object but not with the changes I made from adding the Hydrogen? 如何编写我的程序,以便我可以使用相同的对象,但不能使用添加Hydrogen所做的更改? I know I could use some if cases initially but I don't think it will work because I will eventually start with a compound that has hydrogen=2, oxygen=2, Nitrogren=0, Carbon=1. 我知道我最初可以使用一些情况,但我不认为它会起作用,因为我最终会开始使用氢= 2,氧= 2,硝化物= 0,碳= 1的化合物。

*I didn't include my constructors in the code, they just initialize everything to 0. *我没有在代码中包含我的构造函数,只是将所有内容初始化为0。

class compound {

    int Hydrogen;
    int Carbon;
    int Nitrogen;
    int Oxygen;
    int bond;

    public void addHydrogen(compound comp) {
        Hydrogen++;
    }

    public void addCarbon(compound comp) {
        Carbon++;
    }

}
    public static void main(String[] args) {
        Compound a= new Compound();
        a.addHydrogen(a);
        a.addCarbon(a);
    }

Your question isn't very clear, but one option you might wish to consider would be to make your object immutable - so you could never change the values within a single object, but you could instead make your addCarbon (etc) methods return a new object with appropriate new values in. 您的问题不是很清楚,但您可能希望考虑的一个选项是使您的对象不可变 - 因此您永远不能更改单个对象中的值,但您可以改为使addCarbon (etc)方法返回一个新的具有适当新值的对象。

(Note that currently you're not using your parameters, which appear to have the wrong case anyway...) (请注意,目前您没有使用您的参数,无论如何它们看起来都是错误的...)

Sample code: 示例代码:

public class Compound {
    private final int hydrogen;
    private final int carbon;
    private final int nitrogen;
    private final int oxygen;

    public Compound(int hydrogen, int carbon, int nitrogen, int oxygen) {
        this.hydrogen = hydrogen;
        this.carbon = hydrogen;
        this.nitrogen = nitrogen;
        this.oxygen = oxygen;
    }

    public int getHydrogen() {
        return hydrogen;
    }

    // ... etc for the other getters

    public Compound plusHydrogen() {
        return new Compound(hydrogen + 1, carbon, nitrogen, oxygen);
    }

    // etc for the other plus calls
}

Note that I've called the methods plusHydrogen etc to make it clear that they're not mutating the existing object, but returning a new one. 请注意,我已经调用了方法plusHydrogen等,以明确它们不会改变现有对象,而是返回一个新对象。

Then you can have: 然后你可以:

Compound base = new Compound(0, 0, 0, 0);
Compound withHydrogen = base.plusHydrogen();
Compound withCarbon = base.plusCarbon();
// whatever

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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