Subclass vs Wrapper - Constructor With An Additional Parameter

匆匆过客 提交于 2019-12-11 10:43:01

问题


Which is generally considered the more preferred method when trying to add a constructor with an additional parameter? A subclass or a wrapper? That being, creating a subclass of the class and then just using that subclass' constructor? Or adding a wrapper method which will take the extra parameter and return an object with that parameter set?

Thank you for your time!

EDIT:

I don't have access to the superclass's code.


回答1:


The answer is language dependent. In C#/.NET, you would typically use an overloaded constructor:

public class Foo 
{
   private readonly string _greeting;

   public Foo() : this("Hello") { }

   public Foo(string greeting) {
     _greeting = greeting;
   } 

   //...
}


来源:https://stackoverflow.com/questions/13383254/subclass-vs-wrapper-constructor-with-an-additional-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!