在ArrayList中打印对象成员变量 [英] Print an Objects Member Variable iin an ArrayList

查看:52
本文介绍了在ArrayList中打印对象成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个ArrayList清单",其中填充了不同的,用户定义的类型的各种项目".所有这些项目,无论类型如何,都具有一个成员变量,该成员变量是私有String _description.成员变量是具有相同名称-描述的属性的后备存储.这是一个项目的示例:

Hey guys,

I have an ArrayList "inventory" that is populated with a variety of "items" that are of different, user-defined types. All of these items, regardless of type, have a member variable that is a private String _description. The member variable is a backing store for a property of the same name - Description. Here is an example of an item:

public class Weapon
{ 
private String _description;

public Weapon (String description)
{
_description = description;
}
public Description{get; set;}
}



我需要做的是填充一系列项目"对象,这些对象通过其构造函数初始化为不同的描述,然后通过foreach循环调用这些描述以将其打印到多行文本框中.



What I need to do is stuff a series of "item" objects that are initialized through their constructor to different descriptions and then call those descriptions through a foreach loop to be printed to a multi-line textbox.

foreach(var object in inventory)
{

}



但老实说,我不知道该如何做才能深入到ArrayList中保存的对象并访问每个对象的属性以进行打印.

其背后的想法是,使客户可以查询库存的内容并将其打印在屏幕上,而无需知道库存中物品的数量或类型.

我们将不胜感激.



But I honestly don''t know what to do in order to drill down to the objects that are held in ArrayList and access each object''s property to print.

The idea behind this is so that a client can poll for the contents of the inventory and have them printed out on the screen without knowing the number or type of item in the inventory.

Any help would be appreciated.

推荐答案

您应该做的是创建一个接口,并使它公开Description属性.然后,让您想要在数组中的每个类都实现此接口.用实现您的接口的对象填充您的数组,您可以简单地调用Description属性,因为它们都具有相同的特性:)

What you should do is create an Interface and have it expose the Description Property. Then have every Class you want in your Array Implement this Interface. Fill your array with Objects that Implement your Interface and you can simply call the Description Property because they all have it in common :)

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IItem[] inventory = new IItem[3];
            inventory[0] = new Weapon("Sword");
            inventory[1] = new CoffeeMachine("Senseo");
            inventory[2] = new Weapon("Spear");

            foreach (IItem item in inventory)
            {
                Console.WriteLine(item.Description);
            }

            Console.ReadKey();
        }
    }

    interface IItem
    {
        string Description { get; }
    }

    class Weapon : IItem
    {

        private string _description;

        public Weapon(string description)
        {
            _description = description;
        }

        public string Description
        {
            get { return _description; }
        }

        public void HitInTheHead()
        {
            // Do Something.
        }
    }

    class CoffeeMachine : IItem
    {

        private string _description;

        public CoffeeMachine(string description)
        {
            _description = description;
        }

        public string Description
        {
            get { return _description; }
        }

        public void MakeCoffe()
        {
            // Do Something.
        }
    }
}



注意接口IItem.武器和CoffeeMachine均实现此接口.您现在可以制作一个数组(为什么不使用通用列表 [



Notice the Interface IItem. Both Weapon and CoffeeMachine Implement this Interface. You can now make an Array (why not use Generic List[^]?) of IItem and put anything in it that Implements IItem. You can then iterate through the items and access the one thing they all have in common: the Description Property they MUST Implement because of the Interface :)
Also notice that you cannot get the MakeCoffee and HitInTheHead Methods this way. Your Array only knows that its items are of type IItem and IItem only has the Description Property.
The code above is a working Console App. You can copy/paste it to test.
Good luck!




您说每个项目都有Decription属性.因此,一种方法是拥有一个界面,而您的每个商品都可以实现该界面.像这样的东西:

Hi,

You said each item has Decription property. so one way is to have an interface and each of your item, impelemtns that interface. some thing like this:

public interface ISameDescription
{
string Description {get;set;}
}

public class Weapon:ISameDescription
{
private String _description;

public Weapon (String description)
{
_description = description;
}
public Description{get{} set{}}
}



现在,在您的foreach语句中,您可以看到类似以下内容的内容:



Now in your foreach statement you can have something like:

foreach(ISameDescription o in inventory)
{
 // To Do something with o.Description
}



如果您不想更改代码,则可以利用invoke.
像这样的东西:



if you don''t wish to change your code, you can take advantage of invoke.
some thing like this:

foreach(object o in inventory)
{
  o.GetType().InvokeMember("Description", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public, null, o, null);
}




希望这能对您有所帮助.




hope this can help you.


我想我可能已经丢失了一些东西,但是我认为这很简单:

I guess I might have been missing something, but I think this is as simple as:

for(int i=0; i < inventory.Count; i++){
    Console.Write("{0}", inventory[i].Description);
}



显然,您需要将Console.Write更改为您使用的打印方法.



Obviously you would need to change Console.Write to the print method you use.


这篇关于在ArrayList中打印对象成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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