constructor

Shortcut for subclassing in Scala without repeating constructor arguments?

懵懂的女人 提交于 2019-12-22 03:40:42
问题 Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) { // Implementation } class TestView(writer: XMLStreamWriter) extends View(writer) { // Implementation } Most subclasses of View are not going to take different constructor arguments. I would like to be able to write something like this: class TestView extends View { // Implementation } Is there some shortcut to write subclasses so that you don't have to explicitly define the constructor args and pass

How to call both super(…) and this(…) in case of overloaded constructors?

*爱你&永不变心* 提交于 2019-12-22 03:15:15
问题 I've never needed to do this before but since both have to be the 'first' line in the constructor how should one tackle it? What's the best refactoring for a situation like this? Here's a sample: public class Agreement extends Postable { public Agreement(User user, Data dataCovered) { super(user); this(user,dataCovered,null); } public Agreement(User user,Data dataCovered, Price price) { super(user); if(price!=null) this.price = price; this.dataCovered = dataCovered; } ... } The call to super

Initialization in polymorphism of variables

ⅰ亾dé卋堺 提交于 2019-12-22 02:06:08
问题 Suppose you have the following code class A { int i = 4; A() { print(); } void print () { System.out.println("A"); } } class B extends A { int i = 2; //"this line" public static void main(String[] args){ A a = new B(); a.print(); } void print () { System.out.println(i); } } this will print 0 2 Now, if you remove line labeled "this line" the code will print 4 4 I understand that if there was no int i=2; line, A a = new B(); will call class A, initializes i as 4, call constructor, which gives

Initialization in polymorphism of variables

巧了我就是萌 提交于 2019-12-22 02:06:07
问题 Suppose you have the following code class A { int i = 4; A() { print(); } void print () { System.out.println("A"); } } class B extends A { int i = 2; //"this line" public static void main(String[] args){ A a = new B(); a.print(); } void print () { System.out.println(i); } } this will print 0 2 Now, if you remove line labeled "this line" the code will print 4 4 I understand that if there was no int i=2; line, A a = new B(); will call class A, initializes i as 4, call constructor, which gives

Relevance of 'public' constructor in abstract class

我只是一个虾纸丫 提交于 2019-12-22 01:39:24
问题 Is there any relevance of a 'public' constructor in an abstract class? I can not think of any possible way to use it, in that case shouldn't it be treated as error by compiler (C#, not sure if other languages allow that). Sample Code: internal abstract class Vehicle { public Vehicle() { } } The C# compiler allows this code to compile, while there is no way i can call this contructor from the outside world. It can be called from derived classes only. So shouldn't it allow 'protected' and

Different way of creating a Javascript Object?

旧时模样 提交于 2019-12-22 01:34:55
问题 I'm learning to code and I have a question about some sample code I found: var Comment = new Schema({ user:userStub, time:Date, content: {type:String, required: true, trim:true} }); From what I learned about OOP, I thought the Comment object instance of Schema would be built like this: function Schema (user, time, content){ this.user = user; this.time = time; this.content = content; }; var Comment = new Schema (userStub, time, content); Anyone know the advantage of building the Comment

Does the Project Lombok @Data annotation create a constructor of any kind?

情到浓时终转凉″ 提交于 2019-12-22 01:25:39
问题 I have a class with a @Data annotation, but I'm not sure whether a constructor with arguments is generated or the only generated constructor is the default (no arguments) one from vanilla Java. 回答1: A @RequiredArgsConstructor will be generated if no constructor has been defined. The Project Lombok @Data page explains: @Data is like having implicit @Getter, @Setter, @ToString, @EqualsAndHashCode and @RequiredArgsConstructor annotations on the class (except that no constructor will be generated

templated conversion constructor fails to access protected data members

喜你入骨 提交于 2019-12-22 01:14:29
问题 I have a templated class Rect with conversion constructor which allows conversion between Rect to Rect and vice a versa. But when compiling the code, the compiler gives an error stating the constructor can't access the protected members of the class. Here is code: #include <iostream> #include <list> #include <algorithm> using namespace std; template< typename T > class Rect{ protected: T width, height; public: Rect(T a, T b){ width = a; height = b; } template< typename U > Rect(Rect<U> const

Can the constructor abort instantiation?

爷,独闯天下 提交于 2019-12-21 23:35:51
问题 I want to make tests in a constructor to find out if it is currently a good idea to instantiate the object or not, with the given parameters. But how could I abort and return a warning from a constructor to the new statement? Must such tests instead be done by the caller before each "new" statement? I thought that the constructor would be a good place for it. 回答1: You could use a factory object instead. This could then run your checks and return the instansiated object, or null. This would

Is it good or bad to delegate to another constructor (using this()) within a constructor

丶灬走出姿态 提交于 2019-12-21 20:34:11
问题 Oracle reference doesn't tell about best practice of this keyword while we overload constructors. Can anyone suggest the best practice for it? Option 1: delegate to another constructor public class A { private int x, y, z, p; public A() { this(1,1,1,1); } public A(int x, int y, int z, int p) { this.x = x; this.y = y; this.z = z; this.p = p; } } and Option 2: set each field rather than delegating public class A { private int x, y, z, p; public A() { this.x = 1; this.y = 1; this.z = 1; this.p =