[英]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
. 我在这里指的是
Name
, Balance
和Cards
。 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.