Object-oriented deep understanding 3

Source: Internet
Author: User

Object-oriented deep understanding 3

Namespace. When two classes are not in the same namespace, We need to import the namespace. Shift + ALT + F10. This shortcut key is widely used, including when implementing interfaces and inheriting abstract classes. (CTRL +.

 

Figure 1. Class Library and form applicationProgramDifferences from winform

The problem is, what should I do if I want to call class1.cs in the class library in the program class?

Under the 8.23 review content, right-click the reference and choose add reference. In the displayed dialog box, select the project to be imported and click OK.

Then you can import the namespace.

 

 

Figure 2. Import a namespace

In our previous blog:Section on other issues in C #We also introduced how to import a namespace, which is about importing a namespace during serialization.C #Ing functions and namespaces inThe basics described in. Now let's take a look. Have we expanded it?

 

Scope of variables:

 

Figure 3. Scope instance 1

 

Figure 4. How to define the scope of a Variable

 

Figure 5. Local Variables

 

Figure 6. member variables

The default value of bool is false, and the default value of string is null. The local variable does not have a default value, so it must be declared and assigned a value before use.

InC #Method In (top ):We raised the question of local variables. Do you still remember the examples of the two governors?

 

Detailed encapsulation:

Aside from the concept of a program, let's think about the encapsulation in our life?

For example, Microsoft products have the same functions for users, but their internal encapsulated content is different.

Advantages of encapsulation: convenience

1. attributes are encapsulation and fields are encapsulated.

Encapsulation of attributes: encapsulates changed things. For users, only unified access is provided.

2. Multiple parameters of the method are encapsulated into an object.

3. Add a bunchCodeEncapsulated in a method (quiz game)

4. encapsulate some functions into several classes (calculator +-*/encapsulate into a class

5. encapsulate some code with the same functions into a program set (DLL, exe) and provide a unified access interface. (Attribute name, method name, etc .)

 

Figure 7. Multiple parameters of a method are encapsulated into an object

Detailed inheritance

Inheritance is the same as what we understand in our life. The more specific the subclass is. Subclass is used to extend the parent class.

Inheritance is the relationship between classes.

 

What are the benefits of inheritance?

One of the reasons is code reuse. The second reason is polymorphism.

 

Inherited features:

1. Single-stick.

2. Transmission

 

Figure 8 inheritance

How can we solve the single problem? We will explain the interface later to solve this problem.

 

For example, students of Physical Education inherit from students, while students inherit from humans.

 

Figure 9. Object (base class)

Let's talk about the constructor in inheritance.

Since constructors cannot be inherited, constructor In the subclass inherits the constructor in the parent class by default.

The first solution is to add a non-parameter constructor to the parent class.

 

Figure 10. constructor I in inheritance

 

 

Figure 11. Question 2 of constructor in inheritance

The second solution is to use the base keyword in the constructor In the subclass to call the constructor with parameters in the parent class.

Now we have a problem. We will annotate the name, age, and email code in the constructor In the subclass. Directly input a value after the base keyword in the subclass.

Instantiate the subclass of the entry function and attach different values to the subclass. So what is the display in CN. name in the entry function?

Procedure:

The name of the sub-class is instantiated in the entry function. Then, the sub-class calls the constructor in the parent class, And the song is given to the name of the constructor in the parent class, then execute this. name = Name;, and these lines of code have been canceled. Therefore, it is useless to write Chuan in the entry function. The final result is: Yan Song.

Base initializes the constructor in the parent class, and then initializes the account parameter in its subclass. It is useless to instantiate the Sichuan value of Chinese.

If you change base in the subclass to base (name, age, email)

This is equivalent to passing the constructor In the subclass.

Then run: Chuan.

 

Base keywordPurpose:

1. Call the parent class constructor.

2. Call the parent class member * (later)

 

This keywordRole:

1. As a member of the current object to call the object.

2. Call other constructors in the current class.

This example shows how to call other constructors in the current class.

When we encounter the following situation, we can use it like this:

 

Figure 12 usage of this 1 and 2

How to modify a startup Item:

Tools --- options --- projects and solutions --- generate and run --- check for new solutions...

In this way, you do not need to modify the startup Item.

 

Short attribute code segment: Propfull

Notes for exercise questions:

 

Figure 13. inherit Exercise 1 (1)

 

Figure 14. inherit exercise Question 1 (2)

 

Figure 15. inherit Exercise 1 (3)

In the above questions, I have summarized some of the mistakes we often make. There are two solutions: one is to practice more and the other is to understand more.

 

Access modifier:

1. Private, which can only be accessed within the current class. (If the access modifier is not written, the default value is private .)

2. The access modifier of protected is protected. [Range: 1. the class can be accessed internally. 2. All subclasses can also be accessed internally.]

3. Internal indicates that it can be accessed within the current Assembly. When a reference is added, internal cannot be accessed. (How can I access it? Change internal to public .)

 

Who have higher access permissions for protected and internal? There is no comparability.

Protected can be accessed across datasets as long as it has a parent-child relationship. Internal cannot be accessed once it is cross-assembly.

 

4. Protected internal, which indicates that it has the permission of the protected access modifier and the permission of the internal access modifier.

5. Public indicates public and can be accessed anywhere.

 

The default access modifier of the class is internal, and the access modifier of the class members is private by default.

Class access modifier can only be public or internal

 

The above access modifier text can be carefully understood when writing code.

 

Private Members in inheritance:

First look:

 

Figure 16 private member inheritance question 1

    • Conclusion: Private Members cannot inherit from the quilt class.
    • Cause: 1. Although the Child class inherits the parent class, when the child class object is created, the Private Members in the parent class are also allocated with memory (also available in the Child class ). 2. however, inheritance is only a concept of object-oriented. The purpose is to use sub-classes (polymorphism and code reuse). Private Members cannot access sub-classes even if they inherit them. Therefore, in object-oriented, it is meaningless because it inherits but cannot be accessed.

Let's take a look at why there is a controversy?

See:

Figure 17. Disputes over private members in inheritance

Next, let's take a look at the access level constraints:

 

Figure 18 solution to [accessible inconsistency]

 

Figure 19. [accessible inconsistency errors] solution 2

 

Figure 20 solution 3

 

Figure 21. solution 4

 

Figure 22. Rishi replacement principle

 

Figure 23. Column replacement assignment

 

Figure 24. Rishi conversion Example 1

Check whether the above Code has been written a lot. The purpose is to call out the methods in the subclass. Next we will introduce a better way of writing:

 

Figure 25. Virtual Methods

 

Figure 26. Virtual method implementation Polymorphism

Summary of Virtual Methods:

1. Add the virtual keyword before the return value type of the method. This method becomes a virtual method.

2. When a method is a virtual method, the subclass can override this method in the parent class.

3. Use the override keyword when the subclass overrides the parent class method.

4. When the subclass overrides the methods in the parent class, it points to the subclass object through the parent class type variable. (Poor language expression and good code expression)

Person P = new Chinese ();

P. Say (); in this case, if the say method is rewritten in the Chinese class, the execution result is the say () method after being rewritten in the subclass,

If the Chinese class does not overwrite the say method, the execution result is also the say () in the parent class ()

5. The virtual method is one of the methods to achieve polymorphism.

 

New content:

Fast rewrite: Write an override + space + method to be rewritten + press enter, and write it directly.

 

Okay, this articleArticleWrite it here first.

List of recent articles by the author:
C # basic tutorial (free of charge, the best gift for code lovers. Note: The authors share their carefully organized basic C # tutorials without any commercial purposes.
I hope to share my experiences with more code lovers. Please give me more advice !!!)
Process-oriented, object-oriented, and advanced Process-oriented, object-oriented in-depth understanding 1
Process-oriented, object-oriented in-depth understanding 2
Winform Basics Winform Basics
Commonly used controls in winform
Process-oriented Comparison of three cycles
Method (I) in C)
Our common Array
Object-oriented Thought Change
C # super-class and easy-to-use classes
The use of destructor and namespaces in C #
C # super-class and easy-to-use strings
How to quickly process strings in C #
Value Type, reference type, and others
Arraylist and hashtable
Arraylist and hashtable
File Management
Polymorphism
Section on other issues in C #
GDI + The GDI + code I have collected over the years
The GDI + Code 2 I collected over the years
HTML Overview HTML language that you cannot ignore
You can't ignore HTML 2.
HTML language 3 that you cannot ignore

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.