constructor

Constructor function return is other than “[Object object]”

此生再无相见时 提交于 2019-12-25 07:49:21
问题 When I use the String constructor I allways wonder how is it made.It returns the string you specificed as a parameter even though it is instanciated with the "new" operator like : var str = new String("Hello World") //str == "Hello World" If I make a constructor and I give it a return value it returns an Object when instanciated : function CreateString(st) { return ''+st } var MyString = new CreateString("Hello there!") //MyString == "[Object object]" How can I create a constructor that

How do i deal with multiple contructor arguments or class variables?

我们两清 提交于 2019-12-25 06:35:22
问题 How do I know what to load in a constructor and what to set using the set methods later on? For example, I have a question class which most of the time will call the following vars: protected $question; protected $content; protected $creator; protected $date_added; protected $id; protected $category; At the moment I have it so only the bare essentials $id , $question , and $content are set in the constructor so I don't start building up a huge list of constructor arguments. This however,

Constructor Design - Loading Data inside the constructor (C#)

人盡茶涼 提交于 2019-12-25 05:48:05
问题 I have the "feeling" that my design to invoke a method to load data inside the constructor is not right. I try to write a class that provides the basic logic to perform a calculation (discount, tax, total-amount etc.) for invoices-, orders- or quotes-record. In most cases are the same set of attributes needed (e.g. the attribute "quantity" is present in all entities/tables). In my current design is use this base class (note this is not the real code, i am at home and do not have access to the

How to remind a param passed to the base class constructor?

丶灬走出姿态 提交于 2019-12-25 05:37:26
问题 Having a member and pass the same to the base class works if: DerrivedClass::DerrivedClass(SomeParamType* p) : BaseClass(p), derrivedClassMember(p), { ... } ... but how to share without passing it as param to the DerrivedClass constructor? Is this valid? Or is the derrivedClassMember not initialized at this time? DerrivedClass::DerrivedClass() : BaseClass(derrivedClassMember), derrivedClassMember(new SomeParamType()), { ... } I'm not able to change the base class implementation. 回答1: It is

Creating a constructor to read a txt file

谁说我不能喝 提交于 2019-12-25 04:56:32
问题 I am creating a program that will produces the statistics of a baseball team i am trying to create a constructor to read the file into the teamName instance variable and the battingAverages array. the txt file contains the one word name of the team followed by 20 batting averages. "Tars 0.592 0.427 0.194 0.445 0.127 0.483 0.352 0.190 0.335 0.207 0.116 0.387 0.243 0.225 0.401 0.382 0.556 0.319 0.475 0.279 " I am struggling to find how to go about this and get it started? 回答1: I ran this and

class template without public constructor as member of another class template

大城市里の小女人 提交于 2019-12-25 03:52:05
问题 I have a class template Shape , which contains information about certain shapes (which can be three- or two-dimensional). I only want a few predefined shapes (cube, sphere and square) to be available. All these predefined shapes have the same properties (so the cube always has the same volume, and I only need to remember the properties of one cube). To inhibit someone from creating other Shape s, I made the constructor private : // Flag for the possible shapes enum class Tag { SPHERE, CUBE,

What can I add in constructors in PHP?

China☆狼群 提交于 2019-12-25 03:38:10
问题 Could anyone tell me what I can include in the constructor? I know that I can do the following. function __construct(){ parent::Controller(); session_start(); } But I am wondering if I can add any variables, if statement etc. Thanks in advance. 回答1: Knock yourself out. Add any PHP you want. You can use $this to refer to the object being created. 回答2: You can include variables, function calls, method calls, object declarations, etc, etc, etc inside your default constructor. class Test {

Does not contain a constructor that takes 4 arguments?

丶灬走出姿态 提交于 2019-12-25 03:34:45
问题 I'm fairly new to programming and I've been stuck on this question for awhile now, I have searched for an answer to this question throughout the internet but am still stumped as to why it is not working. The compiler says that the code below does not contain a constructor that takes 4 arguments? I don't understand why? The code is: public class Users { private int _ID; private string _FName; private string _LName; private string _Address; private string _Phone; public int ID { get { return

Declaring/passing structs vs. declaring/passing individual values

我是研究僧i 提交于 2019-12-25 03:26:53
问题 I have two questions, actually. But they are very closely related. Suppose I have a simple struct like this: public struct TradePrice { public float Price; public int Quantity; public TradePrice(float price, int quantity) { Price = price; Quantity = quantity; } } Question 1 Now if I have code somewhere that needs to access these values, is there any difference between doing this: TradePrice tp = new TradePrice(50.0f, 100); And this: float p = 50.0f; int q = 100; I feel like the first, since

Follow Up: Create an an array of objects from classname

喜欢而已 提交于 2019-12-25 03:04:40
问题 I am following up on this question, 1268817 In the question, we find a way to create an isntance of an object given a the name (as a string) of the class. But what about to create an array of those objects... how would one initialize that. I was thinking something in the line of but doesnt seem to work Object[] xyz = Class.forName(className).newInstance()[]; 回答1: Object objects = java.lang.reflect.Array.newInstance(Class.forName(classname), 10); For an array of 10 elements. Annoyingly it