新对象从先前创建的对象中删除数据

时间:2015-03-13 22:38:21

标签: java android object constructor android-studio

我的问题是我正在使用param'4 \ u221a(5)'创建类Pierwiastek的对象。之后我可以获取该对象的参数,如getFirst()和getInside()。然后我用不同的参数'2 \ u221a(2)'创建新对象。这里开始出现问题。也许我在代码上显示:

Pierwiastek p = new Pierwiastek('(4)\u221a(5)');
System.out.println(p.getFirst()+p.getInside());->4 and 5
Pierwiastek d = new Pierwiastek('(2)\u221a(2)');
System.out.println(d.getFirst()+d.getInside());->2 and 2
System.out.println(p.getFirst()+p.getInside());->2 and 2

为什么最后是2和2,而不是4和5?

这是我的构造函数:

public class Pierwiastek {
public static String first;
public static String inside;
public Pierwiastek(String pierwiastek) {
    String[] tmp = pierwiastek.split("\u221a");
    String a , b;
    if(tmp.length > 2){         
        a = pierwiastek.substring(0,pierwiastek.indexOf("\u221a")+1);
        b = pierwiastek.substring(pierwiastek.indexOf("\u221a")+1, pierwiastek.length());
        b = b.replace("\u221a","|");
        pierwiastek = a + b;
    }
    String[] t = pierwiastek.split("\u221a");
        if (String.valueOf(pierwiastek.charAt(1)).equals(")") || String.valueOf(pierwiastek.charAt(0)).equals("\u221a")) { 
            first = "1";
        } else {
            t[0] = t[0].replace("(", "");
            t[0] = t[0].replace(")", "");
            first = t[0];
        }
    t[1] = t[1].replace("(", "");
    t[1] = t[1].replace(")", "");
    t[1] = t[1].replace("|","\u221a");
    inside = t[1];
}
public void setFirst(String f) {
    first = f;
}

public void setInside() {
    inside = this.inside;
}

public String getFirst() {
    return this.first;
}

public String getInside() {
    return this.inside;
}

public static String zlozPierwiastek(){
    return "(" + first + ")\u221a(" + inside + ")";
}
}

1 个答案:

答案 0 :(得分:3)

属性firstinside不应该是静态的。尝试:

private String first;
private String inside;

zlozPierwiastek函数的相同内容:

public String zlozPierwiastek(){
    return "(" + first + ")\u221a(" + inside + ")";
}