与班级一起工作 [英] Working with classes

查看:137
本文介绍了与班级一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是简单而基本的.我用C#编写了很长时间.我有一个3类的项目.主类和2个自定义类.我忘记了如何使用在一个中创建的字符串并在另一个中调用它.下面是示例.我有一个清单,我想用 在另一个班级.我想我不确定是否必须将其放入方法中.谢谢.所有这些类都是相同名称空间的一部分.

This is easy and basic. I havnt written in C# for a long time. I have a project with 3 classes. The main class and 2 custom classes. I forgot how to use strings created in one and call it in another. below is the example. I have a list and I want to use it in another class. I think I have to put it in the method not sure though. Thanks. All the classes are part of the same name space.

推荐答案

这是一个非常简单的示例.

Here is a very simple example.

public class ParentClass
{
    public void Demo()
    {
        var c1 = new Class1();
        var c2 = new Class2(c1.NameList);
    }
}

public class Class1
{
    private List<string> mNames;
    public List<string> NameList { get { return mNames; } }
    public List<string> NameList2  { get; set; }
    public Class1()
    {
        mNames = new List<string>() { "Karen","Joe"};
        NameList2 = new List<string>() { "Bill", "Mary","Ann" };
    }
}
public class Class2
{
    private List<string> mNames;
    public Class2(List<string> pNameList)
    {
        mNames = pNameList;
    }
    public void Demo()
    {
        mNames.ForEach(name => Console.WriteLine(name));
    }
}


这篇关于与班级一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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