In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize C # inheritance". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to achieve C# inheritance"!
one。 Inherited type
In object-oriented programming, there are two distinct types of inheritance: implementation inheritance and interface inheritance
1. Implement inheritance and interface inheritance
* implementation inheritance: indicates that a type derives from a base type and has all member fields and functions of that base type. In implementation inheritance, the derived type takes the implementation code of each function of the base type unless the implementation code of a function is specified in the definition of the derived type. You can use this type of inheritance when you need to add functionality to an existing type, or when many related types share a set of important common functions.
* API inheritance: indicates that a type inherits only the signature of the function, not any code. It is best to use this type of inheritance when you need to specify that the type has some available properties.
two。 Multiple inheritance
C# does not support multiple inheritance, but C# allows types to be derived from multiple interfaces-multiple interfaces. This means that a C# class can be derived from another class and any number of interfaces. More precisely, because System.Object is a common base class, each C # (except Object) has a base class and can have any number of interfaces.
3. Inheritance of structure
One limitation of using structures is that structures do not support implementation inheritance, but each structure is automatically derived from System.ValueType. You cannot encode a structure that implements a type level, but a structure can implement an interface.
two。 Implementation of inheritance
Syntax:
Class MyDreved:BaseClass {}
If the class or structure is also derived from an interface, separate the base class and interface in the list with a comma:
Class MyDreved:BaseClass,IIntenface1,IIntenface2 {}
If no base class is specified in the class definition, the C # compiler assumes that System.Object is the base class.
1. Virtual method
By declaring a base class function as virtual, you can override the function in any derived class:
Class BaseClass {public virtual void VirtualMethod () {/ /}}
You can also declare the property as virtual. For virtual or overridden attributes, the syntax is the same as for non-virtual attributes, but add the virtual keyword to the definition:
Public virtual string Name {get;set;}
The concept of virtual functions in C # is the same as that of standard OOP: virtual functions can be overridden in derived classes. When a method is called, the appropriate method of the derived class is called. In C #, functions are not virtual by default, but (except for constructors) can be explicitly declared as virtual.
When you override a function in a derived class, use the override keyword to display the declaration:
Class MyDreved: BaseClass {public override void VirtualMethod () {/ /}}
Neither member fields nor static functions can be declared virtual, because this concept only makes sense to instance function members in a class.
two。 Hiding method
If a method with the same signature is declared in both the base class and the derived class, but the method is not declared as virtual and override, the derived class method hides the base class method.
Class A {public void a () {Console.WriteLine ('CLASS is A');}} class Blaze A {public void a () {Console.WriteLine (' CLASS is B');}} class client {static void main () {B b=new B (); An astatb; a.a (); b.a () }} / * output CLASS IS ACLASS IS blocks /
In most cases, you override a method, not hide it, because hiding a method causes the wrong method to be called on an instance of a given class. However, C# grammars receive a warning of this potential error at compile time.
In C #, to hide a method, you declare it with the new keyword so that no warning is issued at compile time:
Class A {public void a () {Console.WriteLine ('CLASS is A');}} class Blaze A {public new void a () {Console.WriteLine ('CLASS is B');}} 3. The base class version of the calling function
C # can call the basic version of a method from a derived class: base. ()
Class MyDreved: BaseClass {public override void VirtualMethod () {base.VirtualMethod ();}}
You can use the base. () syntax to call any method in the base class without having to call it from the overload of the same method.
4. Abstract classes and abstract functions
C # allows classes and functions to be declared as abstract. Abstract classes cannot be instantiated and abstractions cannot be implemented directly and must be rewritten in non-abstract derived classes. Obviously, abstract functions are also virtual (although you don't need to provide virtual, and in fact, you can't provide this keyword).
If a class contains abstract functions, the class is also abstract and must be declared abstract:
Abstract class Building {public abstract void Cal ();}
Non-abstract methods cannot be declared in an abstract class, but other non-abstract members can be declared.
5. Sealing class and sealing method
C # allows classes and methods to be declared as sealed. For a class, this means that it cannot be inherited; for a method, it means that the method cannot be overridden.
Sealed class A {} class bazaar A / / error {}
If you do not want overridden methods and properties on the base class, do not declare it as virtual.
6. Constructor of derived class
Assuming that no displayed constructor is defined for any class, the compiler will provide a default initialization constructor for all classes, and the background compiler can solve the problems in the class hierarchy very well. each field in each class is initialized to the corresponding default value.
When you create an instance of a derived class, there are actually multiple constructors at work. The constructor of the class to be instantiated cannot initialize the class itself, and the constructor in the base class must be called.
The calling order of the constructor is to call Object first, and then call the constructor of the base class according to the hierarchy, from the base class to the parent class, until the class to be instantiated is reached. In the process, each constructor initializes the fields in its own class. Because the constructor of the base class is always called first, the derived class can access any member of the base class during execution, because the base class has been constructed and its fields have been initialized.
* adding a parameterless constructor to the hierarchy will replace the default constructor, so during execution, the parameterless constructor added to the base class will be called by default. Other things remain the same.
* add a constructor with parameters to the hierarchy to call the constructor with parameters in the hierarchy, you need to display the call in the constructor of the parent class:
Public abstract class GenericCustomer {private string name; public GenericCustomer () {name = ";} public GenericCustomer (string name) {this.name = name;} public string Name {get {return name;} set {name = value;}} public class Nevermore60Customer: GenericCustomer {private string referrerName; private uint highCostMinutesUsed Ublic Nevermore60Customer (string name): this (name, ") {} public Nevermore60Customer (string name, string referrerName): base (name) {this.referrerName = referrerName;} public string ReferrerName {get {return referrerName;} set {referrerName = value;}} three. Modifier
Modifiers can specify the visibility of a method, such as public or private, and the nature of an item, such as a method that is virtual or abstract.
1. The visibility modifier modifier is applied to state that all classes and members of the public any code can access members and embedded classes of the protected class only within the class and in derived classes internal all classes and members of the private class only within the class and in the assembly that contains it only members and embedded classes of the protected internal class are accessed only within the class, in the derived class and in the assembly that contains it
You cannot define a class as protected,private,protected internal because these modifiers have no meaning for types contained in the namespace. Therefore, these modifiers can only be applied to members. However, you can use these modifiers to define nested classes (embedded classes, classes contained in other classes), because in this case, the class also has the state of members:
Public class OuterClass {protected class InnerClass {}} 2. Other modifiers are applied to explain that new function hidden function static all members static virtual function members can be overridden by derived classes, function abstract override functions override virtual and abstract member sealed classes, methods, properties cannot be inherited and overridden extern only static method members are externally implemented in another language. Interface public interface IDisposable {void Dispose ();}
Declaring an interface is syntactically identical to declaring an abstract class, but it is not allowed to provide the implementation of any member. Abstract classes can provide implementations of members other than methods, such as properties.
In general, interfaces can only contain declarations for methods, properties, indexers, and events.
An interface cannot be instantiated, and it cannot have either a constructor or a field. Interface definitions are also not allowed to include operator overloads.
Modifiers about members are not allowed to be declared in the interface. Interface members are always public and cannot be declared virtual and static. If necessary, declare it in the implemented class.
The class that implements the interface must implement all members of the interface.
Interfaces can inherit from each other in the same way as classes.
At this point, I believe you have a deeper understanding of "how to achieve C# inheritance". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.