constructor

child extended class method calls its super version but that still only sees child data

允我心安 提交于 2019-12-23 01:51:30
问题 class B extends class A. I'll call A the parent and B the child. Both have constructors. B calls super() inside of its constructor. Both have a method with the same name. Perhaps just by coincidence or mistake both have a 'this.x' variable. There then becomes no way to access the parent's this.x variable. It then becomes a point of perhaps unintended communication between the child and parent. class A { constructor(){ this.x = "super x!"; } logx(){ console.log(this.x); } } class B extends A{

How should I write classes? C++

此生再无相见时 提交于 2019-12-23 01:46:22
问题 Hey.. I don't really get them. I read a tutorial about classes in C++, and I don't get a few things: In every example and tutorial that I've seen, functions are never written inside the class! For example, why write a class like this: #include <iostream> using namespace std; class test { private: int x, y; public: test (int, int); int tester () {return x + y; } }; test::test (int a, int b) { x = a; y = b; } int main() { test atest (3, 2); test atest2 (2, 6); cout << "test1: " << atest.tester(

Inheritance when __new__() doesn't return instance of class

为君一笑 提交于 2019-12-23 01:42:10
问题 When __new__ return instance of class, everything is ok, we can create subclass with no problems: class A: def __new__(cls, p1, p2): self = object.__new__(cls) return self def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 class B(A): def __new__(cls, p3): self = super().__new__(cls, 1, 2) return self def __init__(self, p3): super().__init__(1, 2) self.p3 = p3 a = A(1, 2) print(a.p2) # output: 2 b = B(3) print(b.p3) # output: 3 But, If __new__() does not return an instance of cls , then

Is there a convention that objects created using the Builder pattern be immutable?

北城以北 提交于 2019-12-22 18:44:15
问题 According to the book Design Patterns: Elements of Reusable Object-Oriented Software,: The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. In general the Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. With the

Class Constructor arguments C++

倖福魔咒の 提交于 2019-12-22 18:06:20
问题 I am creating a pair1 class for a x and y Cartesian coordinate system. x and y are doubles. I need to have 3 constructors. No arguments, defaults x and y to zero. One arguement assigns x and defaults y to zero. One arugeument defaults x to zero and assigns y. I'm not sure if I am setting up the class right. I get the follwing error: pair1::pair1(double) and pair1::pair1(double) cannot be overloaded. My class: class pair1 { private: double x; double y; public: pair1(){ x = 0.0, y = 0.0; }

add methods in subclasses within the super class constructor

妖精的绣舞 提交于 2019-12-22 18:05:29
问题 I want to add methods (more specifically: method aliases) automatically to Python subclasses. If the subclass defines a method named 'get' I want to add a method alias 'GET' to the dictionary of the subclass. To not repeat myself I'd like to define this modifation routine in the base class. But if I check in the base class __init__ method, there is no such method, since it is defined in the subclass. It will become more clear with some source code: class Base: def __init__(self): if hasattr

How to create a constructor which behaves like a true Array?

南楼画角 提交于 2019-12-22 17:56:05
问题 How can I create a custom array constructor, which is an extended version of the native Array constructor? jQuery, for example, looks like an array with additional methods, such as $().addClass . However, it didn't modify Array.prototype , because new Array().hasClass is undefined . So, how can I create an extended array implementation, without modifying Array.prototype ? Example: Employees( ... ) //-> [{name: 'John', age: 32}, {name: 'Bob', age: 29}]; Employees( ... ).byAge(32)//-> [{name:

ambiguous constructor call while object creation

五迷三道 提交于 2019-12-22 12:25:26
问题 My program is as follows: class xxx{ public: explicit xxx(int v){cout<<"explicit constructor called"<<endl;} xxx(int v,int n=0){cout<<"default constructor called"<<endl;} }; int main(int argc, char *argv[]) { xxx x1(20); //should call the explicit constructor xxx x2(10,20); //should call the constructor with two variables return 0; } When I compile I get the error:- "call of overloaded âxxx(int)â is ambiguous" I know that compiler finds both constructor signature equal since I made an

I need a specific example of how to define a local parameter in the primary constructor of an immutable _case_ class

﹥>﹥吖頭↗ 提交于 2019-12-22 12:02:15
问题 I have normal Scala class I am wanting to refactor to become an immutable case class. As I'm needing the class to be well-behaved in Set operations, I want all the Scala compiler automatically generated methods provided on a case class. IOW, I am wanting to avoid having to write these various methods; equals , hashCode , toString , etc., as that is very error prone. And I am needing to do this for a raft of classes, so I need a general solution, not just a specific solution anomalous quick

Vala different type of constructors

雨燕双飞 提交于 2019-12-22 11:03:33
问题 Why, and what does the three vala constructors ? class construct construct method with class name and more specific, why the 3rd construct is never called when using it from a Gtk.Builder file? 回答1: Short answer: because that's how GObject works. The long answer is just a smidge longer: C doesn't have objects or constructors, it has structs and functions. Structs are very simple; they contain fields, and that's it. No methods, constructors, destructors, etc. They look like this: typedef