constructor

How to unit test the methods of a class whose constructor take some arguments?

江枫思渺然 提交于 2020-05-26 01:22:10
问题 I have a class of form something like this: class A{ public function __constructor(classB b , classC c){ // } public function getSum(var1, var2){ return var1+var2; } } My test case class is something like this: use A; class ATest extends PHPUnit_Framework_TestCase{ public function testGetSum{ $a = new A(); $this->assertEquals(3, $a->getSum(1,2)); } } However when I run the phpunit, it throws some error like: Missing argument 1 for \..\::__construct(), called in /../A.php on line 5 Even if I

Having lots of parameters in a constructor

喜欢而已 提交于 2020-05-22 14:08:26
问题 Is it wrong to have a lot of parameters inside a constructor? Like 10 to 15 parameters? Because I was designing a class where a constructor will have lots of parameters, for example, a Person class. My example person class has 6 parameters in its constructor: public class Person { private String fName; private String lName; private String mInitial; private int age; private String contactNumber; private String emailAddress; public Person(String fName, String lName, String mInitial, int age,

Object is not a constructor in React Native when using pubnub-react

爱⌒轻易说出口 提交于 2020-05-16 05:45:52
问题 I am trying to integrate pubnub-react in react native. Here's my code: import React, { Component } from 'react'; import PubNubReact from 'pubnub-react'; import {key1,key2} from '../config.js export default class extends Component { constructor(props) { super(props); this.pubnub = new PubNubReact({ publishKey: key1, subscribeKey: key2 }); this.pubnub.init(this) } render() { return null } } This is throwing this error: [[TypeError: Object is not a constructor (evaluating 'new _pubnubReact

Object is not a constructor in React Native when using pubnub-react

倖福魔咒の 提交于 2020-05-16 05:45:27
问题 I am trying to integrate pubnub-react in react native. Here's my code: import React, { Component } from 'react'; import PubNubReact from 'pubnub-react'; import {key1,key2} from '../config.js export default class extends Component { constructor(props) { super(props); this.pubnub = new PubNubReact({ publishKey: key1, subscribeKey: key2 }); this.pubnub.init(this) } render() { return null } } This is throwing this error: [[TypeError: Object is not a constructor (evaluating 'new _pubnubReact

Force Uniform Constructor and Method Annotation Values?

做~自己de王妃 提交于 2020-05-14 10:39:30
问题 I'm writing a custom API using Reflection to save Objects to file. I have the following class structure: @Constructor public XYZPOJO(@Key(key = "word") String word, @Key(key = "variations") ArrayList<String> varList) { this.word = word; this.varList = varList; } String word; ArrayList<String> varList = new ArrayList<String>(); @Key(key = "word") public String getWord() { return word; } @Key(key = "variations") public ArrayList<String> getVarList() { return varList; } When saving Object to

Why do we need __init__ to initialize a python class

痞子三分冷 提交于 2020-05-11 10:27:59
问题 I'm pretty new to OOP and I need some help understanding the need for a constructor in a python class. I understand init is used to initialize class variables like below: class myClass(): def __init__ (self): self.x = 3 print("object created") A = myClass() print(A.x) A.x = 6 print(A.x) Output: object created 3 6 but, I could also just do, class myClass(): x = 3 print("object created") A = myClass() print(A.x) A.x = 6 print(A.x) which prints out the same result. Could you please explain why

Why do we need __init__ to initialize a python class

五迷三道 提交于 2020-05-11 10:22:06
问题 I'm pretty new to OOP and I need some help understanding the need for a constructor in a python class. I understand init is used to initialize class variables like below: class myClass(): def __init__ (self): self.x = 3 print("object created") A = myClass() print(A.x) A.x = 6 print(A.x) Output: object created 3 6 but, I could also just do, class myClass(): x = 3 print("object created") A = myClass() print(A.x) A.x = 6 print(A.x) which prints out the same result. Could you please explain why

Initialization with empty curly braces

一笑奈何 提交于 2020-05-11 07:32:07
问题 First attempt and everything works fine: class Base { public: Base() {std::cout << "default ctor!\n"; } }; ... Base b{}; Base b_one = {}; Another way of implementation(add explicit ): class Base { public: explicit Base() {std::cout << "default ctor!\n"; } }; ... Base b{}; Base b_one = {}; // error! Why? I have read on cppreference that in both cases default initialization would be used and no diffences. From list initialization: Otherwise, If the braced-init-list is empty and T is a class

constructor.name is undefined in Internet Explorer

*爱你&永不变心* 提交于 2020-05-10 07:33:05
问题 My debugging work in IE ended today by finding that constructor.name is undefined . I created the following simple code that reproduces the issue: ({}).constructor.name === undefined // => true Is there any workaround to make this work? Maybe overriding somehow the prototype? If possible, I don't want to change the syntax, because the change would be major. JSFIDDLE 回答1: The problem is simply that the name property of function objects is not supported in Internet Explorer. The property is non

List as optional constructor parameter is null in Dart

旧时模样 提交于 2020-04-30 08:45:27
问题 In the following example, why is myList null when no parameter is passed to the constructor? I declare it as an empty ( growable ) list in the class. class MyListClass { List myList = []; MyListClass({this.myList}); } void main() { final obj = MyListClass(); assert(obj.myList != null); } What is the best way to pass an optional list, but default to an empty list? I know you can do the following, but maybe there is a better way? MyListClass({this.myList}) { this.myList ??= []; } UDATE: This is