猿问

C#如何从类函数中访问兄弟类?

我的代码结构如下所示。我只用提出问题的严格要求来简化它。


我有一个 Parent 类,其中包括一个 Item1 类、Item2 类、Item1_to_Item2_relationship 类。由于与此问题无关的原因,我必须保留此结构。


如何从 Item2 访问 Item1 中的值?该代码更好地解释了需要做什么。


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApp3

{

    class Program

    {

        static void Main(string[] args)

        {

            Parent parent = new Parent();


            // Adding item1 values

            Item1 item1_01 = new Item1();

            item1_01.id = "item1_01";

            item1_01.item1_code = "CODE_01";

            parent.items1.Add(item1_01);

            Item1 item1_02 = new Item1();

            item1_02.id = "item1_02";

            item1_02.item1_code = "CODE_02";

            parent.items1.Add(item1_02);


            // Adding item2 values

            Item2 item2 = new Item2();

            item2.id = "item2_01";

            parent.items2.Add(item2);


            // Adding relationships

            Item1_to_Item2_Relationship item1_to_Item2_Relationship = new Item1_to_Item2_Relationship();

            item1_to_Item2_Relationship.item1.id_alt = item1_01.id;

            item1_to_Item2_Relationship.item2.id_alt = item2.id;

            parent.Item1_to_Item2_Relationships.Add(item1_to_Item2_Relationship);


            item1_to_Item2_Relationship = new Item1_to_Item2_Relationship();

            item1_to_Item2_Relationship.item1.id_alt = item1_02.id;

            item1_to_Item2_Relationship.item2.id_alt = item2.id;

            parent.Item1_to_Item2_Relationships.Add(item1_to_Item2_Relationship);


            // How to make the code below return a List<string> with the values "CODE_01" and "CODE_02"?

            foreach (Item2 my_item2 in parent.items2)

            {

                my_item2.item1_codes;

            }



        }

    }


目前,我必须纠正一个接收 Parent 参数的静态函数并在那里执行逻辑。但我相信应该有更好更直观的方式。我怎样才能使上面的代码工作?


DIEA
浏览 136回答 3
3回答

白衣非少年

如其他答案中所述,您可以直接访问它。既然你在维持一段关系,我假设你想通过这段关系来做到这一点&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var test = new List<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Item2 my_item2 in parent.items2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var item in parent.Item1_to_Item2_Relationships)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //implement your own equality comparision if it should be differernt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (item.item2.id_alt == my_item2.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.Add(item.item1.item1_code);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }几个重要的点:您正在处理列表和嵌套循环,该方法的性能不是最好的。为了提高性能,应明智地选择关系数据结构,是否可以将其转换为Dictionary,从而提供更快的查找时间。由于您没有在关系中添加代码,因此代码当前将添加空白字符串,但是您始终可以使用 id 搜索对象,这意味着在列表中进行另一个搜索,这就是我说您可能想要更改的原因底层数据结构。如果它们的项目代码是主键,那么将单个对象也存储在字典中可能会更好。

森林海

最快和最简单的方法是传入parent需要itemX从itemY.例如:class Item1{&nbsp; &nbsp; public string id;&nbsp; &nbsp; public string id_alt;&nbsp; &nbsp; public string item1_code;&nbsp; &nbsp; public Item1()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id = "";&nbsp; &nbsp; &nbsp; &nbsp; item1_code = "";&nbsp; &nbsp; }}class Item2{&nbsp; &nbsp; public string id;&nbsp; &nbsp; public string id_alt;&nbsp; &nbsp; public Item2()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; id = "";&nbsp; &nbsp; }&nbsp; &nbsp; public void AccessItem1Prop(Parent parent) {&nbsp; &nbsp; &nbsp; &nbsp; string item1ID = parent.items1[0].id;&nbsp; &nbsp; }}您也可以传入parent单个项目的构造函数并将其作为对象属性附加到项目,但如果您可以简单地保持事情清洁,我会避免这种方法。

呼如林

我不喜欢代码的结构......但这是简单的答案。您正在将两种不同的项目类型添加到两个不同的集合中。您正在制作一个关系项目,它根据 id 将两者联系起来。尽管您的代码尖叫有助于这个想法是合乎逻辑的并且是关联项目(例如使用数据库)的好方法。我们可以用 Linq 简单地做到这一点,但我宁愿更接近你是如何写这个问题的。首先像你正在做的那样迭代你的项目,然后迭代你的关系。然后根据 Id 比较迭代其他项目,并根据最终的 Id 比较得到答案。foreach (Item2 my_item2 in parent.items2){&nbsp; &nbsp; foreach (Item1_to_Item2_Relationship relatedItem in parent.Item1_to_Item2_Relationships)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (my_item2.id == relatedItem.item2.id_alt)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Item1 item1 in parent.items1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (relatedItem.item1.id_alt == item1.id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(item1.item1_code);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}//Outputs//CODE_01//CODE_02
随时随地看视频慕课网APP
我要回答