constructor

what type of java constructors are these? Constructor chaining?

為{幸葍}努か 提交于 2019-12-29 07:19:08
问题 These are from the spring amqp samples on github at https://github.com/SpringSource/spring-amqp-samples.git what type of java constructors are these? are they a short hand for getters and setters? public class Quote { public Quote() { this(null, null); } public Quote(Stock stock, String price) { this(stock, price, new Date().getTime()); } as oppossed to this one public class Bicycle { public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence;

Scala: curried constructors

喜夏-厌秋 提交于 2019-12-29 06:41:13
问题 I have the following Scala class: class Person(var name : String, var age : Int, var email : String) I would like to use the Person constructor as a curried function: def mkPerson = (n : String) => (a : Int) => (e : String) => new Person(n,a,e) This works, but is there another way to accomplish this? This approach seems a bit tedious and error-prone. I could imagine something like Function.curried, but then for constructors. 回答1: This will work: def mkPerson = (new Person(_, _, _)).curried

How can I set the value of auto property backing fields in a struct constructor?

ε祈祈猫儿з 提交于 2019-12-29 06:38:11
问题 Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty) { this.StringProperty = stringProperty; this.IntProperty = intProperty; } public String StringProperty { get; set; } public Int32 IntProperty { get; set; } } Of course, a compiler error is generated that reads The 'this' object cannot be used before all of its fields are assigned to . Is there a way to assign values to the backing fields or the properties themselves, or do I have

Pass current object type into base constructor call

♀尐吖头ヾ 提交于 2019-12-29 05:27:31
问题 How do I grab the Type of the inherited class and pass it into the base constructor of the class also inherited? See the code sample below: // VeryBaseClass is in an external assembly public abstract class VeryBaseClass { public VeryBaseClass(string className, MyObject myObject) { } } // BaseClass and InheritedClass are in my assembly public abstract class BaseClass : VeryBaseClass { public BaseClass(MyObject myObject) : base(this.GetType().Name, myObject) // can't reference "this" (Type

Why const for implicit conversion?

拥有回忆 提交于 2019-12-29 04:42:05
问题 After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following #include <iostream> class X { public: X( int value ) { printf("constructor initialized with %i",value); } } void implicit_conversion_func( const X& value ) { //produces "constructor initialized with 99" } int main (int argc, char * const argv[]) { implicit_conversion_func(99); } Starting

Objective C two-phase construction of objects

余生长醉 提交于 2019-12-29 04:18:03
问题 I've been reading up on RAII and single vs. two-phase construction/initialization. For whatever reason, I was in the two-phase camp up until recently, because at some point I must have heard that it's bad to do error-prone operations in your constructor. However, I think I'm now convinced that single-phase is preferable, based on questions I've read on SO and other articles. My question is: Why does Objective C use the two-phase approach (alloc/init) almost exclusively for non-convenience

Automatically setting class member variables in Python

感情迁移 提交于 2019-12-29 04:12:26
问题 Say, I have the following class in Python class Foo(object): a = None b = None c = None def __init__(self, a = None, b = None, c = None): self.a = a self.b = b self.c = c Is there any way to simplify this process? Whenever I add a new member to class Foo, I'm forced to modify the constructor. 回答1: Please note that class Foo(object): a = None sets a key-value pair in Foo 's dict: Foo.__dict__['a']=None while def __init__(self, a = None, b = None, c = None): self.a = a sets a key-value pair in

Is Constructor Overriding Possible?

≡放荡痞女 提交于 2019-12-29 03:01:11
问题 What i know is, the compiler writes a default no argument constructor in the byte code. But if we write it ourselves, that constructor is called automatically. Is this phenomena a constructor overriding? 回答1: What you describe isn't overriding. If you don't specify a default constructor, the compiler will create a default constructor. If it's a subclass, it will call the default parent constructor(super()), it will also initialize all instance variables to a default value determined by the

Getter/setter in constructor

一世执手 提交于 2019-12-29 02:53:23
问题 I recently read about the fact that there is a possibility of defining getters/setters in JavaScript. It seems extremely helpful - the setter is a kind of 'helper' which can parse the value to be set first, before actually setting it. For example, I currently have this code: var obj = function(value) { var test = !!value; // 'test' has to be a boolean return { get test() { return test }, set test(value) { test = !!value } }; }; var instance = new obj(true); This code always converts value to

Variable argument constructor _may_ conflict, but compiles

試著忘記壹切 提交于 2019-12-29 01:39:31
问题 I have two constructors that compile just fine but I'd expect Java to complain about the possibility of ambiguity. public Foo(int id, Bar bar, String name, String description){ } public Foo(int id, Bar bar, String... values){ } What gives? 回答1: Java allows these methods to exist, because it has rules about which one will be called if both apply. Specifically, the fixed arity method (without ... ) will be chosen over the variable arity method (with ... ). The JLS, Section 15.12.2, states the