java 如何让两个类共享相同的变量定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10515769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I have two classes share the same variable definitions
提问by Patrick Aquilone
What I really need is to be able to declare regular variables in an interface and implement that interface in two classes that I would not have to have to re-declare these in each class (ie class.data.variables instead of class.variables). Is there any way that I could achieve the same goal differently?
我真正需要的是能够在接口中声明常规变量并在两个类中实现该接口,我不必在每个类中重新声明这些(即 class.data.variables 而不是 class.variables) . 有什么方法可以让我以不同的方式实现相同的目标?
To give more detail. Essentially, I have created a small drawing program that drops JLabels on a JPanel that is on a JScrollPane. Because I have a specific design for these JLabels (ie they are not just for drawing they represent airline objects for this application), I have a class that extends JLabel and adds my application specific variables to it. Ultimately, I read and write an XML file with these variables so they can load and save their designs. Since I can not use this extended class for my XML definitions because it screams about the parent class even though I told it to have NONE as the accessor (I read there is a bug), I have to create an identical class and copy values back and forth for saving and loading. Not too much of a problem except when I add a variable to the JLabel extended class and forget to add it to the XML mimic class and subsequent copy routines.
提供更多细节。本质上,我创建了一个小型绘图程序,将 JLabels 放在 JScrollPane 上的 JPanel 上。因为我对这些 JLabel 有一个特定的设计(即它们不只是用于绘制它们代表此应用程序的航空公司对象),所以我有一个扩展 JLabel 的类并将我的应用程序特定变量添加到它。最终,我使用这些变量读写 XML 文件,以便他们可以加载和保存他们的设计。由于我不能将这个扩展类用于我的 XML 定义,因为即使我告诉它有 NONE 作为访问器,它也会对父类尖叫(我读到有一个错误),我必须创建一个相同的类并将值复制回来来回保存和加载。
So, it would be great if I could make one class (say CellDataRecord.java) that held the extra data declarations and have that class be used in both places (the JLabel extension and the XML data) without having to have something like XML.data.CellDataRecordXXX.
因此,如果我可以创建一个类(比如 CellDataRecord.java)来保存额外的数据声明并且在两个地方(JLabel 扩展和 XML 数据)都使用该类,而不必具有类似 XML 的东西,那就太好了。数据.CellDataRecordXXX。
采纳答案by Ozzy
You can do that with inheritance or using an interface, where the variable is set as a constant in the parent class. Since you are extending a JLabel, you should implement the interface on both classes:
您可以通过继承或使用接口来实现,其中变量在父类中设置为常量。由于您要扩展 JLabel,您应该在两个类上实现接口:
public interface MyInterface {
int someint = 9;
}
public class MyClass1 extends JLabel implements MyInterface {
//this class has access to `someint`
}
public class MyClass2 extends JLabel implements MyInterface {
// also has access to `someint`
}
Edit
编辑
Since you want to be able to change the same variable from different classes, you have to ensure you aren't changing copies and are changing the same variable, so you should use a volatile
keyword on the variable to indicate to java that all threads should check the value before it updates it.
由于您希望能够从不同的类更改相同的变量,您必须确保您没有更改副本并且正在更改相同的变量,因此您应该volatile
在变量上使用关键字来指示 java 所有线程都应该检查更新之前的值。
Now you'll need to have a separate class so that instances can be made from other classes to get the value. You have to use the static
keyword to ensure that one copy is kept for all class instances.
现在您需要有一个单独的类,以便可以从其他类创建实例来获取值。您必须使用static
关键字来确保为所有类实例保留一份副本。
public class MyVariableWrapper {
public static volatile int some_var = 9;
public void updateSomeVar(int newvar) {
some_var = newvar;
}
public int getSomeVar() { return some_var; }
}
Now the other two classes just do this:
现在其他两个类只是这样做:
public class MyClass1 extends JLabel {
MyVariableWrapper myVariableWrapper;
MyClass1() {
super();
myVariableWrapper = new MyVariableWrapper();
// now I have access to `some_var`
}
}
public class MyClass2 extends JLabel {
MyVariableWrapper myVariableWrapper;
MyClass2() {
super();
myVariableWrapper = new MyVariableWrapper();
// now I have access to the same `some_var` as MyClass1
}
// this is a wrapper method for your convenience
// since you don't like the excess code when accessing the variable
public int getSomeVar() {
return myVariableWrapper.some_var;
// or myVariableWrapper.getSomeVar();
}
public void setSomeVar(int newvar) {
myVariableWrapper.some_var = newvar;
// or myVariableWrapper.setSomeVar(newvar);
}
}
Now you can do this:
现在你可以这样做:
MyClass2 myClass2 = new MyClass2();
System.out.println(""+myClass2.getSomeVar());
回答by kyiu
I'm not sure I 100% grasp your problem but from the first few lines of your description, instead of implementing an interface, you could define an abstract class and have your classes extend it. That way, you'll be able to define attributes in the abstract class and these will be common to all subclasses.
我不确定我是否 100% 理解您的问题,但是从您描述的前几行开始,您可以定义一个抽象类并让您的类扩展它,而不是实现一个接口。这样,您将能够在抽象类中定义属性,并且这些属性对所有子类都是通用的。