C # Basic Knowledge series 7 (base, this, new, override, abstract, virtual, static)

Source: Internet
Author: User

This article mainly explains the keywords that I think are not very familiar with or are not very familiar with in C, it mainly includes base, this, new, override, abstract, virtual, and execution problems between static fields and static constructors.

The base keyword is used to implement access to the public or protected members of the base class in the derived class, but is limited to constructors, instance methods, and instance attribute accessors:

  • Call methods that have been overwritten by other methods on the base class.

          
  • Specify the base class constructor to call when creating a derived class instance.

       Name { ; =  

It is incorrect to use the base keyword in a static method.

It is used to reference the current instance of the class, and also includes inherited methods. this can usually be hidden:

  • Restrict hidden members with similar names
       Name { ;   Age { ;  Person( Name, .Name =.Age =
  • Passing objects as parameters to other methods
       Name { ;   Age { ;  Person( Name, .Name =.Age = + 
  • Declare Indexer
     [] PersonList =  [  [ {  { PersonList[param] =

I. new operator

1. When a new class is added, new completes the following two aspects: 1. Call the new class command to allocate memory to the instance in the managed heap; second, call the constructor to initialize the object.

2. When a new struct is created, the new operator is used to call its with constructor to complete instance initialization.

3. When a new int is used, the new operator is used to initialize the value of 0.

4. The new operator cannot be overloaded.

5. An OutOfMemoryException occurs when memory allocation fails.

Ii. new Modifier

The new Keyword can explicitly hide members inherited from the base class. When the inherited member is hidden, the derived version of the member replaces the base class version. Although you can hide a member without using the new modifier, a warning is generated. If

When a member is explicitly hidden with new, the warning is canceled and the fact that the member is replaced with a derived version is recorded.

 

In the subclass, The new Keyword is used to modify the method defined with the same name as the parent class, which is called overwrite. Overwriting does not change the function of the parent class method.

            Main(= = = 

When A subclass is used to create A parent class, such as A c = new B (), overwriting does not change the function of the parent class and still calls the function of the parent class. (This is different from override. I will explain it below)

Iii. new constraints

The new constraint specifies that any type parameter in the generic class declaration must have a common non-parameter constructor. If you want to use the new constraint, this type cannot be an abstract type.

When used with other constraints, the new () constraint must be specified at the end:

      ClassA<T> T : IComparable,     }

Override modifiers must be used to extend or modify the abstract or virtual Implementation of inherited methods, attributes, indexers, or events.

The method declared by override is called the override base method. The override base method must have the same signature as the override method.

You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override.

 

The virtual method is called the virtual method. You can use override to declare a method with the same name in the subclass. This is called "override ". The corresponding method without virtual modification is called its real method. Rewriting changes the function of the parent class method.

            Main(= = = 

 

New and override

1. Neither rewriting nor overwriting will affect the functions of the parent class.

2. When A subclass is used to create A parent class, for example, A c = new B (), rewriting will change the function of the parent class, that is, calling the function of the subclass, but not overwriting, the parent class function is still called.

3. Virtual Methods and real methods can be overwritten (new), Abstract methods, and interfaces cannot.

4. Abstract methods and Interfaces. Methods marked as virtual can be overwritten. Real methods cannot.

5. The frequency of rewriting is relatively high, achieving polymorphism; the frequency of overwriting is relatively low, used to inherit classes that cannot be modified before.

For the abstract keyword, the following five points are summarized:

1. The class defined with the keyword abstract is an abstract class, which can only be used as the base class and cannot be instantiated.
2. classes defined by abstract may not necessarily contain abstract methods, but also non-abstract methods.
3. abstract methods must be included in abstract classes.
4. abstract classes cannot be defined as sealed Classes. Abstract METHODS cannot use virtual, static, or private modifiers.
5. If the derived class does not implement all abstract methods, the derived class must also be declared as an abstract class.

 

Virtual Method)

Virtual keywords are used to modify methods in the base class. Virtual instances can be used in two scenarios:

Case 1: The virtual method is defined in the base class, but the virtual method is not overwritten in the derived class. In the call to a derived class instance, the virtual method uses the method defined by the base class.

Case 2: The virtual method is defined in the base class, and then override is used in the derived class to override the method. In the call to a derived class instance, this virtual method uses a derived override method.

It mainly describes the execution sequence:

1. during compilation, the compiler first analyzes the required static fields. If the class of these static fields contains a static constructor, field Initialization is ignored; if there is no static constructor, the static fields will be initialized.

2. If there are multiple static classes, the initialization sequence of the static members will be initialized first according to the reference sequence, however, if the initialization of static members of a class depends on the static members of other classes, the dependent static members will be initialized first.

3. The static fields of classes with static constructors are initialized only when referenced.

Here are two simple examples:

        X = B.Y+    Y=A.X+  Main(

What are the results? Let's take a look at the second small example:

        X = B.Y+    Y=A.X+  Main(

By comparison, if the two examples are the same as the results after your execution, you should have understood the execution sequence.

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.