----- three characteristics of object-oriented encapsulation, inheritance, polymorphism

Foreword

  There is an article written in front of the head five principles (SOLID five principles) Object-oriented programming and object-oriented design. Today we come to talk about the three characteristics of object-oriented - encapsulation, inheritance, polymorphism

Package

   It is defined as "the one or more items enclosed in a physical or logical packets." In object-oriented design methodology, the package is to prevent access to implementation details. Package only disclose certain external interface, hide specific implementation details. Adds a certain security, prevent leaks and damage information.

  Mentioned package, we have to put down the access modifier.

    • public: all objects can be accessed;
    • private: the object itself can be accessed inside the object;
    • protected: Only class object can access the object and its subclasses
    • internal: a set of objects of the same program can be accessed;
    • protected internal: restricting access to or derived from the current assembly of the type comprising a class.

inherit

  Inheritance is a form of software reuse. Using inheritance can reuse existing data and conduct classes, give them a new function to create a new class. 

  Create a new process based on the existing class (base class, a parent class) (derived class, subclass) is called inheritance. Derived class can automatically obtain all the members except for the base class constructor and destructor, can add new attributes and methods extend its functionality in a derived class .

  Here succession can be divided into the following series:

    • Single inheritance: represents a class derived from a base class, C # using this inheritance
    • Multiple inheritance: Multiple inheritance allows a class derived from multiple classes, C # does not support multiple inheritance, but allows the interface of multiple inheritance
    • Multilayer Inheritance: Inheritance allows greater Multilayer this layer structure, B is derived from class A, class B derived from class C, which is also called an intermediate base class B class, C # supports it, is also very common.
    • Interface inheritance: the interface allows multiple inheritance

Polymorphism

  Refers to the presence of different polymorphic method with the same name exists in the programming, primarily through subclasses of the parent class polymorphic cover, one design principle is to rely on the abstract, without depending on the particular, to increase flexibility. Polymorphism is to reflect this principle.

Examples to explain

  Here we assume that a scene of graphic area is calculated. Here we have an abstract base class shape. Then the other designs to inherit it.

    Class Design

    

    /// <summary>
    /// 抽象类
    /// </summary>
    public abstract class Shape
    {
        private string ShapeName { get; set; }
        public Shape(string shapeName)
        {
            this.ShapeName = shapeName;
        }

        /// <summary>
        /// 计算面积
        /// </summary>
        /// <returns></returns>
        public abstract double CalculateArea();
    }

 

    /// <summary>
    /// 长方形
    /// </summary>
    public class Rectangle:Shape
    {
        /// <summary>
        ////// </summary>
        public double Longm { get; set; }
 

        /// <summary>
        ////// </summary>
        public double Widem { get; set; }
        public Rectangle():base("Rectangle")
        {
            Longm = 0;
            Widem=0;
        }

        public override double CalculateArea()
        {
            return Longm * Widem;
        }
    }
    /// <summary>
    /// 圆形
    /// </summary>
    public class Circle: Shape
    {
        /// <summary>
        /// 半径
        /// </summary>
        public double R { get; set; }

       
        public Circle(): base("Circle ")
        {
            R = 0;
        }

        public override double CalculateArea()
        {
            return Math.PI*R*R;
        }
    }

   transfer

   

class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            Console.WriteLine ( " Please select the calculated pattern area: rectangle (A) / round (B) " );
             String answer = Console.ReadLine ();
             IF (answer == " A " ) 
            { 
                Double longm = 0 ;
                 Double widem = 0 ;
                 the try 
                { 
                    Console.WriteLine ( " Please enter the length: " );
                     longm = double.Parse(Console.ReadLine());
                    Console.WriteLine("请输入宽:");
                     widem = double.Parse(Console.ReadLine());
                }
                catch (Exception)
                {
                    Console.WriteLine("请输入数字!");
                }

                Rectangle rectangle = new Rectangle();
                rectangle.Longm = longm;
                rectangle.Widem = widem;
                Console.WriteLine ($ " this is the area of a rectangle rectangle.CalculateArea {()} " ); 
            } 
            the else 
            { 
                Double R & lt = 0 ;
                 the try 
                { 
                    Console.WriteLine ( " Please enter the radius: " ); 
                 R & lt = int .Parse (Console .readline ()); 
                } 
                the catch (Exception) 
                { 
                    Console.WriteLine ( " Please enter the number! " ); 
                } 
                Circle Circle= New new Circle (); 
                circle.R = R & lt; 
                Console.WriteLine ($ " this is the area of a circle circle.CalculateArea {()} " ); 
            } 
        } 
    }

to sum up

   This effect is not great practical cases, it is convenient to explain the main package inheritance polymorphism appreciated, in the examples, the name of the graphics package, an abstract class graphical abstract base class inherits this round and rectangular base class. rewriting achieve polymorphism override area calculation. More still needs the actual project in practical application.

 

  


                      c # explain the basics series

 

  Welcome to scan the next Fanger Wei code, and the more I learn C # knowledge together

 

  

Guess you like

Origin www.cnblogs.com/hulizhong/p/11227509.html