Course 1: object-oriented concept.
(OOP.
Object-oriented is only a supplement to process-oriented, rather than replacing process-oriented.
Class and object. Classes are abstract and objects are specific. A person is a class, and a person is an object of the "person" class. A class-related variable is called a field and is a class state. Classes do not occupy memory, and objects only occupy memory.
Method: it is the actions that can be executed by the class, such as hello and eat.
Class inheritance: there can be inheritance relationships between classes. For example, computers can be inherited from electrical appliances. The advantage of doing so is that computers only need to define their own unique fields and methods.
Object-oriented features: encapsulation, inheritance, and polymorphism.
Course 2: Define a class.
Let's take a look at this program, which basically includes the definition of the class, the declaration of the class object, the assignment of the object field, and so on.
Course 3: Access Level of class members.
Before defining a class member, there is a keyword, such as the public keyword above. Public means that the value of a member can be changed anywhere in the program.
There is also an access-level restriction Keyword: private can only change its value in the class.
Look at this function:
If the "Method" in the class has the same name as the member of the parameter category. Use this. name to specify the name of the class member rather than the parameter name. Let's take a look at the definition of the following class. In fact, different names can also use the this keyword.
Course 4: Object-oriented attributes.
Simplify field reference and assignment: to avoid unauthorized changes to the field, set the field to private.
Then, set attributes for this field. The set and get actions in the attribute can be used to change the field value.
Course 5: constructor:
Constructor is a class function. For example, an object in the Declaration class is: Persion p1 = new Person ();
Person is a class. P1 is a class object.
Person () is the constructor of the person class. By default, there is no parameter.
However, if you want an object of the class you declare to have an initial value, you can declare the constructor and pass the parameters in. Constructors can be overloaded. Take a look at the following program:
The Gender of the above function cannot be changed. Therefore, if you cannot use {set; get;}, you must use the traditional one:
Private Gender gender;
Public Gender gender
{
Get {return this. gender ;}
}
Also, let's take a look at the two constructors of the above function. There is redundancy in the code of the value assignment field, which does not conform to the programming principles of DRY (Don't Repeat Yourself ). How can we solve this problem? You can use nested function calls.
As follows:
Nested call of constructor: Add ": this (parameter)" after a function )"
Course 6: inheritance of classes. The main role is the reuse of class attributes and methods. For example, first define a Person class, if you need to define another Chinese class. Then, the "Chinese" class can inherit fields, attributes, and methods from the "people" class. For example, the following code:
If a class does not define a parent class, it is an object class by default. An Object is a direct or indirect parent class of any class.
When a subclass inherits a method of the parent class, it cannot be inherited by default, because the inherited method in the subclass may be inappropriate and needs to be modified. How to change it.
First, add the virtual keyword before the method of the parent class.
Method in the subclass:
The Chinese class is a subclass of the Person class, and the Person class is a subclass of the object class. Objects support the. ToString method.
Therefore, the Person and Chinese classes are also supported. You can use the override method to change the behavior of the. ToString method. For example, you can overwrite the. ToString method of the object class in the Person class.
In C #, only one parent class is allowed for a class. In C ++, there can be multiple parent classes.
Can subclass inherit from a constructor declared by the parent class. What should I do if I can.
Constructor cannot be inherited. each subclass must declare its own constructor.
Although it cannot be inherited, child classes can call the constructor of the parent class.
Declare the constructor in the Person parent class.
Declare the constructor In the subclass Chinese:
Use of constructors.
Course 7: Polymorphism
A parent class pointer can point to an object of its subclass.
Example: Person p1 = new Chinese ();
P1.SayHello (); // This is a correct reference if the subclass and parent class have the SayHello method.
If this method exists in the subclass and the parent class is not sure whether this method is available, it is wrong.
Course 8: benefits of Polymorphism.