应用程序类中的访问对象

本文关键字:访问 对象 应用程序 | 更新日期: 2023-09-27 18:29:34

我正在WPF中开发个人理财应用程序。我有我的MainWindow,它显然可以处理渲染。我的问题是,如何优雅地访问App类中的对象?我的结构是这样设置的:

class Application { }
class App : Application {
  private AccountManager accountManager;  // HOW DO I ELEGANTLY ACCESS THIS IN MAINWINDOW?  
                                          // USE SINGLETON PATTERN?
}
class MainWindow { }
class AccountManager {
  List<Account> accounts = new List<Account>();
}
static class AccountFactory {
  static Account CreateFactory(string Account);
}
class Account { }
class Asset : Account { }
class Equity : Account { }
class Expense : Account { }
class Income : Account { }
class Liability : Account { }

应用程序类中的访问对象

如果您想从另一个类访问一个类的成员,请将该成员设为public。请记住,公共字段通常是个坏主意,所以使用公共属性:

public AccountManager accountManager { get; private set; }

setter是私有的,因此MainWindow类的属性将是只读的。感谢@HighCore提供的可读性提示。