Java / Android - 子对象的不可变字符串

时间:2014-06-21 22:11:19

标签: java string inheritance superclass

我有3个Java类:Class1,Class2和Class3。

public class Class1 {
    ArrayList<Class3> arrayOfObjects;

    public Class1() {
        if (arrayOfObjects == null){
            arrayOfObjects = new ArrayList<Class3>
        }
    }
}

public class Class2{
    public String mName;

    public Class2(String name){
        this.mName = name;
    }
}


public class Class3 extends Class2{
    public Class3(String name){
        super(name);
    }
}

Class1的每个实例都包含一个对象的ArrayList。我的问题是,因为字符串是不可变的,如何在Class2中更改它们时,如何让Class3的所有子对象更新它们的“mName”变量?我已经尝试过使用For循环并循环遍历所有内容,这样可以正常工作,但随着列表越来越大,所涉及的时间也越来越多。

2 个答案:

答案 0 :(得分:-1)

为String

创建一个包装器对象
public class MutableString
{
    private String value;
    public MutableString(String value)
    {
        this.value = value
    }
    public void set(String value)
    {
        this.value = value;
    }
}

在你的代码中,Class3和Class2不存储字符串,而是存储对MutableString类型的对象的引用

答案 1 :(得分:-1)

声明

这是一个关于变量阴影,范围和静态字段的非常长篇,非常基础的教程。通过对您的问题的编辑,它并不像原来那样真正适用 我开始写它,但是我觉得我已经完成了,也许它会对某人有用,或者我会找到一个地方将它迁移到将来......


您无需在mName中声明Class3,因为它已从其父级Class2继承该字段。

但是,如果您希望能够对任何Class2进行更改并在Class3中查看结果,那么您需要了解static variablesvariable scopevariable shadowing


变量阴影

如果在Class3中声明另一个字段,其名称与Parent类也具有相同的名称,那么它将“隐藏”或“隐藏”原始字段,因为Java将使用具有正确名称的第一个变量它找到了。


考虑一个非常简单的基类:

public class Parent {
    public int myVariable = 5;

    public void printMyVariable() {
        System.out.println(myVariable);
    }
}

现在让我们定义一些孩子:

public class ChildOne extends Parent {
    // nothing here
}

public class ChildTwo extends Parent {
    public void printMyVariable() {
        System.out.println(myVariable + 2);
    }
}

public class ChildThree extends Parent {
    public int myVariable = 10;
}

public class ChildFour extends Parent {
    public int myVariable = 99;

    public void printMyVariable() {
        int myVariable = 700;
        System.out.println(myVariable);
    }

现在让我们看一下JVM如何读取所有这些不同的值:

public static void main (String[] args) {
    // make some objects:
    Parent parent = new Parent();
    ChildOne childOne = new ChildOne();
    ChildTwo childTwo = new ChildTwo();
    ChildThree childThree = new ChildThree();

    // print some values:
    parent.printMyVariable();
    childOne.printMyVariable();
    childTwo.printMyVariable();
    childThree.printMyVariable();
    childFour.printMyVariable()
}

这会将这些值打印到您的控制台:

5    // original parent class' base value

5    // does not try to override, mask, hide the parent's value or print

7    // printMyVariable() is defined again and the JVM uses
     //   the first matching method it finds which is in the ChildTwo class

10   // myVariable is defined in the class
     //   the class variable shadows the parent variable but the method
     //   still functions as expected

700  // myVariable is shadowed at the class level
     // but the print method also declares a myVariable
     // it is the method's version that gets found first and used

静态变量

现在所有这些都与'实例变量'有关。这些变量对于它们存在的每个副本或对象都有自己的唯一值 如果您更改childOne's,则myVariable对childTwo's值或甚至您可能已指定eslewhere的另一个ChileOne实例没有影响。

您是否将这些字段定义为static,但是,您要说的是要在该类的所有副本或实例之间共享相同的变量和值。

public class Parent {
    public static int myStaticVariable = 25;
}

public class ChildOne extends Parent {
    // nada
}

public class ChildTwo extends Parent {
    public static int myStaticVariable = 99;
}

如果我们现在捅静态变量:

public static void main (String[] args) {
    // a few classes
    Parent parent = new Parent();
    ChildOne childOne = new ChildOne();
    ChildOne childOneCopy = new childOne();
    ChildTwo childTwo = new childTwo();

    // take a peek at the values
    System.out.println(parent.myStaticVariable):
    System,out.println(childOne.myStaticVariable);
    System,out.println(childTwo.myStaticVariable);

    // let's change the value
    parent.myStaticVariable = 66;
    System.out.println(parent.myStaticVariable):
    System,out.println(childOne.myStaticVariable);
    System,out.println(childTwo.myStaticVariable);

    // and the two copies of ChildOne?
    childOne.myStaticVariable = 123;
    System,out.println(childOneCopy.myStaticVariable);
    childOneCopy.myStaticVariable = 456;
    System,out.println(childOne.myStaticVariable);
}

这些结果:

25    // no surprise here

25    // ChildOne does not define its own variable so it uses the Parent's

99    // As in the earlier example the variable is getting shadowed


66    // we just changed the variable value so this looks normal

66    // even though it was the Parent's variable we changed
      //   the Child shares it too

99    // Since ChildTwo's variable is shadowing the Parent value it has not changed.


123   // 123? But we changed the original ChildOne...

456   // Again we modified the other object but since they share
      //   the same static variable; changing one also changes the other.