The concrete realization of C object oriented features and their functions

  • 2020-05-19 05:39:26
  • OfStack

As we all know, object-oriented programming is characterized by encapsulation, inheritance and polymorphism. C# is a fully object-oriented language. Since Java was introduced later than Java, it is more perfect for the embodiment of object-oriented ideas than Java. So how can encapsulation, inheritance and polymorphism be embodied in C#? Here are some examples and explanations.

1. The encapsulation

The benefits of encapsulation are as follows:

Data do not leak, can achieve 1 set of protection

Class users do not have to consider the specific data operation, convenient

The procedure structure is strong, the level is clear, easy to maintain

On relevant fields, methods, encapsulation is plays an important and indispensable role in object-oriented programming, but does not represent can not access the contents of the class or specific instantiation object, and provides the interface for users, it is ok to just let them call, they only do their work, don't need to worry about or care about what kind of specific do you wrote, not to mention every one line of code that you write what mean.

In C#, the encapsulation of variables will often need to be called outside of the class, in the form of properties, rather than directly as C++ with public members or private members and associated method calls, which are either not object-oriented or cumbersome.

Declaration of properties in C# :


public class TestClass
    {
        public string Info
        { 
            get; 
            set; 
        }
    }

The declaration of a property is similar to a function, with an access level (if set to private is equal to a private field, you might as well just write a variable), then a type, then a property name, followed by a curly brace of 1, get, set to control whether the property is read only, written only, or readable and writable. Of course, get and set end with a semicolon, which is called an automatic attribute. You can also customize get and set if you want.

The encapsulation of the method by C# simply changes the access level of the method, such as setting it to public or private (or not), which corresponds to external access and only internal access.

2. The inheritance

It is well known that inheritance mechanism can improve the reusability and extensibility of code to improve the development efficiency and reduce the amount of code. Unlike C# and C++, you can inherit from one class or implement multiple interfaces, but you can't inherit from multiple classes.

Example inheritance syntax:


class Son : Father
{
      //Do Something
} 

Inheritance allows a subclass or derived class to obtain all inheritable contents of a parent or base class, such as fields and methods, but imposes a one-point limit on the access level, which is that the private level cannot be inherited. In addition to this, there needs to be clear, if you need to call a member of the base class (parent), you need to use base keywords, and if in the method, you need to use the current members of the class, but because the nuptial (such as parameters and a class member variable name repetition), you need to use this keyword which is to determine whether access to the members.

All C# classes inherit from System.Object, so there are several fixed, common methods for any class, which is a good point for C# to embody the object-oriented idea!

Let's take a look at the characteristics of static classes:

The static class cannot use sealed or abstract modifiers

Static classes must be directly inherited from System.Object, not other

A static class cannot implement any interface

A static class cannot contain any operators

A static class cannot contain static members modified with protected or protected internal

The static class intelligence contains static members

Static classes can contain static constructors, but not instance constructors

Static classes cannot be instantiated

9 static classes are sealed and cannot be inherited

Since static classes are automatically loaded by CLR when the assembly that contains them is loaded, it is a good idea to use static classes to implement methods that do not manipulate data and are not associated with specific objects in your code.

The only thing left to note is that when using inheritance, figure out the order in which constructors are called, initializing the class's instance fields, calling the base class constructor, and finally calling your own constructor.

3. The polymorphism

When a method implemented in a derived class is called through a reference to a base class, different derived classes will produce different results of the call, which is called polymorphism, and the polymorphism in C# is divided into run-time polymorphism and compile-time polymorphism. Compile-time polymorphism is realized by function overloading and run-time polymorphism is realized by overwriting virtual methods.

Method overload

Premise: in the same class, the method name is the same, and the method signature is different (including the method name and parameter information (modifier, number, type, and number of generic parameters), but the return value type, parameter and type name are not part 1 of the method signature)

Method overloading example:


public string Function(int x)
{
 return x.ToString();
}
public string Function(DateTime x)
{
 return x.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
public string Function(double x,double y)
{
 return (x+y).ToString();
}

The above example implements one of three overloads named Function with the return type string, which respectively returns one int type parameter to string and one DateTime type parameter to a string in the format of "year, month, day, hour: minute: second. Millisecond", and converts the sum of the two double type parameters to string type

(2) the virtual method

The virtual keyword is used to define virtual methods, as follows:


class Car
{
 public virtual void Drive()
 {
  Console.WriteLine(" driving ..");
 }
}

The reason Drive is set to virtual is so that derived subclasses can override it, so that all derived classes of Car can implement the new Drive method.

Note: the virtual keyword must precede the return type of the return method. Virtual methods can have method bodies, while abstract methods do not

There are two remaining items to note: static member functions cannot be virtual, and constructors cannot be virtual

Example overwrite method:


class Track : Car
{
 public override void Drive()
 {
  Console.WriteLine(" Drive big trucks ");
 }
}
class Jeep : Car
{
 public override void Drive()
 {
  Console.WriteLine(" Drive jeeps ");
 }
}

(3) abstract classes and abstract methods
Has the following note: (1) they can't be instantiated (2) the abstract methods there can be no method body, in the class must be an abstract class (3) using abstract keyword (4) abstract methods have no implementation, followed by a semicolon (5) an abstract class derived class must implement all abstract methods (6) when the abstract class from the base class inherited virtual method, abstract classes can use abstract methods to rewrite the virtual method.

I won't give you specific examples

4. To summarize

The whole idea of physiographical objects is, in the final analysis, to simplify the code, reduce the amount of code, and build program code that is more in line with the logic of real life, thus reducing the burden on programmers. You can't use the idea of the object of the face deliberately or ignore the function or the framework of the program. You should make use of the idea of the object of the face according to the actual situation, so as to reduce the burden and provide convenience for others!


Related articles: