multiple-constructors

How to simplify multiple constructors?

北城以北 提交于 2021-02-16 08:40:13
问题 I would like to have two constructors for a class, as follows: public MyClass() { // do stuff here } public MyClass(int num) { MyClass(); // do other stuff here } Is the above the correct way to achieve my purpose? Is there some kind of shorthand which is better? 回答1: public MyClass() { // do stuff } public MyClass(int num) : this () { // do other stuff with num } The : this() bit is called a Constructor Initialiser . Every constructor in C# has a an initialiser which runs before the body of

How to simplify multiple constructors?

[亡魂溺海] 提交于 2021-02-16 08:35:47
问题 I would like to have two constructors for a class, as follows: public MyClass() { // do stuff here } public MyClass(int num) { MyClass(); // do other stuff here } Is the above the correct way to achieve my purpose? Is there some kind of shorthand which is better? 回答1: public MyClass() { // do stuff } public MyClass(int num) : this () { // do other stuff with num } The : this() bit is called a Constructor Initialiser . Every constructor in C# has a an initialiser which runs before the body of

javascript: different constructors for same type of object

浪子不回头ぞ 提交于 2019-12-22 05:28:26
问题 is it possible to have more than one constructors for a class in javascript? i.e. one with zero parameters, one with one, one with two, etc... if so, how? thanks! 回答1: No, Javascript does not support function overloading. However, inside every function you have access to an arguments object, which holds all the arguments supplied to the function, declared or not. You can simply look at it and decide what exactly you want to do in your constructor. Bad, unrefined example: function Foo() {

MEF Constructor Parameters with Multiple Constructors

﹥>﹥吖頭↗ 提交于 2019-12-19 06:27:06
问题 I'm starting to use MEF, and I have a class with multiple constructors, like this: [Export(typeof(ifoo))] class foo : ifoo { void foo() { ... } [ImportingConstructor] void foo(object par1) { ... } } I am using catalog.ComposeExportedValue() when composing to supply the par1 value to second constructor: ... catalog.ComposeExportedValue(par1Value); catalog.ComposeParts(this); ... To hold the components I'm using: [ImportMany(typeof(ifoo))] public List<Lazy<ifoo, ifoometadata>> FooList { get;

Best way to do multiple constructors in PHP

让人想犯罪 __ 提交于 2019-12-17 02:28:09
问题 You can't put two __construct functions with unique argument signatures in a PHP class. I'd like to do this: class Student { protected $id; protected $name; // etc. public function __construct($id){ $this->id = $id; // other members are still uninitialized } public function __construct($row_from_database){ $this->id = $row_from_database->id; $this->name = $row_from_database->name; // etc. } } What is the best way to do this in PHP? 回答1: I'd probably do something like this: <?php class Student

inheritance Problems with undefined types

。_饼干妹妹 提交于 2019-12-13 05:07:22
问题 I want to be able to create a constructor that can call on its types but without any constraints. public class Box { public class Command { public string name; public string num; public Object PARAMS { get; set; }//<--- HERE } } I want PARAMS to be an undefined type that could possibly call one of these other classes public class Person:Box { public string LastName { get; set; } public string FirstName { get; set; } } or public class Item:Box { public string ItemName { get; set; } public

javascript: different constructors for same type of object

匆匆过客 提交于 2019-12-05 08:22:05
is it possible to have more than one constructors for a class in javascript? i.e. one with zero parameters, one with one, one with two, etc... if so, how? thanks! No, Javascript does not support function overloading. However, inside every function you have access to an arguments object , which holds all the arguments supplied to the function, declared or not. You can simply look at it and decide what exactly you want to do in your constructor. Bad, unrefined example: function Foo() { function singleParamConstructor(foo) { ... } function twoParamConstructor(foo, bar) { ... } switch (arguments

MEF Constructor Parameters with Multiple Constructors

守給你的承諾、 提交于 2019-12-01 04:15:24
I'm starting to use MEF, and I have a class with multiple constructors, like this: [Export(typeof(ifoo))] class foo : ifoo { void foo() { ... } [ImportingConstructor] void foo(object par1) { ... } } I am using catalog.ComposeExportedValue() when composing to supply the par1 value to second constructor: ... catalog.ComposeExportedValue(par1Value); catalog.ComposeParts(this); ... To hold the components I'm using: [ImportMany(typeof(ifoo))] public List<Lazy<ifoo, ifoometadata>> FooList { get; set; } And to create the foo instance I'm using the value property, FooList[0].Value . Everthing works

In Scala, how can I subclass a Java class with multiple constructors?

蓝咒 提交于 2019-11-27 10:43:48
Suppose I have a Java class with multiple constructors: class Base { Base(int arg1) {...}; Base(String arg2) {...}; Base(double arg3) {...}; } How can I extend it in Scala and still provide access to all three of Base's constructors? In Scala, a subclass can only call one of it's superclass's constructors. How can I work around this rule? Assume the Java class is legacy code that I can't change. It's easy to forget that a trait may extend a class. If you use a trait, you can postpone the decision of which constructor to call, like this: trait Extended extends Base { ... } object Extended { def

In Scala, how can I subclass a Java class with multiple constructors?

百般思念 提交于 2019-11-26 15:18:11
问题 Suppose I have a Java class with multiple constructors: class Base { Base(int arg1) {...}; Base(String arg2) {...}; Base(double arg3) {...}; } How can I extend it in Scala and still provide access to all three of Base's constructors? In Scala, a subclass can only call one of it's superclass's constructors. How can I work around this rule? Assume the Java class is legacy code that I can't change. 回答1: It's easy to forget that a trait may extend a class. If you use a trait, you can postpone the