constructor

Java constructor/method with optional parameters? [duplicate]

半腔热情 提交于 2019-12-20 09:28:16
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Java optional parameters I know that in PHP if you want to call a function with less parameters you declare the function like: function foo(int param1, int param2 = "2"); and now I can call foo(2) and param2 will be set to 2. I tried to do this in a Java constructor but it seems it isn't possible. Is there a way to do this or i just have to declare two constructors? Thanks! 回答1: Java doesn't have the concept of

Base Class Doesn't Contain Parameterless Constructor?

主宰稳场 提交于 2019-12-20 09:09:18
问题 I'm making my constructors a bit more strict by removing some of my empty constructors. I'm pretty new to inheritance, and was perplexed with the error that I got: Base Class Doesn't Contain Parameterless Constructor. How can I make A2 inherit from A without there being an empty constructor in A. Also, for my own personal understanding, why is A2 requiring an empty constructor for A? Class A{ //No empty constructor for A //Blah blah blah... } Class A2 : A{ //The error appears here } 回答1: In

PHP - best way to initialize an object with a large number of parameters and default values

浪尽此生 提交于 2019-12-20 08:48:53
问题 I'm designing a class that defines a highly complex object with a ton (50+) of mostly optional parameters, many of which would have defaults (eg: $type = 'foo'; $width = '300'; $interactive = false; ). I'm trying to determine the best way to set up the constructor and instance/class variables in order to be able to: make it easy to use the class make it easy to auto-document the class (ie: using phpDocumentor) code this elegantly In light of the above, I don't want to be passing the

What is the difference between getDeclaredConstructors and getConstructors in the Class API?

心不动则不痛 提交于 2019-12-20 08:39:46
问题 I notice that in the Java Reflection API there are two different methods for invoking constructors: the getDeclaredConstructors / getConstructors method. Although the Java docs are slightly different ( getDeclaredConstructors seems to imply that it returns ALL constructors, rather than public ones), its not clear why the API explicitly supports these two different methods. More importantly, I'm wondering : when would one method be preferable to another if we are invoking classes dynamically ?

ASP.net MVC Controller - Constructor usage

女生的网名这么多〃 提交于 2019-12-20 08:02:56
问题 I'm working on an ASP.net MVC application and I have a question about using constructors for my controllers. I'm using Entity Framework and linq to Entities for all of my data transactions. I need to access my Entity model for nearly all of my controller actions. When I first started writing the app I was creating an entity object at the beginning of each Action method, performing whatever work I needed to and then returning my result. I realized that I was creating the same object over and

static in the main class java and non static in constructor [closed]

我只是一个虾纸丫 提交于 2019-12-20 07:32:13
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class. The keyword static refers to the main class. the reason why methods in a main class are static is

Calling default constructor

房东的猫 提交于 2019-12-20 06:29:02
问题 I have a problem with a default constructor in C++. It's a simple thing but can't see what's wrong with it. I have a constructor with 3 optional parameters, with const values on initialization list: data::data(int D = 1, int M = 1, int Y = 1583) : Day(D), Month(M), Year(Y) { if (!CorrectDate()) throw "Wrong Date!"; } Why can I call it with one, two or three parameters and it works just fine but doesn't when I call it with no parameters? data tommorrow(); 回答1: data tomorrow(); is a declaration

How can the assignment from int to object be possible in C++?

拟墨画扇 提交于 2019-12-20 05:46:05
问题 class phone { public: phone(int x) { num = x; } int number(void) { return num; } void number(int x) { num = x; } private: int num; }; int main(void) { phone p1(10); p1 = 20; // here! return 0; } Hi, guys Just I declared a simple class like above one. After that I assigned int value to the object that class, then it worked! (I printed its value. It was stored properly) If there is not a construct with int parameter, a compile error occurred. So, I think it's related with a constructor. Is that

How can the assignment from int to object be possible in C++?

我的梦境 提交于 2019-12-20 05:45:45
问题 class phone { public: phone(int x) { num = x; } int number(void) { return num; } void number(int x) { num = x; } private: int num; }; int main(void) { phone p1(10); p1 = 20; // here! return 0; } Hi, guys Just I declared a simple class like above one. After that I assigned int value to the object that class, then it worked! (I printed its value. It was stored properly) If there is not a construct with int parameter, a compile error occurred. So, I think it's related with a constructor. Is that

Override a constructor class

ぐ巨炮叔叔 提交于 2019-12-20 05:41:20
问题 Below is my code. I am not getting what is the mistake. Can anyone be able to guide. class State { static String country; static String capital; State() // Constructor { country = "America's"; capital = "Washington D.C"; } static void display() { System.out.println(capital + " " + "is" + " " + country + " " + "capital."); } } class Place extends State // Method Overriding { static void display() { System.out.println("Capital is Washington D.C."); } public static void main(String[] args) {