+ 3
can someone please tell me what constructors are???
4 Réponses
+ 6
as a function it is, params of contructor are the same of any function.
They can change, they even can be empty, its all up to how its defined.
Person p1 = new Person();
for example, this one is empty, so the constructor params don't require anything.
Person p1 = new Person("Ivan");
Instead, this one requires a String parammeters, wich is the name, to later be assigned to the atributte class for p1 object.
+ 4
A method from class which is called when you use the new command (creating a new object).
Even if you don't create one, there is always the default, which is empty.
the constructors are mostly publics and exactly the same name of the class.
+ 1
For example:
class Person{
int age;
string name;
Person(int age, string name){
this.age = age;
this.name = name;
}
}
Here, the parameters of the Constructor are age and name inside the parentheses of the Constructor, Person(int age, string name) - when constructing a new object of the Person class you pass concrete arguments for those parameters like this:
Person p1 = new Person(34, Michael);
0
Thanks, but what are parameters of a constructor.