Tôi đã tạo lớp sau với phương thức main
, tạo ra phiên bản mới Application
và các phiên bản ApplicationModel
, ApplicationView
và ApplicationController
cho riêng Application
này.Làm thế nào để có được thể hiện cụ thể của lớp từ một lớp khác trong Java?
public class Application
{
// Variables
private ApplicationSettings settings;
private ApplicationModel model;
private ApplicationView view;
private ApplicationController controller;
// Constructor
public Application()
{
settings = new ApplicationSettings();
model = new ApplicationModel();
view = new ApplicationView(model);
controller = new ApplicationController();
}
// Main method
public static void main(String[] args)
{
Application application = new Application();
}
// Getters for settings, model, view, controller for instance of Application
}
Tôi biết rằng sẽ luôn chỉ có một trường hợp duy nhất là Application
.
Và tôi muốn có ví dụ cụ thể này trong các lớp học ApplicationModel
, ApplicationView
và ApplicationController
của mình.
Làm cách nào có thể?
Đây là giải pháp hoàn hảo cho tôi. Cảm ơn bạn rất nhiều! –
Và, có thể, bạn đã mắc lỗi đánh máy. 'Application _app' nên tĩnh, nếu nó được tham chiếu từ phương thức tĩnh.Xin vui lòng, sửa nó, vì tôi không chắc chắn. :) –
@EdwardRuchevits: vâng! Đó không phải là lỗi đánh máy. Tôi chỉ quên đặt từ khóa vào đó. Nó ** phải ** ** tĩnh **. –