English 中文(简体)
我如何能够有两班共享相同的变量定义
原标题:How can I have two classes share the same variable definitions

我确实需要的是,能够宣布一个接口中的定期变量,并在两个类别中实施这一接口,而我不必重新申报这些变量。 我能否以不同方式实现同一目标?

提供更多细节。 基本上,我制定了一个小幅绘画方案,将JLabels从一个正在JProllPane的JPanel上 drop。 由于我有这些日志的具体设计(即他们不仅是为申请目的而提炼航空公司物体),因此我有一个班子,延伸了JLabel,并增加了我的具体变量。 最后,我阅读并撰写一份含有这些变量的XML文件,以便它们能够装载和节省设计。 页: 1 我无法将这一扩大的类别用于我的XML定义,因为尽管我告诉我说NONE是进入者,但我不得不为储蓄和装货设定相同的类别和复制价值。 除了在JLabel延长班级增加一个变量并忘记在XML奇米级和随后的例行复印件中增加这一变量外,问题还不多。

因此,如果我能做一个类别(例如,Mile DataRecord.java),即持有额外数据申报,并在两个地点(JLabel延伸和XML数据)使用这一分类,而不必有XML.data.CellDataRecordXXX等。

最佳回答

在继承或使用接口的情况下,如果变数在母系中固定,你就可以这样做。 由于你正在延长一个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

由于你希望能够改变不同类别相同的变数,你必须保证您的正本不会改变,并且正在改变同样的变数,因此,你应当使用<条码>挥发性<<>> > 符号>,在变量上用关键词来表示,所有线人都应核对其更新的价值。

现在,你需要单独开设一个班级,以便从其他班级获得价值。 您必须使用<代码>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; }
}

现在,其他两个班子也这样做:

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);
    }
}

现在你可以这样做:

MyClass2 myClass2 = new MyClass2();
System.out.println(""+myClass2.getSomeVar());
问题回答

我不敢肯定我会百分之百地了解你的问题,但从你描述的头几行来看,你可以确定一个抽象的类别,并延长其班级。 这样,你就能够界定抽象类别中的属性,这些属性对所有子类都很常见。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签