constructor

Passing mandatory data to super classes in Kotlin

柔情痞子 提交于 2019-12-14 02:11:44
问题 I've got an issue creating entities in Kotlin. In particular, it's about passing data to a super class. Find an example below. I've got an abstract super class called Trackable that classes can extend. It has a user property that stores who has created that particular object. abstract class Trackable( var createdBy: User ) : Persistable() An class called Contract would now implement the Trackable super class with a compile-time error for the time being: @Entity data class Contract( var

How to catch exception from a constructor in a derived class with C#?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 01:16:57
问题 I was wondering how to catch an exception from a constructor in a derived class with C#. Something as such: public class MyClassA { public MyClassA() { //Do the work if (failed) { throw new Exception("My exception"); } } } public class MyClassB : MyClassA { public MyClassB() { //How to catch exception from a constructor in MyClassA? } } 回答1: Do not even try to figure out how to do this. If the base class constructor is throwing an exception, it means the base class is in a bad state. If the

How to use es6 constructor instructions with a different context

三世轮回 提交于 2019-12-14 00:52:15
问题 Is it possible to use es6 constructor instructions on another instance by changing the "this" context (call, apply or other)? This is possible using es5 "classes". Here is a small example of what I mean: function ES5() { this.foo = 'foo'; } class ES6 { constructor() { this.bar = 'bar'; } } var a = new ES6(); ES5.call(a); console.log(a.foo + a.bar); //foobar var b = new ES5(); //Reflect.construct(ES6); ?? ES6.call(b); //TypeError: Class constructor ES6 cannot be invoked without 'new' console

C++11 calling constructor from constructor of same class type

我们两清 提交于 2019-12-14 00:48:48
问题 I was told the following was possible due to changes in C++11: class SomeType { int number; public: SomeType(int new_number) : number(new_number) {} SomeType() : SomeType(42) {} }; But when I try to build I get an error: "SomeType" is not a nonstatic data member or base class of class "SomeType" error C2614: 'SomeType' : illegal member initialization: 'SomeType' is not a base or member Is this feature not yet supported in Visual Studio 2010? Do I need to configure something to get this to

Loading an object from PHP session, does it call constructor?

爱⌒轻易说出口 提交于 2019-12-14 00:27:34
问题 If I save some object in a php session variable $_SESSION['geoip'] = new GeoIP(); and then on future page loads I use $geoip = $_SESSION['geoip'] , will it call the constructor? My constructor for that GeoIP class is making a call to an remote API (CURL), so I was hoping to save on API calls by only doing it the first time and then storing the results in my session variable. GeoIP.class.php: require_once('geoPlugin.class.php'); class GeoIP { public $currentIP; public $geoplugin; function _

Confused about JavaScript prototypal inheritance with constructors

好久不见. 提交于 2019-12-14 00:26:28
问题 I've read pages and pages about JavaScript prototypal inheritance, but I haven't found anything that addresses using constructors that involve validation. I've managed to get this constructor to work but I know it's not ideal, i.e. it's not taking advantage of prototypal inheritance: function Card(value) { if (!isNumber(value)) { value = Math.floor(Math.random() * 14) + 2; } this.value = value; } var card1 = new Card(); var card2 = new Card(); var card3 = new Card(); This results in three

What exaclty happens when a you call a constructor(new Class),do instance initializer blocks runs first? [closed]

跟風遠走 提交于 2019-12-14 00:09:04
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Im a complete beginner in the Java especially OOP so please pardon my naivety but recently while going through Head First Java,they said, A constructor is a code that runs when somebody says new on a class type. But then i tried this code to see what actually happened but to my surprise the output was totally

Default constructor issue when creating an array Java

此生再无相见时 提交于 2019-12-13 23:18:02
问题 I have the following class: public class ArrayObjects<E> implements SomeImp<E>{ int maxCapacity, actualSize; public ArrayObjects(){ maxCapacity = 10; array = (E[]) new Object[maxCapacity]; } } Eclipse marks an error and says the following: "array cannot be resolved to a variable" Also it shows some additional details: -Type safety: Unchecked cast from Object[] to E[] Does anyone know what am I doing wrong? My goal is to have an array in my class constructor that can hold any kind of object

JavaScript inheritance with unexpected behaviour

会有一股神秘感。 提交于 2019-12-13 21:15:29
问题 I don't know how to solve the following JavaScript problem: function A() { var numbers = []; this.addNumber = function(number) { numbers.push(number); } this.getNumbers = function() { return numbers; } } A.prototype.alertNumbers = function() { var numbers = this.getNumbers(); var length = numbers.length; var number; var numbersString = ""; for (var i = 0; i < length; i++) { number = numbers[i]; numbersString += " " + number; } alert(numbersString); } B.prototype = new A(); function B() { this

basic constructor usage in java

匆匆过客 提交于 2019-12-13 21:00:51
问题 For whatever reason, I cannot find this question anywhere else, nor can I find the answer online. If I have the following: package temp1; public class MainClass { public static void main(String[] args) { } public MainClass(int radius_x, int area_x, int circumference_x) { int radius = radius_x; int area = area_x; int circumference = circumference_x; } } Assuming that this is even correct usage, then how would I actually use the variables defined in the constructor here? They only work inside