[.net Basics of Object-Oriented Programming] (8) Examples of classes and classes

This article is transferred from: https://www.cnblogs.com/yubinfeng/p/4552274.html

Category, as the name implies, means classification and category. If we want object-oriented programming, we need to classify different things. Classes can be said to be the core of .net object-oriented.

Class: is an abstract collection of objects with the same properties and functions.

1. Class definition 

    <Access modifier> class <class name> {class members (fields, attributes, methods, events)}

   For example, for example, "human" is a class that can contain attributes such as gender, race, country, etc. In addition to including attributes, it also contains some other functions, such as: eating, sleeping, etc., which can be understood as a class method.

code show as below:    

Copy code
1 class Person 
 2 { 
 3 public string gender {get; set;} 
 4 public string race {get; set;} 
 5 public string Country {get; set;} 
 6 public string Eat (string strCountry) 
 7 { 
 8 switch (strCountry) 
 9 { 
10 case "United States": 
11 
12 return "Love to eat Western food"; 
13 
14 case "Korea": 
15 
16 return "Love to eat kimchi"; 
17 default: 
18 return "Don't know";
19                 }
20             }
21         }
Copy code

 

  For class modifiers, the five access modifiers described in the previous section are supported

Access modifier

Explanation

public

Public access. Without any restrictions.

private

Private access. Access is restricted to members of this class, and subclasses and instances cannot be accessed.

protected

Protect access. Access is limited to this class and subclasses, and instances cannot be accessed.

internal

Internal access. Access is limited to this project, others cannot be accessed.

protected internal

Internally protected access. Access is limited to this project or sub-category, others cannot be accessed

  •  The default modifier of the class namespace is: public
  •  The default access modifier for the class is: internal 
  •  The default access modifier for members of the class is: private
  •  The scope of a class member can never exceed the class that contains it
  •  You can declare a static class static class ClassName, but all its members must be explicitly defined as static

2. Class instantiation

  An instance of a class: it is a real object of the class

  <Class name> <instance name> = new <class name> ([parameter of constructor])

    For example, the human defined above is a class, and a specific individual is an instance of human. The process from class to class instance is called class instantiation.  

Person person=new Person();

3. Constructor

The constructor, also called the constructor, is the method that initializes the class.

Its name is the same name, there is no return value, and there is no need for void, which is called when new.

All classes have constructors, but C # will default to a parameterless constructor, if not defined, the default will be used.

If a constructor is defined, it is invalid by default.

4. Nested classes

     C # allows class nesting

     Nested types are considered to be members of outer types, so nested types can access all other members of outer types.

     According to the principle that the default access modifier of class members is: private, the default access modifier of nested classes is private

     Nested types can freely access members of outer types, regardless of whether those members are private. If the outer type wants to access the nested type, it is subject to the access rules.

     Regarding the inheritance of nested classes (the inheritance will be explained in detail later), if a type inherits a type that contains nested types, then it will naturally inherit the nested types in the base type, because that nested type itself is the base A member of the type. Therefore, as long as the nested type is not private or cannot be inherited, the newly declared nested type in the subtype can also inherit that nested type.

Here is the nested class code:

Copy code
1 static class Tree
2 {
3     private  static string _treeLeafColor = "Green";
4     static class  AppleTree
5     {
6         private static string appleTreeLeaf = _treeLeafColor;
7     }
8 }
Copy code

5. Distributed class

Some classes have complex functions, with a large number of fields, attributes, events, methods, and even nested members. If all the class definitions are written together, the file is huge and the code lines are large, which is not easy to understand and debug.

Characteristics of distributed classes:
A, the same class name;
B, with partial modifier;
C, with the same namespace;

Copy code
// The file name is partOne.cs 
using System; 
public partial class example 
{ 
  public void method1 () {} 
} 
// The file name is partTwo.cs using System; public partial class example { public void method2 () {} }
Copy code

Both methods belong to the class example but are put in different files. After the editor finds the partial, the class name and namespace are the same, and the files are automatically compiled together to form a complete class.

Key points:

1. Classes can be declared as static classes using the modifier static. After declaring static, all members of the class must be static;

2. The default access modifier of a class is: internal. When you define a private class, you need to add the modifier private; and the default access modifier of the members of the class is: private, public methods need to be declared as: public

3. Class naming convention: use the first letter capitalization rule, do not use the system class name or the same name as the modifier;

Guess you like

Origin www.cnblogs.com/hanguoshun/p/12728348.html