本文介绍了我可以传递参数给基类的构造从派生类的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 假设我有一个抽象基类甲板: 公共抽象类甲板 {公清单<卡>牌; 公共甲板(字符串[]值,字符串[]套装) {...} :} 和派生类EuchreDeck: 公共类EuchreDeck:甲板 {的String []值=新的String [] {9,10,J,Q,K,一个 }; 的String [] =适合新的字符串[] {俱乐部,黑桃,心,钻石}; 公共EuchreDeck():基地(值,西服)//错误。 {} :} 我希望能够实例化 EuchreDeck 并传递给基类,即 VAR gameDeck =新EuchreDeck()两个字符串数组; 。 目前我得到的错误:对象引用是必需的非静态字段,方法或属性 EuchreDeck.values 这是可能的,还是会调用派生默认构造函数总是调用基默认构造函数? 解决方案 是的,你可以,如果你做的阵列做静态: 公共类EuchreDeck:甲板 {私人静态只读的String []值=新的String [] {9,10 ,J,Q,K,A}; 私人静态只读的String [] =适合新的字符串[] {俱乐部,黑桃,心,钻石}; 公共EuchreDeck():基地(值,套装) { } } 为什么你曾与实例级别的会员,你不能使用它的原因是因为它不是合法的这样做。这个来自 C#规格10.10.1构造函数初始化它规定: 这是实例构造函数初始化不能访问创建的实例是。因此,它是在构造函数初始化的参数表达式来引用此编译时错误,因为它是一个参数表达式通过引用任何实例成员一个编译时错误一个简单的名称。 通过切换数组为静态,他们通过实例,而是由 EuchreDeck 的键入的 $不再访问b $ b 这是说,我可能会建议你采取的设计略微好办法。也许使用工厂,为您创造这些专门的甲板,而不是它们的构造 作为的例如的,也许重构是这样的: 更改你的基地甲板只取套牌: 公共抽象类甲板 {公开名单<卡>牌; 保护甲板(IEnumerable的<卡>卡) { this.Cards =新的List<卡>(卡); } } 然后回复到出厂设置是这样的: 公共类EuchreDeck:甲板 {私人EuchreDeck(IEnumerable的<卡>卡):基地(卡) { } 公共类工厂:DeckFactory {私人静态只读的String []值=新的String [] {9 ,10,J,Q,K,A}; 私人静态只读的String [] =西服新的字符串[] {俱乐部,黑桃,心,钻石}; 公共静态EuchreDeck的Create() { VAR卡= CreateCards(价值观,套); 返回新EuchreDeck(卡); } } } 实例化/用法: EuchreDeck.Factory.Create(); 您可以玩的工厂使用。我只是嵌套它的类,所以你不能创建一个 EuchreDeck 具有无效套牌。你的 DeckFactory 基础将有你的转换方法(它看起来像你现在在你的甲板构造函数) 除此之外,我不知道,如果你有一个特定需要的 EuchreDeck ;我假设你有一个与之关联的其他方法?如果没有,你很可能完全抛弃了阶级,只是让工厂创建一个甲板与所需的卡片。 Suppose I have an abstract base class Deck: public abstract class Deck{ public List<Card> cards; public Deck(string[] values, string[] suits) {...} ...}and a derived class EuchreDeck:public class EuchreDeck : Deck{ string[] values = new string[] { "9", "10", "J", "Q", "K", "A" }; string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public EuchreDeck() : base(values, suits) // Error. {} ...}I want the ability to instantiate EuchreDeck and have the two string arrays passed to the base class, i.e. var gameDeck = new EuchreDeck();.Currently I'm getting the error: "An object reference is required for the non-static field, method, or property EuchreDeck.values."Is this possible, or will calling the derived default constructor always call the base default constructor? 解决方案 Yes, you can do this if you make the arrays static:public class EuchreDeck : Deck{ private static readonly string[] values = new string[] { "9", "10", "J", "Q", "K", "A" }; private static readonly string[] suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public EuchreDeck() : base(values, suits) { }}The reason why you can't use it as you had with instance-level members is because it's not legal to do so. This comes from the C# specification 10.10.1 Constructor Initializers where it states: An instance constructor initializer cannot access the instance being created. Therefore it is a compile-time error to reference this in an argument expression of the constructor initializer, as is it a compile-time error for an argument expression to reference any instance member through a simple-name.By switching the arrays to be static, they are no longer accessed via the instance but rather by the EuchreDeck type.That said, I might suggest you take a slight tweak on the design. Maybe use a factory to create these specialized decks for you rather than their constructors.As an example, maybe refactor something like this:Change your base Deck to just take the set of cards:public abstract class Deck{ public List<Card> Cards; protected Deck(IEnumerable<Card> cards) { this.Cards = new List<Card>(cards); }}Then have the factory setup like this:public class EuchreDeck : Deck{ private EuchreDeck(IEnumerable<Card> cards) : base(cards) { } public class Factory : DeckFactory { private static readonly string[] Values = new string[] { "9", "10", "J", "Q", "K", "A" }; private static readonly string[] Suits = new string[] { "clubs", "spades", "hearts", "diamonds" }; public static EuchreDeck Create() { var cards = CreateCards(Values, Suits); return new EuchreDeck(cards); } }}Instantiation/usage as:EuchreDeck.Factory.Create();You could play around with the factory usage. I just nested it in the class so you couldn't create a EuchreDeck with an invalid set of cards. Your DeckFactory base would have your conversion method (which looks like you currently have in your Deck constructor)Beyond that, I'm not sure if you have a specific need for a EuchreDeck; I'm assuming you have other methods associated with it? If not, you could probably ditch the class altogether and just let the factory create a Deck with the needed cards. 这篇关于我可以传递参数给基类的构造从派生类的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 09:13
查看更多