constructor

Static Constructor and Exceptions

北慕城南 提交于 2019-12-12 08:45:12
问题 What happen if a static constructor throws and exception and it is not handled? Will it remain till the application domain alive? 回答1: An unhandled exception in a static constructor, will be wrapped in a TypeInitializationException . If you're on .NET 2 or newer an unhandled exception will terminate the process. 回答2: The exception will propagate and if it is not caught somewhere like for example a global exception handler it will simply kill the application domain. 回答3: if an Exception

Private constructor and public parameter constructor

不打扰是莪最后的温柔 提交于 2019-12-12 08:43:09
问题 I heard that a private constructor prevents object creation from the outside world. When I have a code public class Product { public string Name { get;set;} public double Price {get;set;} Product() { } public Product(string _name,double _price) { } } Here I still can declare a public constructor (parameter), won't it spoil the purpose of the private constructor? When do we need both private and public constructor (parameter) in code? I need a detailed explanation please. 回答1: The reason you

c++ call constructor within another constructor of the same class

做~自己de王妃 提交于 2019-12-12 08:24:02
问题 I am using MinGW-w64 with 4.8.1 (with -std=c++11) and trying to call one constructor of my class within another constructor of the same class. Unfortunately, I failed to compile the code below. A::A(const char *pc) { A(string(pc)); } A::A(string s) { vector<string> tmpVector; tmpVector.push_back(s); A(tmpVector); } // Constructor A::A(vector<string> filePathVector) { } Below is the error GCC is complaining about. In file included from ../parser/nsn/parser.h:37:0, from main.cpp:2: ../parser

Call to super must be first statement in the constructor, but it is

☆樱花仙子☆ 提交于 2019-12-12 08:22:51
问题 I keep getting an error saying that "call to super must be the first statement in the constructor". The problem is that it is the first statement in my constructor. public void CheckingAccountCustomer(int a){ super(n, p, b); accountNo = a; } And here is my superclass for this as well. public void customer(String n, int p, double b){ name = n; pin = p; balance = b; } What am I doing wrong here? 回答1: This code public void customer(String n, int p, double b){ is not a constructor. Constructors

Why is new String(“Hello”) invalid in C#?

烈酒焚心 提交于 2019-12-12 08:19:48
问题 What is the logic/reason behind making String s= new String("Hello World"); Illegal in C#? The error is The best overloaded method match for `string.String(char*)' has some invalid arguments I'm not interested in the API docs, I am interested in why this is illegal. Is is because of pooling static strings? like Java pools Integer(-128) to Integer(127) with horrendous results? ( of course strings too ) 回答1: It would be rather pointless to use the constructor to create a new string based on

Java: Alternative to passing “this” as constructor argument in order to reference creating object

谁说胖子不能爱 提交于 2019-12-12 07:51:39
问题 I've spent a while thinking about different solutions that the one I went for as I've read around (I am not really experienced with Java yet) that using this for a constructor argument isn't usually a good practice. What I am trying to do is to instantiate several objects of class JobGroupMod and for every JobGroupMod I have to create a certain number of JobMod objects that must be able to reference back the JobGroupMod objects in which they've been spawned from. In order to accomplish that I

Kotlin: Initialize class attribute in constructor

情到浓时终转凉″ 提交于 2019-12-12 07:42:25
问题 I create a Kotlin-class with a class attribute, which I want to initialize in the constructor: public class TestClass { private var context : Context? = null // Nullable attribute public constructor(context : Context) { this.context = context } public fun doSomeVoodoo() { val text : String = context!!.getString(R.string.abc_action_bar_home_description) } } Unfortunately I have to declare the attribute as Nullable with the "?" sign, although the attribute will be initialized in the constructor

Why constant data member of a class need to be initialized at the constructor?

牧云@^-^@ 提交于 2019-12-12 07:35:31
问题 I want to know why constant data member of a class need to be initialized at the constructor and why not somewhere else? What is the impact of doing so and not doing so? I also see that only static constant integral data can be initialized inside the class other than that non of the data members can be initialized inside the class. for eg:- Suppose below is my class declaration class A{ int a; // This we can initialize at the constructor or we can set this member by calling "vSet" member

Is there any reason to choose __new__ over __init__ when defining a metaclass?

送分小仙女□ 提交于 2019-12-12 07:08:57
问题 I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is there any reason to prefer one over the other? Update : Bear in mind that I'm asking about using __new__ and __init__ in a metaclass. I already understand the difference between them in another class. But in a metaclass

calling jframe methods from constructor

别说谁变了你拦得住时间么 提交于 2019-12-12 06:30:41
问题 Are there any problems in calling methods from the constructor in this particular case? class GUI2 { JFrame jfrm; static Container cntr; GUI2(){ jfrm=new JFrame("Raaga"); jfrm.setSize(555,493); jfrm.setResizable(false); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); jfrm.setVisible(true); } 回答1: There would be no problems if you write so. Ofcourse,writing too much business logic