constructor

Why is there a constructor method if you can assign the values to variables?

≯℡__Kan透↙ 提交于 2019-12-11 07:22:08
问题 I'm just learning PHP, and I'm confused about what the purpose of the __construct() method? If I can do this: class Bear { // define properties public $name = 'Bill'; public $weight = 200; // define methods public function eat($units) { echo $this->name." is eating ".$units." units of food... <br />"; $this->weight += $units; } } Then why do it with a constructor instead? : class Bear { // define properties public $name; public $weight; public function __construct(){ $this->name = 'Bill';

A method to take a number of types

自古美人都是妖i 提交于 2019-12-11 06:58:44
问题 I currently am working with a constructor which takes in of type object I am then testing it's type based on instanceof Public MyClass (Object obj) { if(obj instanceof CusClass1){ CusClass1 myObject = (CusClass1) obj; globalVar1 = myObject.getAttrib1(); globaVar2 = myObject.getAttrib2(); } if(obj instanceof CusClass2){ CusClass2 myObject = (CusClass2) obj; globalVar1 = myObject.getAttrib1(); globaVar2 = myObject.getAttrib2(); } } Can this be offset to an initalise method called from within

How to override a base class constructor in Javascript

ぐ巨炮叔叔 提交于 2019-12-11 06:49:50
问题 The Udacity ES6 training has a question about overriding a base class constructor. I've got a solution but Udacity doesn't let me get away with it. The assignment is: Create a Bicycle subclass that extends the Vehicle class. The Bicycle subclass should override Vehicle's constructor function by changing the default values for wheels from 4 to 2 and horn from 'beep beep' to 'honk honk'. class Vehicle { constructor(color = 'blue', wheels = 4, horn = 'beep beep') { this.color = color; this

Calling Different Constructors on each index of Array of Objects

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:48:17
问题 I am working on a project which handles the Data of a Library Section having multiple classes. Interface of interest of a class here is this. class Author { private: //data members public: Author (string _name) ; Author (string _name, int _books) ; } ; Data is present with at least the name of the author, and no. of books may come along. I cannot have a default constructor here naturally. But if create a static array like this. Author auth_arr[100] ; Compiler gives me error due to absence of

HTML in Mootools' Element constructor?

会有一股神秘感。 提交于 2019-12-11 06:34:56
问题 I'm currently using the Mootools Element constructor method to dynamically add a new row into a table. function newPermission(somedata) { var newUserPerm = new Element('tr', { 'html': '<td>foo</td><td>bar</td>' }); newUserPerm.inject('permissions_table'); } However, upon checking the resulting code, the following HTML string gets added to the table: <tr>foobar</tr> I'm sure there's some way to send the HTML tags as well, but I can't find much on it here, except one other question, in which

Can you mix free and constructor in C++? [duplicate]

北城以北 提交于 2019-12-11 06:15:25
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is there any danger in calling free() or delete instead of delete[]? I was reading this question: In what cases do I use malloc vs new? Someone raised that one reason to use malloc was if you were going to use free. I was wondering: Is it valid to mix a free call and a constructor initialization in C++? i.e. Can I say: my_type *ptr = new my_type; free(my_type); Is that somehow invalid or worse than: my_type *ptr

Multiple constructors with variable argument parameters

霸气de小男生 提交于 2019-12-11 06:08:30
问题 Given these two constructors: SomeClass(int... params) { // Do things } SomeClass(long... otherParams) { // Do other things } What happens when an object foo is instantiated? SomeClass foo = new SomeClass(); Is the undefined default constructor somehow called? Or is one of those constructors with an empty array called? If so, what’s the precedent? I’ve done some basic testing and found that if a constructor without parameters is defined then that will be called. Otherwise, it appears that an

C++ - How to make read only class member variables in Visual Studio 2010

和自甴很熟 提交于 2019-12-11 05:58:53
问题 Hi I am trying to make some public member variables read only. I know I could do something like: private: int _x; public: const int& x; Constructor(): x(_x) {} I'm looking for something more manageable and easier to read. I found several templates on the internet all of which do similar to what is described as a proxy class in this SO answer. I'm trying to adapt that proxy class so that I can put the template in an include and write something like this for each variable in a class that I need

Shortcut for creating constructor with base (C# Visual Studio 2015)

喜夏-厌秋 提交于 2019-12-11 05:38:22
问题 In Visual Studio C# you can, in a class, type ctor and then press Tab and Visual Studio will create a constructor for that class for me. Very convenient. But is there a way (shortcut) to make Visual Studio generate all constructors with bases from its inherited class? For example, public class User:ClassA { public User() {} public User(string S):base(S) {} public User(string S, int I):base(S, I) {} ... } 回答1: I do not have ReSharper and it is possible. You can select your base class and press

Java: java.util.NoSuchElementException: No line found

白昼怎懂夜的黑 提交于 2019-12-11 05:37:13
问题 I have a problem and don't know what to do. Here's the exception: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at Auto.<init>(Auto.java:32) at Auto.main(Auto.java:22) And that's my Code: import java.util.Scanner; public class Auto { private String Marke1; String Modell1; String Farbe1; double Höhe; double Breite; double Länge; int Tueren = 4; public static void main(String[] args) { System.out.println("Hello Auto!");