从抽象类中的Jframe类获取变量

时间:2022-09-25 07:33:28

My problem that I want to get variable from jframe class to abstract class in java

我的问题是我想从jframe类变量到java中的抽象类

exemple:

public class MainWindow extends Frame {
    String Variable 1;
    ..... 
}

public abstract class AbstractController { 

    // I need to use variable 1 in this class...

}

thank for all...

谢谢大家......

1 个解决方案

#1


1  

My problem that I want get variable from jframe class to abstract class in java

我想要的问题是从java中的jframe类到抽象类的变量

I'm going to suggest that no, you really don't want to do that, you don't want to directly access and modify variables from one class in another. You appear to be creating something akin to a model-view-controller or view-controller, and so you will likely want to pass references as needed, encapsulate all variables, and access variables or "states" through controlled public methods. So something like so:

我会建议不,你真的不想这样做,你不想直接访问和修改另一个类中的变量。您似乎正在创建类似于模型 - 视图 - 控制器或视图 - 控制器的东西,因此您可能希望根据需要传递引用,封装所有变量,并通过受控公共方法访问变量或“状态”。所以像这样:

public class View {
    private String someStateField;
    //..... 

    public String getSomeStateField() {
        return someStateField;
    }

    public void setSomeStateField(String someStateField) {
        this.someStateField = someStateField;
    }
}

and the controller

和控制器

public abstract class AbstractController { 
    private View view;

    public AbstractController(View view) {
        this.view = view;
    }

    public view getView() {
        return view;
    }

    public void someMethod() {
        // can now call view methods here
    }

}

Of course the concrete version of your controller will need to have a similarly structured constructor and call the super's constructor:

当然,控制器的具体版本需要具有类似结构的构造函数并调用super的构造函数:

public class ConcreteController { 

    public ConcreteController(View view) {
        super(view);
    }

    // ....

}    

and the main method would need to hook it all together:

并且主要方法需要将它们全部挂钩:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        View view = new View();
        AbstractController controller = new ConcreteController(view);
        // .....
    });
}

The specifics of your solution will of course depend on the rest of your program's structure and set up, and if you need a more detailed answer, then please update your question with greater detail and code, and comment back to me.

您的解决方案的细节当然取决于您的程序的其余结构和设置,如果您需要更详细的答案,请更新您的问题更详细和代码,并回复给我。

#1


1  

My problem that I want get variable from jframe class to abstract class in java

我想要的问题是从java中的jframe类到抽象类的变量

I'm going to suggest that no, you really don't want to do that, you don't want to directly access and modify variables from one class in another. You appear to be creating something akin to a model-view-controller or view-controller, and so you will likely want to pass references as needed, encapsulate all variables, and access variables or "states" through controlled public methods. So something like so:

我会建议不,你真的不想这样做,你不想直接访问和修改另一个类中的变量。您似乎正在创建类似于模型 - 视图 - 控制器或视图 - 控制器的东西,因此您可能希望根据需要传递引用,封装所有变量,并通过受控公共方法访问变量或“状态”。所以像这样:

public class View {
    private String someStateField;
    //..... 

    public String getSomeStateField() {
        return someStateField;
    }

    public void setSomeStateField(String someStateField) {
        this.someStateField = someStateField;
    }
}

and the controller

和控制器

public abstract class AbstractController { 
    private View view;

    public AbstractController(View view) {
        this.view = view;
    }

    public view getView() {
        return view;
    }

    public void someMethod() {
        // can now call view methods here
    }

}

Of course the concrete version of your controller will need to have a similarly structured constructor and call the super's constructor:

当然,控制器的具体版本需要具有类似结构的构造函数并调用super的构造函数:

public class ConcreteController { 

    public ConcreteController(View view) {
        super(view);
    }

    // ....

}    

and the main method would need to hook it all together:

并且主要方法需要将它们全部挂钩:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        View view = new View();
        AbstractController controller = new ConcreteController(view);
        // .....
    });
}

The specifics of your solution will of course depend on the rest of your program's structure and set up, and if you need a more detailed answer, then please update your question with greater detail and code, and comment back to me.

您的解决方案的细节当然取决于您的程序的其余结构和设置,如果您需要更详细的答案,请更新您的问题更详细和代码,并回复给我。