Inner classes in C #

Source: Internet
Author: User

Recently looking at Java on the one hand of the book, see a very strange problem, Java class can also define a class, this structure is very special! Later found that I have shallow knowledge, the original C # also has internal classes, have not been aware of this grammatical structure!

There are several advantages to using inner classes:

(1) Abstract The behavior of an external class in a state, hidden implementation, by modifying the access modifier inside, you can set only the external class can access the class

(2) Extension of the namespace, you can put the class name of the outer class as a namespace of the inner class (here is just the equivalent, but not the real namespace)

(3) The inner class can be used as an extension of the outer class, which can be better encapsulated in the live.

The above features are randomly summed up, there may be some lied, following some specific examples:

1. Definition of the inner class:

Nested classes: Define another class in one class, mainly into static nested classes and non-static nested classes (also known as "inner Classes")

The definition structure of an inner class: (1) Defining a class directly in a Class (2) Defining a class in a method (3) Anonymous inner class

2. External classes accessing internal classes

External class Access inner class example

The code defines an outer class person and an inner class student, where variables and methods of various modifiers are used in the inner class student, and from the example above, the outer class is able to access only the fields, methods, and columns in the nested class that have the modifier public, internal, Property.

Calling the Show () method of the external class runs the following result:

3. Internal class access to external classes

1 namespace GameStatistical.Test.InnerClass
2 {
3 public class Person1
4 {
5 private string name;
6
7 public string Name
8 {
9 get {return name;}
Ten set {name = value;}
11}
private string sex;
13
public string Sex
15 {
get {return sex;}
set {sex = value;}
18}
19
public void Show1 ()
21 {
Console.WriteLine (this.name + "==>" + this.sex);
23}
24
private static void Show2 ()
26 {
Console.WriteLine ("===================>");
28}
29
internal void Show3 ()
31 {
Console.WriteLine (this.name + "==>" + this.sex);
33}
34
35
36
Notoginseng public class Student
38 {
Setper public void (string name, string sex)
40 {
Person1 p = new Person1 ();
P.name = name;
P.sex = sex;
44
P.SHOW3 ();
P.show1 ();
47}
48
49}
50}
51}

This code also defines an external class Person1 and an inner class student, Setper () in the inner class calls the method in the outer class, and writing this code we can find that the nested class can access the methods, properties, fields of the outer class without the restriction of the access modifier

4. Inheritance of inner classes

1 namespace GameStatistical.Test.InnerClass
2 {
3 public class Person
4 {
5 public class Student
6 {
7 public static int age;
8 internal static int height;
9 private static string sex;
10
public virtual void Show ()
12 {
Console.WriteLine ("Age:" +age);
Console.WriteLine ("Height:" +height);
15}
16
internal void Display ()
18 {
Console.WriteLine ("Internal");
Console.WriteLine ("Age:" + ages);
Console.WriteLine ("Height:" + height);
22}
23}
24
25
public void Show ()
27 {
Student.age = 21;
Student.height = 75;
Student Student = new Student ();
Student. Show ();
Student. Display ();
33}
34}
35}

inner class inheritance, where the inner class defines the parent class, where public virtual void Show () uses the virtual modifier, which can be used by subclasses to override this method, to see if the inner class inherits subclasses can override this method.

1 namespace GameStatistical.Test.InnerClass
2 {
3 public class Subperson:person
4 {
5 public class Substudent:student
6 {
7 public override void Show ()
8 {
9 base. Show ();
10}
11}
12}
13}

The code above overrides the show () method, which shows that the inheritance of the inner class can be

5. Reflective Inner class

For this code, as seen from other web sites, the reflection inner class we cannot directly operate directly through the "." operator, but through the "+" operator. As mentioned earlier, the inner class is also an effective way to manage namespaces, which is a bit different from the normal and inner classes.

Reflection Inner Class

Activator.CreateInstance ("GameStatistical.Test.InnerClass", "gamestatistical.test.innerclass.reflectionperson+ Student ");

Reflection General Class

Activator.CreateInstance ("GameStatistical.Test.InnerClass", "GameStatistical.Test.InnerClass.ReflectionPerson".  Student ");

In the actual operation, the inner class seems to use less, here is just a very simple introduction, as a knowledge point summed up.

Good text to the top concern me to collect the article

Inner classes in C #

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.