Summarize the two days of the study notes, is so-called warm and know new, hope to better learn the new knowledge behind
1. Object-oriented concept
Object-oriented three features: encapsulation, inheritance, polymorphism
2. Class object is a reference pass
Passing an object to a method is also a reference to the object itself, and modifying the object will affect the outer object
3, NULL
Indicates that the variable does not point to any object
Value types (ValueType): base types such as numeric types (int, long, double, and so on), Boolean, enumeration, struct value is copy pass, cannot be null;
String is not a value type
4. Local variables and member variables
(1) The local variable must be initialized, the member variable declaration has been initialized by default (basic numeric type initialization value is 0; Boolean type default initialization value is false;string type initialization value is null)
(2) When member variables and local variables (function parameters can also be regarded as local variables) when the name of the member variable is considered a local variable, in order to avoid confusion, access to the member variable with "this.", "This" represents the current object;
5. Public and private
class test{ publicint age; Modified by public, this member variable can be accessed by the external and this class itself privatestring name; is modified by private, this member variable can only be accessed within this class // can declare a SetName method to be provided to the outside world Outside the private variable that is manipulated by this method to the inside name assignment public void setName (int name) { this. name=name; }}
Member variables can be declared as public or private, and member variables declared as private can only call private members within the class
Public members can be accessed internally or externally by the class, and private members can only be accessed internally, which protects internal members that do not want to be called by the outside world, including (field) variables, methods, and not being accessed by outsiders.
The field/member variable (Member Variable) is generally declared private. Value/Assignment by Get/set method;
6. Properties
To avoid any random assignment of member variables, you must declare the member variable as private, and then provide the Get/set method, which is cumbersome to write and call, and C # provides a syntax for the property
Declaration of a property
To declare a property to a private member variable to read the write to the outside
private int age;// declares a private variable that cannot be accessed by the outside world public int age{ get { return this . Age;// The outside world gets the value of the age variable through the return value of age, Span style= "color: #0000ff;" >set { // value is the external value, the property is assigned to call the Set method to assign the value of the outside world to the private variable of this class age this . Age=value; }}
The simplification of attributes, if simple get/set logic, can be simpler:
Public int age{ get; Set ;}
Get, set can be individually declared as private, protected, so that you can set different levels of access
If the property is only get or the Set method is read-only or write-only property. Read-only writing is not shorthand.
7. Getting Started with constructor functions
(1) A constructor is a special function that creates a class object and initializes the class before the creation is complete. If you define a class that does not have a declaration constructor, the compiler will default to a parameterless constructor when it compiles, and if any of the parameter constructors are defined, no default parameterless constructor is provided.
(2) The format and characteristics of the constructor:
The method name must match the class name
No return value type
Constructors can overload
The following is a code example:
1 class Person2 {3 //the method of constructing without parameters,4 //If you re-write a constructor, the default parameterless constructor will be killed, you need to write a5 PublicPerson ()//no return value, same method name and class name6 { 7 }8 9 PublicPerson (intAge//A parameter constructor with only one argumentTen { One //this.age = age; AAge =Age ; - } - the PublicPerson (intAgestringName//constructors with two parameters - { - //this.age = age; -Age =Age ; + //this.name = name; -Name =name; + } A at Private intAge ; - - Public int Age - { - Get{returnAge ;} - Set{age =value;} in } - Private stringname; to + Public stringName - { the Get{returnname;} * Set{name =value;} $ }Panax Notoginseng Public voidSayHello () - { theConsole.WriteLine ("name="+name+"; age="+Age ); + } A } the + //called - Static voidMain (string[] args) $ { $person P1 =NewPerson ();//The new object is called without a parameter construction method - P1. SayHello (); - theperson P2 =NewPerson ( +);//constructor that calls only int type arguments - P2. SayHello ();Wuyi thePerson P3 =NewPerson ( -,"Zhang San");//calling a constructor with an int type argument and a string type - P3. SayHello (); Wu - Console.readkey (); About}View Code
8. Static Introduction
(1) In some scenarios, multiple instances of a class are required to share a member; Sometimes you want to define a method that is not associated with a specific object and does not require new to be called;
Example: the console class of the Writeline;messagebox show
(2) The static method can be called directly from the class name without the need for new.
(3) The static variable is the shared memory space, and the non-static variable is Object-isolated;
(4) The This keyword cannot be used in the static method, because static is independent of the object and is not unique to anyone;
(5) Static members can access only static members and cannot access non-static members. Non-static members can access static members.
9. Namespaces
You can use file names and folders to explain namespace problems
Namespace syntax: namespace namespace name
The classes in the current namespace do not need to be referenced, and the using using references to classes in other packages;
It is also possible to use "System.Data.SqlClient.SqlConnection" in the form of "namespace + class name", without using, for classes that use multiple names at the same time, better than uising aliases;
10. Inheritance (Inherit)
A class in C # can "inherit" from other classes, if a inherits B, then a is called a subclass of B, and B is called a parent class (the base class). Subclasses inherit all non-private members from the parent class. Subclasses can also have sub-classes;
A class in C # can have only one parent class, and if no parent class is specified, System.Object is the parent class
The constructor method of a subclass defaults to accessing the parent class's parameterless construction method, and there is a line of default statement base () after the subclass's construction method.
classfu{ PublicFu () {Console.WriteLine ("FU"); }}classzi:fu{ PublicZi ():Base()//regardless of whether you explicitly call base (), the console will output FU when the new subclass object{Console.WriteLine ("Zi"); }}//when the new subclass object is executed, the constructor of the parent class is first performed to initialize the parent class, and then the subclass is initialized. Zi z =NewZi ();
You can access the parameter constructor in the parent class through base (parameter). See the following code:
classfu{ PublicFu (intA//The parent class defines a constructor that has a parameter, and no parameterless constructor{Console.WriteLine ("FU"+a); }}classzi:fu{ PublicZi ():Base(0)//If the parent does not have a parameterless constructor, you need to display the other argument constructors that call the parent class{Console.WriteLine ("ZI"); } PublicZi (intA):Base(a) {Console.WriteLine ("ZI"+a); }}
If you define a constructor that has a parameter, there is no parameterless constructor, and the parent class does not have a parameterless constructor, you need to display the other argument constructors that call the parent class
11. The difference between private, public and protected
Public members can be accessed by all classes
Private members cannot be accessed by subclasses and external classes, and subclasses can only access private members of the parent class indirectly through the public method of the parent class. This guarantees the security of the private member of the parent class.
Protected members can only be accessed by themselves and by their own subclasses (directly or indirectly) and cannot be accessed by "alien".
12. polymorphic
Object-oriented three major features: encapsulation, inheritance, polymorphism. Polymorphism is one of the most powerful features of object-oriented, it is also the most difficult one, and the design pattern is the manifestation of polymorphism.
The same method defined in the subclass as the parent class is called overriding (override) or overwrite, and the method that can be overridden in the parent class is declared as virtual.
Override modifier
classDiqiuren {//Public void SayHello () Public Virtual voidSayHello ()//in the parent class, the method that you want subclasses to override is annotated with virtual{Console.WriteLine ("I'm from the earth."); } } classZhongguoren:diqiuren {//Public void SayHello () Public Override voidSayHello ()//The same method in the override parent class in the subclass must be labeled override{Console.WriteLine ("I am a Chinese"); } }
Call:
// call void Main (string [] (args) {zhongguoren zgr1 = new Zhongguoren (); Zgr1. SayHello (); // // Diqiuren dqr1 = new Zhongguoren (); Dqr1. SayHello (); // The method implementation of the call is also the implementation of the object, not the implementation of the variable, or the method that invokes the new object // dqr1. Bainian (); Regardless of what type of object is actually pointed to, what method can be called depends on the type of the variable }
The parent class variable can point to the child class object, the memory is also the subclass object, has and only this one object variable is what type does not matter, actually executes whose method mainly depends on the memory object is what type
Abstract class
Abstract class abstraction methods are mainly used to restrict subclasses "must implement these abstract methods"; Subclasses can also not be implemented, then subclasses are abstract classes, subclasses of subclasses ... To achieve. Imagine if a subclass inherits an abstract class and you don't implement an abstract method, what if you call an abstract method? I have no concrete implementation of this abstract method, how do you call me?
Abstract methods
Abstract methods have no method body; Once an abstract method is defined in a class, the class must be decorated as abstract; the abstract class cannot be instantiated (new);
13. Interface
Grammar:
Public Interface ispeakable{ void speak (); does not provide the body specific implementation even {} is not necessary}
(1) An interface is a type of declarative "capability" that does not provide specific implementations
(2) Do not provide the implementation method, even {} can not have
(3) The interface cannot be instantiated and can only be "implemented" by the class
Public class Teachercang:ispeakable// Inheritance interface { publicvoid speak ()// ways to implement an interface { Console.WriteLine ("yamadie");} }
(4) You can use either an interface type variable or a class type variable to call speak
(5) The meaning of the interface is to define "what to do", the class defines "how to do"
(6) cannot declare a variable (field) in an interface declaring a variable with no implementation code is meaningless
(7) An interface can define multiple methods, or it can define no method (* Identity interface)
(8) The interface is "capability" is not an implementation, so it is not possible to define a constructor (not necessary)
(9) class has only one parent class, classes can implement multiple interfaces
05 Object-Oriented Basics