C# 尝试使用方法将旧对象的属性分配给新对象

C# Trying to use a method to assign attributes of an old object to new objects

提问人:kenLeeDep 提问时间:7/21/2023 最后编辑:kenLeeDep 更新时间:7/21/2023 访问量:22

问:

我是 c# 和一般编程的新手,大概有几周的时间,这让我感到困惑。下面的代码是我遇到的问题。

internal class Program
    {
        static void Main(string[] args)
        {
            Students tester1 = new Students();
            Students tester2 = new Students();

            Assigner(tester1, tester2);

            Console.WriteLine("{0}, {1}.",
                tester1.name, tester2.name);
            Console.WriteLine("{0}, {1}.",
                tester1.birthday, tester2.birthday);
            Console.WriteLine("{0}, {1}.",
                tester1.height, tester2.height);


            Console.ReadLine();
        }


       public static void Assigner(Students aMulti1, Students aMulti2)
        {
            Students student1 = new Students("Jack", 0411, 155);
            Students student2 = new Students("Kelly", 0704, 179);
            Students student3 = new Students("Gabriel", 1212, 177);
            Students student4 = new Students("Irene", 0222, 169);
            Students student5 = new Students("Anderson", 0115, 162);

            Shuffler shuffler = new Shuffler();
            List<Students> studentList = new List<Students> 
            { student1, student2, student3, student4, student5 };
            shuffler.Shuffle(studentList);

            aMulti1 = studentList[0];
            aMulti2 = studentList[1];

        }

我的目的是随机化对象列表(studentList),然后让输入对象从列表中随机选择对象的属性。问题是该方法似乎没有做任何事情,因为 tester1 和 tester2 仍然具有空属性。

类代码如下,我复制了 Shuffler。

    public class Shuffler
    {
        public Shuffler()
        {
            _rng = new Random();
        }

        public void Shuffle<T>(IList<T> array)
        {
            for (int n = array.Count; n > 1;)
            {
                int k = _rng.Next(n);
                --n;
                T temp = array[n];
                array[n] = array[k];
                array[k] = temp;
            }
        }

        private System.Random _rng;
    }

    public class Students
    {
        public string name { get; set; }
        public int birthday { get; set; }
        public int height { get; set; }

        public Students()
        {

        }

        public Students (string aName, int aBirthday, int aHeight)
        {
            name = aName;
            birthday = aBirthday;
            height = aHeight;
        }

    }

我已经在下面测试了该方法本身,它有效,让 tester1 和 tester2 具有随机学生的属性。

static void Main(string[] args)
        {
            Students tester1 = new Students();
            Students tester2 = new Students();

            Students student1 = new Students("Jack", 0411, 155);
            Students student2 = new Students("Kelly", 0704, 179);
            Students student3 = new Students("Gabriel", 1212, 177);
            Students student4 = new Students("Irene", 0222, 169);
            Students student5 = new Students("Anderson", 0115, 162);

            Shuffler shuffler = new Shuffler();
            List<Students> studentList = new List<Students> { student1, student2, student3, student4, student5 };
            shuffler.Shuffle(studentList);

            tester1 = studentList[0];
            tester2 = studentList[1];

            Console.WriteLine("{0}, {1}.", 
                tester1.name, tester2.name);
            Console.WriteLine("{0}, {1}.",
                tester1.birthday, tester2.birthday);
            Console.WriteLine("{0}, {1}.",
                tester1.height, tester2.height);

            Console.ReadLine();


        }

让我停顿的是,我编写的原始方法,但体积更大,不必要地混乱,我一直在尝试返工,似乎有效,但它要求我首先生成并随机化字符串列表,然后使用字符串作为 if 条件来确定分配了哪些属性。

        static void Main(string[] args)
        {
            Students tester1 = new Students();
            Students tester2 = new Students();

            Shuffler shuffler = new Shuffler();
            List<string> studentList = new List<string> {
                   "student1", "student2", "student3", "student4", "student5"};
            shuffler.Shuffle(studentList);

            tester1.name = studentList[0];
            tester2.name = studentList[1];

            Assigner(tester1);
            Assigner(tester2);

            Console.WriteLine("{0}, {1}.",
                tester1.name, tester2.name);
            Console.WriteLine("{0}, {1}.",
                tester1.birthday, tester2.birthday);
            Console.WriteLine("{0}, {1}.",
                tester1.height, tester2.height);

            Console.ReadLine();
        }

        public static void Assigner(Students choice)
        {
            Students student1 = new Students("Jack", 0411, 155);
            Students student2 = new Students("Kelly", 0704, 179);
            Students student3 = new Students("Gabriel", 1212, 177);
            Students student4 = new Students("Irene", 0222, 169);
            Students student5 = new Students("Anderson", 0115, 162);

            if (choice.name == "student1")
            {
                choice.name = student1.name;
                choice.birthday = student1.birthday;
                choice.height = student1.height;
            }

            else if (choice.name == "student2")
            {
                choice.name = student2.name;
                choice.birthday = student2.birthday;
                choice.height = student2.height;
            }

            else if (choice.name == "student3")
            {
                choice.name = student3.name;
                choice.birthday = student3.birthday;
                choice.height = student3.height;
            }

            else if (choice.name == "student4")
            {
                choice.name = student4.name;
                choice.birthday = student4.birthday;
                choice.height = student4.height;
            }

            else if (choice.name == "student5")
            {
                choice.name = student5.name;
                choice.birthday = student5.birthday;
                choice.height = student5.height;
            }


        }

我猜这是因为 Shuffler 方法发生在另一个不起作用的方法中,因为那些有效的方法在方法之外完成了洗牌,但我不知道下一步该去哪里。我想让我的代码看起来更好一点,并使其更易于维护。如果我使用工作方法,我必须添加如此多的 ifs 条件,而不必输入对象和属性一次。

我能在这里做些什么,还是我的方法本身是错误的?我已经准备好了,渴望学习。谢谢。

C# 方法 属性

评论

0赞 David Conrad 7/21/2023
这不是问题,但应该称为.StudentsStudent

答:

0赞 David Conrad 7/21/2023 #1

问题是,无法修改 中的变量。为此,您需要使用参数:AssignerMainout

public static void Assigner(out Students aMulti1, out Students aMulti2)

并且您还必须在调用中指定:Assigner

        Assigner(out tester1, out tester2);

由于 和 的原始值将被覆盖,因此没有必要将它们分配给 student 对象,但您仍然需要声明它们:tester1tester2

        Students tester1;
        Students tester2;

当每个实例代表一个学生时,调用类确实没有意义,但我在这里保留了名称不变。Students

评论

0赞 kenLeeDep 7/21/2023
我根据您的建议修改了代码,它起作用了。谢谢。我是否可以进一步阅读以更好地理解它,因为“出局”从未出现在我观看的视频中。我还想问一下,我是否可以在一个单独的类中执行该方法,以及我的第四个代码块示例中的代码是否有效?
0赞 David Conrad 7/21/2023
@kenLeeDep 例如,您可以阅读有关 out 参数修饰符的文档。第四个代码块起作用的原因是它正在修改 and 对象,而不是尝试分配给它们。tester1tester2
0赞 David Conrad 7/21/2023
顺便说一句,如果你愿意,你可以通过单击复选标记来接受我的答案。
0赞 kenLeeDep 7/21/2023
明白了。还是这个网站的新手。谢谢你,也谢谢你的后续回答。