After learning the C # video, I have a deeper understanding of the object-oriented concept.
So what is object-oriented? What kind of program is object-oriented design? To understand these, we must first understand the three main characteristics of object-oriented: encapsulation, inheritance, and peptides.
Summary:
I. Encapsulation
Encapsulates objective things into abstract classes.
For example, cat class
Class cat // defines cat class {private string name; // declares the private string variable of cat class, which is the internal field public CAT () // constructor {This. name = "";} public CAT (string name) // reload the constructor {This. name = Name;} Public String shout () // shout cat call method {return "meow ";}}
The cat's attribute name and shout () methods are in the cat class.
On the surface, the cat class uses {} to enclose its attributes and methods. In fact, it is an object encapsulation. Just like our commonly used laptop, many parts are encapsulated inside. When we use a computer, we only need to press the switch to pull it, without having to worry about their internal operations.
Ii. Inheritance
It is the relationship between classes. The inherited class is the base class, And the inherited class is the derived class.
The key symbol inherited in C # is ":". For example, the cat class inherits the animal class.
Class Cat: Animal // cat class inherits animal class {public CAT (): Base () // The Sub-constructor must call the same constructor of the parent class. Use the base keyword {} public CAT (string name): Base (name ){}
}
Using inheritance to put the common part of the subclass into the parent class not only reduces code duplication, but also facilitates modification.
3. Peptides
Peptides indicate that different objects have the same action, but they execute code by themselves.
Peptide implementation: 1. Define the method to be implemented as a virtual method in the parent class; 2. Implement the method using override in the subclass.
The details are as follows:
Class animal {Public Virtual string shout () // defines a virtual shout method, which can be rewritten by subclass {return "" ;}} class Cat: animal // cat class inherits animal {public override string shout () // overwrites the virtual method in the parent class to get its shout method {return "" ;}} class Dog: animal {public override string shout () {return "Wang ";}}
Summary:
1. attribute is the data of the encapsulated object, and the method is the behavior of the object. In code, the attribute is not followed by parentheses, and the method is enclosed by parentheses.
2. Determine whether two classes are inherited or not, and then determine whether the class is full or not, for example, "dog is an animal ".
3. abstract classes must be inherited and instantiation is meaningless.
4. the overload is two or more constructors, And the overwrite is the virtual/override method in the parent class ).
5. The virtual methods defined in the base class have method bodies, which are rewritten and selected during implementation. abstract methods do not have method bodies and must be overwritten by subclasses.