如何在构造函数中实现继承的属性?
How to implement an inherited property in a constructor?
class Player
继承自 Deckholder
,其中 属性 Deck
已被声明。
public class Deckholder
{
public Deck Deck { get; set; }
public Deckholder(Deck deck)
{
Deck = deck;
}
}
public class Player : Deckholder
{
public string Name { get; set; }
public int Score { get; set; }
public Player(string name, Deck deck)
{
Deck = deck;
Name = name;
Score = 0;
}
VSCode 告诉我,
No argument that fits the formal parameter "deck" from "Deckholder.Deckholder(Deck) was found."
在 Player
-构造函数下有红色波浪线。但是我该如何解决呢?我的意思是我确实有一个 Deck deck
作为参数,我需要它。
您必须从 Player
class 的构造函数中调用 Deckholder
class 的构造函数。为此使用 base
关键字:
public Player(string name, Deck deck) : base(deck)
{
Name = name;
Score = 0;
}
您不必再在 Player
class 中分配 deck
,因为它是在基本构造函数中完成的。
class Player
继承自 Deckholder
,其中 属性 Deck
已被声明。
public class Deckholder
{
public Deck Deck { get; set; }
public Deckholder(Deck deck)
{
Deck = deck;
}
}
public class Player : Deckholder
{
public string Name { get; set; }
public int Score { get; set; }
public Player(string name, Deck deck)
{
Deck = deck;
Name = name;
Score = 0;
}
VSCode 告诉我,
No argument that fits the formal parameter "deck" from "Deckholder.Deckholder(Deck) was found."
在 Player
-构造函数下有红色波浪线。但是我该如何解决呢?我的意思是我确实有一个 Deck deck
作为参数,我需要它。
您必须从 Player
class 的构造函数中调用 Deckholder
class 的构造函数。为此使用 base
关键字:
public Player(string name, Deck deck) : base(deck)
{
Name = name;
Score = 0;
}
您不必再在 Player
class 中分配 deck
,因为它是在基本构造函数中完成的。