简体   繁体   English

如何初始化来自不同类的各种数据类型成员值?

[英]How to initialize various data type members values from different classes?

I have a public class named as ABC. 我有一个名为ABC的公开课。 There are various types of variables declared with the getter and setter method along with that. 用getter和setter方法声明的变量种类繁多。

Those values will be coming from various other classes. 这些值将来自其他各种类别。

What will be the best approach to initialize those data types? 初始化这些数据类型的最佳方法是什么?

I can go with making that class as singleton.I don't want to create multiple object for that class. 我可以将该类作为单例来进行,我不想为该类创建多个对象。

The values which will be coming from classes are B,C,D,E. 来自类的值是B,C,D,E。

If i create a singleton object in B and pass that with the Intent to C,D,E and assign remaining values will that be the best approach ? 如果我在B中创建一个单例对象并将其Intent传递给C,D,E并分配剩余值,那将是最好的方法?

OR 要么

creating a public static object as a constant.and access that object from different classes and store variable. 创建一个公共常量对象作为常量,然后从不同的类和存储变量访问该对象。

OR 要么

is there any other better approach ? 还有其他更好的方法吗?

Please advice. 请指教。 [Please Note: Coding is for Android] [请注意:编码适用于Android]

I would go for the singleton approach 我会选择单例方法

public class ABC
{
    private static ABC instance;

    private String memberVar;

    private ABC()

    public static ABC getInstance()
    {
        if (instance == null) {
            instance = new ABC();
        }
        return instance;
    }
}

Then you can access it from the other classes via ABC.getInstance().yourGetter() 然后,您可以通过ABC.getInstance()。yourGetter()从其他类访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM