本身实例化一个类的最佳方法是什么? [英] What is the best way to intantiate a class in itself ?

查看:97
本文介绍了本身实例化一个类的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个噱头的问题,我想尝试使它工作。



我想在一个单独的CS文件中有一个类,已经有一些自己定义的实例和一个可以在Main中引用的实例的List。可能吗?我应该使用嵌套类吗?或者,最好的方法是什么?



如果能够工作,我将永远不必编辑这个类,除了添加更多实例。我只是不想在Main中实例化这个类,因为我不想在那里意外地改变一些值,通过一些错误点击等等。我只想把这个类放在一个单独的文件中并且永远不再看它,只是引用已经存在的列表。



我尝试过:



I have a problem with one gimmick that I would like to try and make it work.

I would like to have a class in a separate CS file, that already has a few instances of it defined in itself and a List of those instances availible for reference in Main. Is it possible? Should I use a nested class for it? Or else, what would be the best way to do it?

If this is going to work I will never have to edit this class except to add more instances. I just don't want to instantiate this class in Main because I don't want to accidentially change some values there, by some missclick etc. I just want to have this class in a separate file and to never look at it again, just referencing the list that is already there.

What I have tried:

class Parent
{
        private static List<Nested> list_of_instances = new List<Nested>();

        readonly Nested instance1 = new Nested { parameter1=123, parameter2=1234};
        readonly Nested instance2 = new Nested { parameter1=321, parameter2=4321};

        public static List<Nested> List_of_instances
        {
            get { return list_of_instances; }
        }


   class Nested
   {
            public double parameter1 { get; set; }
            public double parameter2 { get; set; }

            public Nested()
            {
                list_of_instances.Add(this);
            }
   }
} 





在Main我想获取那些默认列表像这样的例子:





And in Main I want to obtain the list of those default instances like this:

List<double> parameter1_list = new List<double>();
foreach (Parent.Nested item in Parent.List_of_instances)
            {
                parameter1_list.Add(item.parameter1);
            }
            
            ComboBox1.ItemsSource = parameter1_list; 



在XAML中我只有一个简单的ComboBox,我希望用每个类实例的parameter1值填充:


In XAML I have just a simple ComboBox that I want to populate with values of parameter1 of every class instance:

<ComboBox x:Name="ComboBox1"/>



最后,没有任何内容添加到组合框中,我在这个程序中没有错误,所以我不知道从哪里开始寻找我的错误。



这整个概念是错的还是可行的?这样做的正确方法是什么?


In the end, nothing is added to the combo box and I get no errors in this program so I don't know where to start looking for my mistake.

Is this whole concept just wrong or is this doable? What would be the correct way for getting this do work?

推荐答案

不建议采用整体方法,但为了回答您的问题,您需要对代码进行某些更改。以下是您需要在父类中进行的更改,以解决您的问题。



The overall approach is not very recommended but to answer your question you need to do certain changes in your code. Below are the changes that you need to do in your parent class which should solve your problem.

public class Nested
{
        private static Nested instance1 = new Nested { parameter1 = 123, parameter2 = 1234 };
        private static readonly Nested instance2 = new Nested { parameter1 = 321, parameter2 = 4321 };
        private static List<Nested> list_of_instances = new List<Nested>();
        public static List<Nested> List_of_instances
        {
            get
            {
                if (list_of_instances == null)
                {
                    list_of_instances.Add(instance1);
                    list_of_instances.Add(instance2);
                }
                return list_of_instances;
            }
        }
        public double parameter1 { get; set; }
        public double parameter2 { get; set; }

        private Nested()
        {
            //Private constructor will make sure that no other class able to make instance of this object
        }
}





您现在可以在Main方法中循环实例列表: -





You can now loop through your instance list in Main method:-

List<double> parameter1_list = new List<double>();
            foreach (Nested item in Nested.List_of_instances)
            {
                parameter1_list.Add(item.parameter1);
            }

            ComboBox1.ItemsSource = parameter1_list;


我会回答我自己的问题...



要实例化一个类,我所要做的就是为每个新实例添加一个static关键字。



就是这样。不需要嵌套类。
Well I will answer my own question...

To instantiate a class in itself all I had to do was to add a "static" keyword for every new instance.

That was all. No need for nested classes.


这篇关于本身实例化一个类的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆