The concrete implementation of C # Object-oriented feature and its function explanation

Source: Internet
Author: User

The concrete implementation and function of C # Object-oriented features font: [Increase decrease] Type: Reprint all the object of the face, the final analysis is to simplify the code, reduce the amount of code, build more realistic logic of the program code, thereby reducing the burden on the programmer. Can not blindly or deliberately to use the idea of the face object and ignore the program to achieve the function or framework, according to the actual situation

As we all know, object-oriented programming is characterized by: encapsulation, inheritance, polymorphism. C # is a completely object-oriented language, because it is more than the time to launch the Java, so the idea of object-oriented thinking is more perfect than Java, then how to embody encapsulation, inheritance and polymorphism in C #? Here is an example and explanation.

1. Encapsulation

The benefits of encapsulation include the following:

① data is not leaked, you can do some protection

Users of the ② class do not have to consider specific data operations, which facilitates

③ program has strong structure, clear level and easy maintenance

Encapsulation of related fields and methods is important for object-oriented programming, but it does not mean that you cannot access the contents of a class or a specific instantiation object, but also provide the user with an interface, just let them invoke it, they only do their work, There's no need to think about or take into account what you're writing about, not to mention what every line of code you write is meant to be.

In C #, the encapsulation of variables will often need to be called outside of this class, in the form of attributes, rather than in C + + directly with the public members or private members of the associated method call, which either does not conform to the object-oriented characteristics, or too cumbersome.

Declaration of attributes in C #:

The code is as follows:
Public class TestClass
    {
Public string Info
        {
get;
set;
        }
    }
The declaration of a property is similar to a function, with the access level first (if it is set to private, it is better to write a variable directly), then the type, followed by the property name, followed by a pair of curly braces, inside the get, set to control whether the property is read-only, write-only, or readable and writable. Of course, the get and set are all directly terminated with semicolons, which are called automatic attributes, and if necessary, you can also customize the return type that is required in get and set,get, along with the property type.

C # Encapsulation of methods requires only changing the access level of the method, such as public, or private (or not), which is accessible externally and only internally.

2. Inheritance

We all know that the 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 or implement multiple interfaces from one class, but you cannot inherit from more than one class.

Example inheritance syntax:


class Son:father
{
//do Something
}
Inheritance allows subclasses or derived classes to obtain all inheritable content of a parent class or a base class, such as fields, methods, but with a certain restriction on the access level, that is, the private level cannot be inherited. In addition to this, it is important to note that if you need to call a member in the base class (the parent Class), you need to use the base keyword, and if you need to use a member of the current class in the method, but because of the name (such as the name of the parameter and the class member variable), you need to use the This keyword to

C # All classes inherit from System.Object, so no matter what class has several fixed, public methods, this is C # embodies the object-oriented thinking very good point!

Let's talk about the characteristics of a static class:

① static classes cannot use the sealed or the abstract modifier

② static classes must inherit directly from System.Object and cannot be other

③ Static class cannot implement any interface

④ Static class cannot contain any operators

⑤ static classes cannot contain static members that use protected or protected internal adornments

⑥ static class intelligence contains static members

⑦ static classes can contain static constructors, but cannot contain instance constructors

⑧ static classes cannot be instantiated

⑨ static classes are sealed and cannot be inherited

Because static classes are loaded automatically by the CLR when the assembly that contains the class is loaded, it is a good idea to use static classes to implement some non-operational data and not to associate with specific objects in your code.

The rest of the note is that when using inheritance, make sense of the sequence of calls to the constructor, initialize the instance fields of the class first, then call the base class constructor, and finally call its own constructor.

3. polymorphic

When a method implemented in a derived class is called by a reference to a base class, different derived classes produce different invocation results, which is polymorphic, while polymorphism in C # is run-time polymorphic and compile-time polymorphic. At compile time, multi-state is implemented by using function overloading, and runtime polymorphism is realized by overriding the virtual method.

① Method Overloading

Premise: In the same class, the method name is the same, the method signature is different (including the name of the method and the parameter information (the modifier, number, type, and number of generic parameters of the parameter), but the name of the return value type, parameter, and type argument is not part 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 example above implements a three overload called function that returns a type string, which returns an int argument as a string and returns a DateTime parameter as a "year-month-day: minutes: seconds. Milliseconds" format for strings, Adds and converts two double-type arguments to the string type

② virtual method

Defining virtual methods requires the use of the virtual keyword, as follows:


Class Car
{
public virtual void Drive ()
{
Console.WriteLine ("Driving..");
}
}


The drive is set to a virtual method, so that derived subclasses can be overridden so that all car derived classes implement the new drive method.

Note: The virtual keyword must precede the return type of the return method, the virtual method can have a method body, and the abstract method does not allow the method body

Two remaining notes: static member functions cannot be virtual functions, constructors cannot be virtual functions

Examples of overwrite methods:


Class Track:car
{
public override void Drive ()
{
Console.WriteLine ("Drive The Big Truck");
}
}
Class Jeep:car
{
public override void Drive ()
{
Console.WriteLine ("Open Jeep");
}
}


③ abstract classes and abstract methods
The following points are noted:

(1) They cannot be instantiated

(2) An abstract method cannot have a method body, and a class must be an abstract class

(3) using the abstract keyword

(4) The abstract method is not implemented, followed by a semicolon

(5) Derived classes of abstract classes must implement all abstract methods

(6) When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method using an abstract method.

4. Summary

The idea of all the objects in the face is ultimately to simplify the code, reduce the amount of code, and build a program code that is more in line with real-life logic, thus reducing the burden on programmers. Can not blindly or deliberately to use the idea of the face object and ignore the implementation of the program's function or framework, according to the actual situation, reasonable use of the face of the object of thought, reduce the burden, and provide convenience for others!

The concrete implementation of C # Object-oriented feature and its function explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.