Object-Oriented Programming in C,
All object-oriented languages have three basic features, and C # is no exception.
- Encapsulation-encapsulate objective things into classes and hide internal implementations of classes to ensure data integrity;
- Inheritance-the parent class objects can be reused through inheritance;
- Polymorphism-a capability that allows assigning sub-objects to help objects.
1. Encapsulation
Encapsulation refers to hiding the internal data of the class and preventing the object instance from directly operating on it. In C #, encapsulation can be reflected by keywords such as public private protected and internal.
C # provides an attribute mechanism to protect the internal data status.
Public class Person
{
Private string _ name;
Private int _ age;
Public string Name
{
Get {return _ name ;}
Set {_ name = value ;}
}
Public int Age
{
Get {return _ age ;}
Set
{
// In the attribute definition, you can add logical code according to the system's business logic
If (value <0 | value> 120)
{
Throw (new ArgumentOutOfRangeException ("AgeIntProperty", value, "age must be "));
}
_ Age = value;
}
}
}
2. Inheritance
In C #, a class can inherit from another existing class. The subclass obtains all the members of the base class except the constructor and destructor. Static and sealed classes cannot be inherited at the same time.
C # is different from C ++. It only supports single inheritance.
// Base class
Public class Animal
{
Private int _ age;
Public int Age
{
Get {return _ age ;}
Set
{
If (value <0 | value> 10)
{
Throw (new ArgumentOutOfRangeException ("AgeIntProperty", value, "age must be between 0 and 10 "));
}
_ Age = value;
}
}
}
// Subclass (Horse)
Public class Horse: Animal
{}
// Subclass (Goat)
Public class Sheep: Animal
{}
Note that the subclass does not allow direct access to private members of the parent class. It only protects access to Members and public members.
Private Members also inherit from the quilt class, but the subclass cannot directly access the private member. The subclass can indirectly access the private member by calling the public or protection method.
2.1 Seal
Public sealed class SealedClass
{
// Define class members here
}
// The sealed class cannot be the base class of other classes. The following code will cause errors during compilation.
Public class Test: SealedClass
{}
2.2 subclass initialization sequence
When initializing a subclass, besides the constructor of the subclass, the base class constructor is also called. The initialization sequence of subclass is as follows:
(1) instance fields of the initialization class;
(2) Call the constructors of the base class. If the base class is not specified, the System. Object constructor is called;
(3) Call the constructor of the subclass.
An example is provided below:
// Parent class
Public class Parent
{
// ② Call the constructors of the base class
Public Parent ()
{
Console. WriteLine ("base class constructor called ");
}
}
// Subclass
Public class ChildA: Parent
{
// When creating a ChildA object
// ① Initialize its instance Field
Private int FieldA = 3;
// ③ Call the subclass Constructor
Public ChildA ()
{
Console. WriteLine ("the constructor of the subclass is called ");
}
Public void Print ()
{
Console. WriteLine (FieldA );
}
}
Class Program
{
Static void Main (string [] args)
{
ChildA childa = new ChildA ();
}
}
3. Polymorphism
Polymorphism definition: objects of the same type call the same method but show different behaviors.
3.1 Use the virtual and override keywords to implement method Rewriting
Only when the base class member is declared as virtual or abstract can the class be overwritten. If the subclass wants to change the implementation behavior of the virtual method, the override keyword must be used.
Public class Animal
{
Private int _ age;
Public int Age
{
Get {return _ age ;}
Set
{
If (value <0 | value> 10)
{
Throw (new ArgumentOutOfRangeException ("AgeIntProperty", value, "age must be between 0 and 10 "));
}
}
}
// Almost all animals have the ability to make sounds.
// But for animals, the sound of each animal is different.
Public virtual void Voice ()
{
Console. WriteLine ("animal starts to sound ");
}
}
// Horse (subclass). The subclass should override the method of the base class to implement its own unique behavior.
Public class Horse: Animal
{
// Override the parent class method using the override keyword
Public override void Voice ()
{
Console. WriteLine ("Trojan ");
}
}
// Goat (subclass)
Public class Sheep: Animal
{
// Override the parent class method using the override keyword
Public override void Voice ()
{
Console. WriteLine ("goat name ");
}
}
Class Program
{
Static void Main (string [] args)
{
Animal horse = new Horse (); // assign the subclass object to the parent class variable
Horse. Voice ();
Animal horse = new Horse (); // assign the subclass object to the parent class variable
Horse. Voice ();
Console. ReadKey ();
}
}
If you want to access the Virtual Methods in the parent class in the subclass, you must use the base keyword to complete the call. Base. Voice ();
In the above case, we recommend that you set the Animal class to an abstract class and use the abstract keyword to avoid directly creating such class instances in the code.
3.2 prevent a derived class from overwriting virtual members
Public class Horse: Animal
{
// With the sealed keyword, Horse cannot extend the Voice method any more.
Public sealed override void Voice ()
{
// Call the base class Method
Base. Voice ();
Console. WriteLine ("Trojan ");
}
}
3.3 hide base class members with new members
You can use the new keyword in the subclass to hide the parent class members.
Public class Horse: Animal
{
Public new void Voice ()
{
......
}
}
In this form, if you want to call the method of the parent class, you can only forcibly convert the object to the parent class type.
4. parent classes of all classes
In C #, all classes are derived from the System. Object Class. Below are some members of the Object class:
Public class Object
{
// Method
// Constructor
Public Object ();
// Virtual Member. Subclass can override these methods.
Public virtual bool Equals (object obj );
Protected virtual void Finalize ();
Public virtual int GetHashCode ();
Public virtual string ToString ();
// Instance Member
Public Type GetType ();
Public object MemberwiseClone ();
// Static member
Public static bool Equals (object objA, object objB );
Public static bool ReferenceEquals (object objA, object objB );
}
Summary: This article focuses on some basic knowledge points of C # in Object-Oriented aspects. If you can familiarize yourself with these three features, you will have a lot of ideas.