Objective:
First we need to know why we want to use constructors, or we need to know what we can do with constructors to solve our problems:
1, when assigning the initial value, we may need to repeat the object name, but if the use of the constructor is not so cumbersome. After all, the code is like that, the shorter the better (as everyone knows, I won't say it).
2, if there is such a property, do not allow users to arbitrarily change, we generally define this property as a read-only type of properties. So how do we initialize an attribute of this read-only type when it cannot be instantiated and then assigned a value? As smart as you, the constructor can help us.
So what's so easy to use, does the compiler make it for us? Of course, we have defined a class, and if there is no write constructor, then the compiler will automatically add a constructor with no parameters in this class. And once we have written a constructor, the compiler will not add this constructor with no parameters.
With so much to say, what is a constructor? Constructors are special methods used to create objects, such as the method name and the class name, with no return value and no even void. What is the syntax format?
[Access modifier] < class name > (parameter)
{
The body of the constructor
}
Here are some of the characteristics of the constructor:
1. The name must be the same as the class name
Give me a chestnut:
public class person {public person (string Name,char gender,int age) { this._name = name;//this demonstrative pronoun This._gender = gender; This._age = age; }
2, cannot be called directly, you must use the new operator to invoke.
In the above pest, we should write this when we call the person class:
Person Zsperson = new Person ("Zhang San", ' Male ', 18);
3, it can be overloaded (a class can define the number of different parameters or different types of constructors). Just a little bit not said, that is, if we write a constructor, the original parameterless constructor is not gone, this time we need to do? That is to write it again.
Summarize:
Constructors can be used to create objects, and objects can be initialized in constructors. Small series debut Time is short, I hope you can point out the shortcomings.
Objective:
in the Visual C # video , learned the constructor, feel very interesting, because the understanding is not enough depth, here first to do a brief summary.
First we need to know why we want to use constructors, or we need to know what we can do with constructors to solve our problems:
1, when assigning the initial value, we may need to repeat the object name, but if the use of the constructor is not so cumbersome. After all, the code is like that, the shorter the better (as everyone knows, I won't say it).
2, if there is such a property, do not allow users to arbitrarily change, we generally define this property as a read-only type of properties. So how do we initialize an attribute of this read-only type when it cannot be instantiated and then assigned a value? As smart as you, the constructor can help us.
So what's so easy to use, does the compiler make it for us? Of course, we have defined a class, and if there is no write constructor, then the compiler will automatically add a constructor with no parameters in this class. And once we have written a constructor, the compiler will not add this constructor with no parameters.
With so much to say, what is a constructor? Constructors are special methods used to create objects, such as the method name and the class name, with no return value and no even void. What is the syntax format?
[Access modifier] < class name > (parameter)
{
The body of the constructor
}
Here are some of the characteristics of the constructor:
1. The name must be the same as the class name
Give me a chestnut:
public class person {public person (string Name,char gender,int age) { this._name = name;//this demonstrative pronoun This._gender = gender; This._age = age; }
2, cannot be called directly, you must use the new operator to invoke.
In the above pest, we should write this when we call the person class:
Person Zsperson = new Person ("Zhang San", ' Male ', 18);
3, it can be overloaded (a class can define the number of different parameters or different types of constructors). Just a little bit not said, that is, if we write a constructor, the original parameterless constructor is not gone, this time we need to do? That is to write it again.