C # advanced programming note Day 3, August 1, September 8, 2016,

Source: Internet
Author: User

C # advanced programming note Day 3, August 1, September 8, 2016,

1. Virtual method: declare a base class functionVirtualIn any derived class.RewriteThis function.

2. in Java, all functions are virtual, but in C #, C # requires that you useOverrideExplicit keyword declaration.

// Parent class MyBaseClass {public virtual string VirtualMethod () {return "This is test! ";}}// Derived class MyDerivedClass: MyBaseClass {public override string VirtualMethod () {return" Haha, I override you ."}}

3. Neither the member field nor the static function can be declared as virtual, because this concept only makes sense to the instance function members in the class.

4. Hide method: if the method with the same signature is declared in both the base class and the derived class, but this method is not declared as virtual and override, the method of the derived class hides the base class method. In most cases, it is necessary to rewrite the method, rather than hide the method, because hiding the method will pose a risk of calling an error method for the instance of the given class. To hide a method in c #, use the new keyword.

class MyDerivedClass:MyBaseClass{    public new string VirtualMethod(){        //...        return 0;    }}

5. base class version of the called function: base. <MethodName> ()

  

class MyBaseClass{    public virtual string getName(){        return "Hello ,Robert!";    }}class MySonClass : MyBaseClass{    public override string getName(){        return base.getName();    } }

6. [Topic] abstract classes and abstract functions.(Abstract)

  Abstract class:

    • Abstract classes cannot be sealed ).
    • Abstract classes cannot be instantiated.
    • If a class contains abstract functions, the class is also abstract. It must also be declared as abstract.

  Abstract Functions:

    • Abstract functions cannot be implemented directly. They must be rewritten in a non-Abstract derived class.
    • Abstract functions are also virtual. (Although virtual keywords are not required, a syntax error is generated if the keyword is provided .) You do not need to explicitly write the virtual File.

7. sealing and sealing methods. Sealed

For a class: the class cannot be inherited.

For method: This method cannot be rewritten.

String is the seal class.

To use the sealed keyword on a method or attribute, you must first declare it as a method or attribute to be overwritten from the base class. If you do not want override methods or attributes on the base class, do not declare them as virtual.

8. [execution process of constructors in a derived class]

abstract class GenericCustomer{    private string name;}class NevermoreCustomer:GenericCustomer{    private uint highCostMinutesUsed;}GenericCustomer customer=new NevermoreCustomer();

 

[Execution process]: The Compiler first finds the constructor of the class it is trying to instantiate. In this example, It is NevermoreCustomer, the default NevermoreCustomer constructor first needs to run the default constructor for its direct base class GenericCustomer, and then GenericCustomer constructor is its direct base class System. object running default constructor, System. the Object does not have any base class, so its constructor executes and returns the control to the GenericCustomer constructor. Now, the GenericCustomer constructor is executed to initialize the variable name to null. Return the control to the NevermoreCustomer constructor, execute the constructor, initialize highCostMinuteUsed to 0, and exit.

The Calling order of the constructor is to call System. Object first, and then proceed from top to bottom according to the hierarchy. Until the class to be instantiated by the compiler is reached.

9. this and base

This is to call other constructor methods in the current class.

When base is used in a derived class, it calls the constructor in the base class.

10. interface. A predefined Microsoft interface, System. IDisposable. It contains a method Dispose (), which is implemented by the class and used to clean up the code.

  

1 public interface Idisposable{2     void Dispose();3 }

The interface is syntactically identical to the declared abstract class, but it cannot provide implementation methods for any member of the interface. Generally, an interface can only contain methods, attributes, indexers, and event declarations. An interface cannot be instantiated. It can only contain the signatures of its members. The interface neither has constructor nor field. The interface definition cannot contain Operator overloading, And the modifier about the member cannot be declared.Interface members are always common and cannot be declared as virtual or static.

11. [differences between abstract classes and interfaces in topic C]

  I. abstract classes

① An abstract class is a special class, but cannot be instantiated. In addition, it has other features of the class.

② Abstract classes can contain abstract methods, which are not supported by common classes.

③ Abstract methods can only be declared in abstract classes without any implementation. The Derived classes must overwrite them.

④ An abstract class can be derived from an abstract method that overwrites the base class or overwrites the base class. If not, its derived class must overwrite them.

  Ii. interface:Is the reference type. Similar to a class, the following three similar abstract classes.

① It cannot be instantiated.

② Contains the unimplemented method declaration.

③ The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members. (Not only methods, but also other members .)

In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members.

A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes)

  Iii. Differences between abstract classes and interfaces

① Class is the abstraction of objects. abstract classes are considered as objects, and abstract classes are called abstract classes. The interface is a behavior specification or provision.

② A class can implement several interfaces at a time, but only one parent class can be extended.

③ Interfaces can be used to support callback, but inheritance does not.

④ The abstract class cannot be sealed.

⑤ The specific methods implemented by the abstract class are virtual by default, but the interface methods in the class implementing the interface are non-virtual by default.

⑥ A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function in this interface, and has to implement other methods in the interface, it is called interface pollution.

7. Avoid using inheritance to implement component functions, but use black box multiplexing, that is, object combination. Because the inheritance layers increase, the most direct consequence is that when you call a class in this group, you must load all of them into the stack.

If an abstract class implements an interface, you can map the methods in the interface to an abstract class, instead of implementing the methods in the abstract class.

  Iv. Use of abstract classes and interfaces

    ① If multiple versions of a component are expected to be created, an abstract class is created. Abstract classes provide simple methods to control component versions.

② If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces.

③ If you want to design a large functional unit, use an abstract class. If you want to provide general implemented functions among all the implementations of the component, use an abstract class.

④ Abstract classes are mainly used for closely related objects, and interfaces are suitable for providing general functions for classes that do not want to be closed.

 

A good example on the Internet:

Airplanes fly, birds fly, they all implement the same interface "fly", but Boeing 747 belongs to the aircraft abstract class, pigeons belong to the bird abstract class

 

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.