【问题标题】:(C#) Access/Modify Objects in a List(C#) 访问/修改列表中的对象
【发布时间】:2017-02-02 14:16:30
【问题描述】:

新来的,我已经学习 c# 大约一个月了。

不管怎样,我已经搜索 StackOverflow 几天了,但找不到我的问题的具体答案...

//Here's my Class

public class Guy
{
     public static int ID { get; set; }
     public static int LifeExpectancy { get; set; }
     public static bool Living { get; set; }

     public Guy(int id, int lifeExpectancy, bool living)
     {
         ID = id;
         LifeExpectancy = lifeExpectancy;
         Living = living;
     }
}

我要做的是创建特定数量的“someGuy”对象,然后使用这种方法将它们放入公共列表中......

public static List<Guy> Guys = new List<Guy>();

public static void makeSomeGuys(int howManyGuys)
{
    for (int i = 0, i <= howManyGuys; i++)
    {
        int id = i;
        int lifeExpectancy = 80;
        bool alive = true;

        Guys.Add(New Guy(id, lifeExpectancy, alive));

        Console.WriteLine("Made a new Guy {0}", id);         
    }
    return; 
}

问题按重要性排序:

如何访问特定对象及其参数? (从“Guys”列表访问。)

如何在另一个类中访问此列表中的对象? (我不是绝对需要,我很好奇)

我可以使用参数在列表中搜索对象吗? (而不是像...humanPopulation[number]

我应该为修改了参数的对象创建一个新列表吗? (而不是将其留在原始列表中)

是否可以从列表中删除项目? (一般来说,这是人们做的事情吗?如果是,为什么?)

我真的只需要回答第一个问题。其余的只是奖金。谢谢!

【问题讨论】:

标签: c# list oop


【解决方案1】:

首先你需要从 Guy 类的属性中移除 static 修饰符,即:

public int ID { get; set; }
public int LifeExpectancy { get; set; }
public bool Living { get; set; }

因为 static 导致属性成为类本身的属性,而不是类的实例(个体“人”)。

获取第一个人(第零个人)的预期寿命:

Console.WriteLine(Guys[0].LifeExpectancy);

获取第五个人的预期寿命:

Console.WriteLine(Guys[4].LifeExpectancy);

【讨论】:

    【解决方案2】:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace test
    {
        public class Guy
        {
            private int m_ID;
            private int m_LifeExpectancy;
            private bool m_Living;
    
            public int ID
            {
                get { return m_ID; }
                set { m_ID = value; }
            }
            public int LifeExpectancy
            {
                get { return m_LifeExpectancy; }
                set { m_LifeExpectancy = value; }
            }
            public bool Living
            {
                get { return m_Living; }
                set { m_Living = value; }
            }
    
            public Guy(int id, int lifeExpectancy, bool living)
            {
                ID = id;
                LifeExpectancy = lifeExpectancy;
                Living = living;
            }
        }
    
        public class MyFactory
        {
            public IList<Guy> MakeSomeGuys(int howManyGuys)
            {
    
                IList<Guy> localGuys = new List<Guy>();
    
                for (int i = 0; i <= howManyGuys; i++)
                {
    
                    int id = i;
                    int lifeExpectancy = 80;
                    bool alive = true;
    
                    localGuys.Add(new Guy(id, lifeExpectancy, alive));
    
                    Console.WriteLine("Made a new Guy {0}", id);
                }
    
    
                return localGuys;
            }
        }
    
        public class program
        {
            public void Main()
            {
                MyFactory mf = new MyFactory();
                IList<Guy> guys = mf.MakeSomeGuys(5);
    
                //How do I access a specific object as well as its parameters? (Accessing from the list "Guys".)
                int GetFirstGuyId = guys.FirstOrDefault().ID; //LEARN LINQ
    
                //How do I access an object from this list in another class? (Not that I absolutely need to, I'm curious)
                //you need to learn about object oriented encapsulation for better understanding.
    
                //Can I search for an object in a list by using its parameters? (As opposed to doing something like...humanPopulation[number])
                Guy guyById = guys.Where(g => g.ID == 5).FirstOrDefault(); // returns the first match (need to learn lambda expression)
    
                //Should I create a new list for objects that have had their parameters modified? (As opposed to leaving it in the original list)
                // you need to learn about passing values by value / reference (by reference you already changing the original!).
    
                //Is it possible to remove items from a list? (Just in general, is that a thing people do? if so, why?)
                //yes
                guys.Remove(guyById);
    
            }
        }
    }
    

    【讨论】:

    • 第一个问题可以是:int firstGuyLifeExpectancy = guy[0].LifeExpectancy;
    • 我看不懂你写的代码的最上面的部分,你纠正我封装不当的部分。以你的方式做这件事有什么不同吗?还是刚刚好?
    • 这样你就可以控制你的输入/输出获取/设置,例如只有当它大于零时才设置值等等......
    • 而'value'就是m_ID的返回值,对吧?如果是这样,我完全理解,非常感谢您的帮助。
    • 是的,当您使用 set 时,值是您传递给属性并设置 m_ID 的值,get 是 m_ID 值的返回,欢迎您。
    【解决方案3】:

    你可能是 C# 和 OO 编程的新手,所以我在这个答案中包含了一些很好的链接。

    仅关于问题 1:

    首先,您的Guy 类属性未正确封装。确保您正确地确定了IDLifeExpectancyLiving 属性的范围,如this 文章中所示。

    如果您想访问一个特定项,即具有特定 ID 的 Guy,则最好使用像 Dictionary 这样的关联容器。

    如果您对List 容器感到满意,则需要在Guys 上使用Find 方法,如链接中的示例所示。您会注意到文档中的术语 谓词this 链接将详细说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多