2019.9.01 encapsulation, inheritance, polymorphism

A, Package:

Package: The objective things packaged as such, ease of use and modification;

Function and structure is similar to the method used, different program execution process;

Highlights: member variables, properties, member methods, constructors, member method static and non-static, namespace, commonly used in public access modifier public, parvate private, only the class itself can access, internal default, the same name under space can be accessed, protected protected, under the same inheritance can be accessed;

Duplicate names: the same scope allowed under the same name issue, most short answer is to explain the scope of a flower in parentheses, and each cycle is a different case of the same name braces are not related;

1, the basic member variable ***** "broker"

class student // New Class

{

  private string _name; // member variables 

  public string name // property

  {  

  get {return _name;} // return function is to throw out a value.

            // If you only get, it is only out of a fixed output value;

  set {_name = value;} // if the statement is executed student.name = "abcde"; then value of this is "abcde";

            // value of the role is to give the member variable values ​​from the outside.

  }

2, ***** member variables can have multiple "broker", and the data types can be inconsistent

  private bool _sex;

  public bool sex

  {

    get{ return  _sex; }

    set{ _sex  =  value; }

  }

  public string sexabc

  {

  get {return "abcd" + _sex;} // if the statement is executed student.name = "abcde"; then value of this is "Enter to false";

               // If the statement is executed student.name = "false"; this value is the value of "abcdfalse";

  set {

    if(value == "false")  

      _sex = value; 

    else

      _sex = "Please enter false";  

    }

  }

3, the constructor *****

class student 

{

  public student () {} // create a constructor is to execute this step; the default is not written out, but real;

 }

student s = new student (); // This process is called instantiation process; and Student () This method is a constructor;

Summary: The process of rebuilding the object is to instantiate the class of the process;

Examples of the process is the constructor function during execution;

Constructor Create is formed after completion of the class;

4, ***** member method

  private string  _name;

  public  string name

    {

      get { return  _name; }

      set { value  =  _name; }

    }

    public string say () // Create a method

    {

      return "la la la" + _name;

    }

    student  s  =  new  student(); 

    s.name = "abcd";

    string str = s.say();

    Console.Write (s.str); // output is the value returned by this method say, in this case, La La ABCD;

Summary: Within the constructor can give private assignment; if it will be reassigned to private coverage value assigned in the constructor, new output, if there is no assignment, then the default constructor is given in the output value of the outside;

5, static method *****

Containing static keyword is a static method;

Examples of static can not use static methods;

Static methods belong to the class itself, as a class name to point out; public static int abc () {} assignment method is: student.abc (); student is the class name;

5, assembly or namespace *****

namespace;

Need to reference using space name;

Second, inheritance

public class fly // parent class, this is the class name;

{

}

public string brid: fly // sub-class, the class name; can inherit the parent's use of public property;

{

}

public string little brid: brid // grandson class, the class name; class can inherit his grandfather, communal property of the parent class;

{

}

1, the parent class, the base class;

2, subclass, derived class;

3, there may be numerous parent category younger;

4, a subclass can have only one parent;

Third, polymorphic

public class brid     

  {

    public virtual string eat()

      {  return "aaaaa";  }

  }

 

public class little brid:brid

{

  public override string eat()

    { return "abcdefg"; }

}

1, is possible as the method name, will be displayed overloaded, different parameters;

2, virtual virtual, override rewrite, voerload overloaded;

3, an abstract class ***** / abstract methods:

abstract class fly

{

  public abstract string eat();

}

class brid :fly

{

  public override string eat()

    { return "aaaaaa"; }

}

Abstract methods must be placed in an abstract class;

An abstract class can not be instantiated;

If there is an abstract class abstract methods, abstract classes all inherit times over the younger classes will need to return a value;

An abstract class is not an abstract method only;

Abstract classes can only use as a parent;

abstarct keyword;

4, the interface *****

interface fly

{

  string flying();

}

Keyword interface;

Interface can not have a substantive way;

Can not be instantiated;

Not need access modifier, the default public;

Can only be inherited;

All interfaces are all inherited methods must achieve; otherwise error;

Guess you like

Origin www.cnblogs.com/LiTZen/p/11444242.html