constructor-overloading

C# constructors overloading

余生颓废 提交于 2019-11-26 16:57:39
问题 How I can use constructors in C# like this: public Point2D(double x, double y) { // ... Contracts ... X = x; Y = y; } public Point2D(Point2D point) { if (point == null) ArgumentNullException("point"); Contract.EndContractsBlock(); this(point.X, point.Y); } I need it to not copy code from another constructor... 回答1: You can factor out your common logic to a private method, for example called Initialize that gets called from both constructors. Due to the fact that you want to perform argument

Constructor overloading in Java - best practice

ぃ、小莉子 提交于 2019-11-26 07:26:26
There are a few topics similar to this, but I couldn't find one with a sufficient answer. I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more advice. I'm referring to both constructor overloading in a simple class and constructor overloading while inheriting an already overloaded class (meaning the base class has overloaded constructors). Thanks :) While there are no "official guidelines" I follow the principle of KISS and DRY. Make the overloaded constructors as simple as possible, and the

Constructor overloading in Java - best practice

佐手、 提交于 2019-11-26 01:58:20
问题 There are a few topics similar to this, but I couldn\'t find one with a sufficient answer. I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I\'d like to hear more advice. I\'m referring to both constructor overloading in a simple class and constructor overloading while inheriting an already overloaded class (meaning the base class has overloaded constructors). Thanks :) 回答1: While there are no "official