05 object-oriented basics, 05 object-oriented

Source: Internet
Author: User

05 object-oriented basics, 05 object-oriented

I would like to sum up my study notes over the past two days. I hope I can better learn new things.

 

1. object-oriented concepts
Object-oriented features: encapsulation, inheritance, and Polymorphism

 

2. class objects are passed by reference.
Passing an object to a method also transmits a reference to the object itself. modifying this object will affect the external objects.

 

3. null
Indicates that the variable does not point to any object.
Value Type (ValueType): values (int, long, double, etc.), boolean, and other basic types. The enumerated and struct values are copied and transmitted, and cannot be null;
String is not a value type

 

4. Local variables and member variables
(1) local variables must be initialized. The member variables are initialized by default (the initialization value of the basic value type is 0, the default initialization value of the boolean type is false, and the initialization value of the string type is null)
(2) When member variables and local variables (function parameters can also be seen as local variables) are duplicated, member variables are considered as local variables to avoid confusion, add "this. "," this "indicates the current object;

 

5. public and private

Class test {public int age; // modified by public. This member variable can be accessed externally and by the class itself; // modified by private, this member variable can only be accessed within this class. // you can declare a setName method to provide it to external entities. You can use this method to assign a public void setName (int name) value to the internal private variable name) {this. name = name ;}}

You can declare the member variables as public or private. If the member variables are declared as private, you can only call the private member within the class.


Public members can be accessed inside or outside the class, and private members can only be accessed internally. This protects internal members who do not want external calls, including (Field) variables and methods) not Externally accessed


Field/Member Variable (Member Variable) is generally declared as private. Use the get/set method to set values or assign values;

 

6. Attributes
To avoid assigning values to member variables randomly, you must declare the member variables as private and then provide the get/set method, which is difficult to write and call. C # provides a property syntax.
Attribute Declaration
Declare attributes for a private member variable to read and write data to the outside world

Private int age; // declare a private variable. The public int Age {get {return this. age; // The value of the Age variable is obtained from the outside through the return value of age.} set {// value is the value passed by the outside world, when the attribute value is assigned, call the set Method to assign the external value to the private variable age this of this class. age = value ;}}

Attribute simplification. For simple get/set logic, you can write more easily:

public int Age{    get;    set;}

Get and set can be declared as private and protected separately, so that different access levels can be set.

If only the get or set method of the attribute is read-only or only the attribute is written. Read-only writes cannot be abbreviated.

 

7. Getting started with Constructor
(1) constructor is a special function that creates class objects and initializes classes before they are created. If the defined class is a constructor without declaration, a non-argument constructor is provided by the compiler during compilation. If any constructor with parameters is defined, no default constructor is provided.


(2) format and features of constructor:
The method name must be the same as the class name.
No Return Value Type
Constructors can be overloaded.

The following is a sample code:

 

 

1 class Person 2 {3 // The constructor without parameters. 4 // if you re-write a constructor, the constructor without parameters will be killed by default, write a 5 public Person () // No return value. The method name and class name are the same 6 {7} 8 9 public Person (int age) // only one parameter has a constructor 10 {11 // this. age = age; 12 Age = age; 13} 14 15 public Person (int age, string name) // constructor with two parameters 16 {17 // this. age = age; 18 Age = age; 19 // this. name = name; 20 Name = name; 21} 22 23 private int age; 24 25 public int Age26 {27 get {return age;} 28 set {age = value ;} 29} 30 private string name; 31 32 public string Name33 {34 get {return name;} 35 set {name = value;} 36} 37 public void SayHello () 38 {39 Console. writeLine ("name =" + name + "; age =" + age); 40} 41} 42 43 // call 44 static void Main (string [] args) 45 {46 Person p1 = new Person (); // when a new object is called, no parameter constructor 47 p1.SayHello (); 48 49 Person p2 = new Person (19 ); // call the Construction Method 50 p2.SayHello (); 51 52 Person p3 = new Person (30, "James "); // call the constructor with int type parameters and string type 53 p3.SayHello (); 54 55 Console. readKey (); 56}View Code


8. Introduction to static
(1) In some scenarios, multiple instances of a class are required to share one member. Sometimes, you want to define methods that are not associated with a specific object and can be called without new ones;
Example: WriteLine of the Console class; Show of MessageBox
(2) The static method can be called directly by class name without the need for new;
(3) static variables are shared memory and non-static variables are isolated from objects;
(4) The keyword this cannot be used in the static method, because static exists independently of objects and is not unique to anyone;
(5) static members can only access static members, but cannot access non-static members. Non-static members can access static members.

 

9. namespace
You can use the file name and folder in the file system to explain the namespace problem.


Namespace Syntax: namespace name

Classes in the current namespace do not need to be referenced; use using to reference classes in other packages;


You can also use "System. data. sqlClient. sqlConnection ", no using, applicable to the use of multiple duplicate classes at the same time, better than the alias in uising;

 

10. Inheritance)


C # A class can be "inherited" from other classes. If A inherits B, A is the subclass of B, and B is the parent class (base class) of ). Subclass inherits all non-private members from the parent class. Child classes can also have child classes;


C # A class can have only one parent class. If no parent class is specified, System. Object is the parent class.


By default, the constructor of the subclass accesses the non-argument constructor of the parent class. After the constructor of the subclass, there is a default statement base ()

Class Fu {public Fu () {Console. writeLine ("FU") ;}} class Zi: Fu {public Zi (): base () // whether or not base () is explicitly called (), when a new subclass object is created, the Console outputs fu {Console. writeLine ("zi") ;}// new subclass object, first execute the constructor of the parent class to initialize the parent class, and then initialize the Child class. Zi z = new Zi ();

 

You can use base (parameter) to access the parameter constructor in the parent class. See the following code:

Class Fu {public Fu (int a) // The parent class defines a constructor with parameters, and no constructor without parameters {Console. writeLine ("FU" + a) ;}} class Zi: Fu {public Zi (): base (0) // if the parent class has no constructors without parameters, you need to display other constructors with parameters that call the parent class {Console. writeLine ("ZI");} public Zi (int a): base (a) {Console. writeLine ("ZI" + );}}

If a constructor with parameters is defined, there is no constructor without parameters. If the parent class has no constructor without parameters, you must display other constructor with parameters that call the parent class.

 

11. Differences between private, public, and protected
Public members can be accessed by all classes.

The private member cannot be accessed by the quilt class or external class. The subclass can only access the private member of the parent class indirectly through the public method of the parent class. This ensures the security of the parent class private member.
The protected member can only be accessed by itself and its own subclass (either directly or indirectly), and cannot be accessed by the "outer surname.

 

12. Polymorphism
Object-oriented features: encapsulation, inheritance, and polymorphism. Polymorphism is one of the most powerful object-oriented features and the most difficult features. design patterns are the manifestations of polymorphism.
The same method defined in the subclass as in the parent class is called override or overwrite. The override method in the parent class must be declared as virtual.

 

Override Modifier

Class DiQiuRen {// public void SayHello () public virtual void SayHello () // In the parent class, Mark virtual {Console in the way that the subclass can be override. writeLine ("I am from Earth");} class ZhongGuoRen: DiQiuRen {// public void SayHello () public override void SayHello () // override {Console. writeLine ("I am a Chinese ");}}

Call:

// Call static void Main (string [] args) {ZhongGuoRen zgr1 = new ZhongGuoRen (); zgr1.SayHello (); // The override parent class method of the Chinese subclass, therefore, ZhongGuoRen's SayHello method is called. // you can use the parent class variable to point to the subclass object DiQiuRen dqr1 = new ZhongGuoRen (); dqr1.SayHello (); // The Implementation of The called method is the implementation of the object, rather than the implementation of the variable. The method of the new object is called. // dqr1.BaiNian (); no matter what type of object actually points to, what method can be called depends on the type of the variable}

The parent class variable can point to the subclass object. It is also a subclass object in the memory. It does not matter what type this object variable is, the method to execute depends on the type of objects in the memory.

 

Abstract class

Abstract class abstract methods are mainly used to restrict sub-classes from "must implement these abstract methods". sub-classes can also not be implemented, so sub-classes are also abstract classes that are implemented by sub-classes. Imagine: If the subclass inherits the abstract class but you have not implemented the abstract method, what if you call the abstract method? I have no specific implementation of this abstract method. how do you call it?

Abstract Method
The abstract method does not have a method body. Once an abstract method is defined in the class, the class must be modified to abstract; the abstract class cannot be instantiated (new );

 

13. Interfaces

Syntax:

Public interface ISpeakable {void speak (); // it does not provide the specific implementation of the object and does not need to be used even}

(1) An interface is a type that declares "capabilities" and does not provide specific implementations.
(2) implementation methods are not provided, and neither {} can be provided.
(3) The interface cannot be instantiated and can only be "implemented" by the class"

Public class TeacherCang: ISpeakable // inherited interface {public void speak () // method for implementing the interface {Console. WriteLine ("yamadie ");}}

(4) You can use both interface type variables and class type variables to call speak.
(5) The interface defines "what to do" and "How to do"
(6) An interface that does not implement Code cannot declare a variable (field ).
(7) The interface can define multiple methods, or do not define any methods (* Identify the interface)
(8) The interface is a "Capability", not an implementation. Therefore, the constructor cannot be defined (this is not necessary)
(9) the class has only one parent class, and the class can implement multiple interfaces.

 

Related Article

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.