constructor

When to use Long vs long in java?

╄→гoц情女王★ 提交于 2019-12-18 12:11:42
问题 Below is my Interface - public interface IDBClient { public String read(ClientInput input); } This is my Implementation of the Interface - public class DatabaseClient implements IDBClient { @Override public String read(ClientInput input) { } } Now I have a factory which gets the instance of DatabaseClient like this - IDBClient client = DatabaseClientFactory.getInstance(); .... Now I need to make a call to read method of my DatabaseClient which accepts the ClientInput parameter and below is

Delphi/pascal: overloading a constructor with a different prototype

半腔热情 提交于 2019-12-18 11:47:30
问题 I'm trying to create a child class of TForm with a special constructor for certain cases, and a default constructor that will maintain compatibility with current code. This is the code I have now: interface TfrmEndoscopistSearch = class(TForm) public /// original constructor kept for compatibility constructor Create(AOwner : TComponent); overload; override; /// additional constructor allows for a caller-defined base data set constructor Create(AOwner : TComponent; ADataSet : TDataSet;

Call constructor on TypeScript class without new

▼魔方 西西 提交于 2019-12-18 11:47:19
问题 In JavaScript, I can define a constructor function which can be called with or without new : function MyClass(val) { if (!(this instanceof MyClass)) { return new MyClass(val); } this.val = val; } I can then construct MyClass objects using either of the following statements: var a = new MyClass(5); var b = MyClass(5); I've tried to achieve a similar result using the TypeScript class below: class MyClass { val: number; constructor(val: number) { if (!(this instanceof MyClass)) { return new

Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class?

安稳与你 提交于 2019-12-18 11:36:21
问题 So, the question is more or less as I wrote. I understand that it's probably not clear at all so I'll give an example. I have class Tree and in it there is the class Node, and the empty constructor of Tree is written: public class RBTree { private RBNode head; public RBTree(RBNode head,RBTree leftT,RBTree rightT){ this.head=head; this.head.leftT.head.father = head; this.head.rightT.head.father = head; } public RBTree(RBNode head){ this(head,new RBTree(),new RBTree()); } public RBTree(){ this

Are default constructors called automatically for member variables?

两盒软妹~` 提交于 2019-12-18 11:27:27
问题 Say I have this class: //Awesome.h class Awesome { public: Awesome(); private: membertype member; } //Awesome.cpp #include "Awesome.h" Awesome::Awesome() :member() { } If I omit the member() in the initialization list of the constructor of Awesome , will the constructor of member be called automatically? And is it only called when I don't include member in the initialization list? 回答1: From § 8.5 If no initializer is specified for an object, the object is default-initialized; if no

best way to create object

末鹿安然 提交于 2019-12-18 11:03:18
问题 This seems to be very stupid and rudimentary question, but i tried to google it, but couldn't a find a satisfactory answer, public class Person { public string Name { get; set; } public int Age { get; set; } public Person(){} public Person(string name, int age) { Name = name; Age = age; } //Other properties, methods, events... } My question is if i have class like this, what is the best way to create an object? Person p=new Person("abc",15) OR Person p=new Person(); p.Name="abc"; p.Age=15;

How to handle 'this' pointer in constructor?

点点圈 提交于 2019-12-18 10:58:21
问题 I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw pointers. So the child would have code such as shared_ptr<Parent> , and boost provides the shared_from_this() method which the parent can give to the child. My problem is that shared_from_this() cannot be used in a constructor, which isn't really a crime

Constructor with all class properties or default constructor with setters?

随声附和 提交于 2019-12-18 10:52:49
问题 Following are the two approaches: constructor with all the class properties Pros: I have to put an exact number of types of parameters so if I make an error the compiler warns me (by the way, is there a way to prevent the problem of having erroneously switched two Integer on the parameter list?) Cons: if I have lots of properties the instantiation line can become really long and it could span over two or more lines setters and the default empty constructor Pros: I can clearly see what I'm

What does a constructor return in Java?

旧巷老猫 提交于 2019-12-18 10:42:48
问题 After going on the post on this topic I found myself little confused. So again I am asking this: "Does Java constructor returns any value?" My books say they can't return a value, but my professor says they can and they are always doing so. As the control needs to be transferred to someone with some value either void? 回答1: This is a little confusing: constructors indeed do not return a value; it is operator new that does. However, constructors are always used with a new * , so it looks like

How to override constructor of Python class with many arguments?

守給你的承諾、 提交于 2019-12-18 10:34:19
问题 Say, I have a class Foo, extending class Bar. And I want to slightly override Foo's consructor. And I don't want even know what signarure of Bar's constructors is. Is there a way to do this? If you didn't understand, I mean the following: class Bar: def __init__ (self, arg1=None, arg2=None, ... argN=None): .... class Foo (Bar): #Here i just want add additional parameter to constructor, but don't want to know anything about Bar's other parameters (arg1, arg2..., argN) def __init__ (self, my