简体   繁体   English

c#如何在函数中使用实例化对象的属性(列表)?

[英]c# How to use an instanciated object's property(which is a list) in a function?

I have two classes. 我有两节课。 A Player Class that has the properties Name, Balance and Cards: 具有属性名称,余额和纸牌的玩家职业:

class Player
{
    public string Name;
    public decimal Balance;
    public List<Card> Cards; // My question is related to this property
}

And a Card class: 和卡片类:

class Card
{
    public string Name;
    public int Attack;
    public int Defense;
    public decimal Cost;
}

I instantiated an object for the player: 我为播放器实例化了一个对象:

var player = new Player("Ahmad", 100);

and also a card: 还有一张卡:

var card = new Card("Mino", 3, 3, 200);

then added the card to the player's Cards: 然后将该卡添加到玩家的卡中:

player.Cards[0] = card;

I can simply display it on the main method where it was instantiated but the Problem is : How can I access the player's Cards in a function? 我可以简单地在实例化它的主要方法上显示它,但是问题是 :如何在功能中访问玩家的牌? like so: 像这样:

///<summary>
/// Function to show current cards
/// </summary>
public static void ViewCards(object player)
{
    var cards = player.Cards  // This line doesn't work
    foreach (var card in cards)
    {
        Console.WriteLine($"{card.Name}");
        Console.WriteLine($"Attack: {card.Attack}");
        Console.WriteLine($"Defense: {card.Defense}");
        Console.WriteLine($"Cost: ${card.Cost}");
    }    
}

Change the method signature Change the player parameter , from Object to Player 更改方法签名将Player参数从Object更改为Player

public static void ViewCards(Player player)
{
}

If you want to maintain a more OO approach you could make it a method on the player. 如果您想维持一种更加面向对象的方法,则可以将其设为播放器上的一种方法。

class Player
{
    public string Name {get;set;}
    public decimal Balance {get;set;}
    public List<Card> Cards {get;set;}

    public void ViewCards()
    {
        foreach (var card in Cards)
        {
            Console.WriteLine($"{card.Name}");
            Console.WriteLine($"Attack: {card.Attack}");
            Console.WriteLine($"Defense: {card.Defense}");
            Console.WriteLine($"Cost: ${card.Cost}");
        }    
    }
}

And then on some method 然后用某种方法

player.ViewCards();

Also you should not use public fields, instead use a Property. 另外,您不应使用公共字段,而应使用属性。 This promotes better encapsulation and class cohesiveness. 这将促进更好的封装和类内聚性。 I am referring here to Name , Balance , and Cards . 我在这里指的是NameBalanceCards The get; get; and set; set; will create a backing field during compile time. 将在编译期间创建一个后备字段。

You need to cast the object to class Player 您需要将对象强制转换为Player类

///<summary>
/// Function to show current cards
/// </summary>
public static void ViewCards(object player)
{
    var cards = (player as Player)?.Cards;  // cast object to Player, returs null if object is not of correct type

    if (cards != null)
    {
       foreach (var card in cards)
       { 
           Console.WriteLine($"{card.Name}");
           Console.WriteLine($"Attack: {card.Attack}");
           Console.WriteLine($"Defense: {card.Defense}");
           Console.WriteLine($"Cost: ${card.Cost}");
       }  
    }    
}

If you don't want to specify object type, you can use dynamic 如果您不想指定对象类型,则可以使用动态

public static void ViewCards(dynamic player)
{
    var cards = player.Cards  // This line doesn't work
    foreach (var card in cards)
    {
        Console.WriteLine($"{card.Name}");
        Console.WriteLine($"Attack: {card.Attack}");
        Console.WriteLine($"Defense: {card.Defense}");
        Console.WriteLine($"Cost: ${card.Cost}");
    }    
}

But you may have an exception on runtime if your object doesn't have a property Cards 但是,如果您的对象没有属性Cards,则在运行时可能会出现异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 function 中将对象列表作为参数传递,然后使用对象的属性 C# - How to pass list of objects as parameter in function and then use object's attributes C# 使用 C# 反射,如果该对象是列表内的对象的属性,则如何获取该对象的属性及其值 - Using C# Reflection, how to get Object's properties and their values if that Object is a property of Object that is inside of the List C# 使用对象的属性而不是 List.Contains() 的引用 - C# Use object's property rather than reference for List.Contains() 如何使用C#对象数组作为属性 - How to use a C# Object Array as a Property C# 如何在 object 列表中使用变量作为属性名称 - C# How to use variables as property names in an object List object constructor for key-value string arrays C#如何将对象添加到具有对象列表的列表中 - C# how to add object to a list which has list of object C# - 按List中的Object属性排序<T> - C# - Order by an Object's property in List<T> 打印对象的List &lt;&gt;属性值C# - Print object's List<> property value C# 通过C#中另一个对象的属性对对象列表进行排序 - Ordering a list of objects by another object's property in C# 如何使用C#在.Net中的类型对象列表中选择对象属性的所有值 - How to select all the values of an object's property on a list of typed objects in .Net with C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM