Classes and inheritance

Source: Internet
Author: User

    1. All classes are derived from object;
    2. A derived class reference can be converted into a base class;
    3. Shielding base class members use the keyword new to block base class members;
    4. Virtual method and Overwrite method

The base class method is marked as virtual

There is a matching override method in a derived class

For Example:

Class mybaseclass{

Virtual public void printf () {

Console.WriteLine ("Fuck");}

}

Class myderived:mybaseclass{

Override public void printf () {

Console.WriteLine ("Fuck Again")

}

Class pargram{

Static void Main () {

myderived derived = new myderived ();

MyBaseClass MYBC = (mybaseclass) derived;

Derived.printf ();

Mybc.printf ();

}}

Result output:

Fuck Again

Fuck Again

Conclusion: After overwrite, no matter how many inheritance is the highest-level derived class, it is passed by the base class. If you use new to mask the base class, override cannot overwrite it. However, the base class must be converted by a derived class.

    1. Access level:

Public: All classes, including classes inside the assembly and external classes and external classes, are free to access members.

Private: can only be accessed by members of its own class. It cannot be accessed by other classes, including classes that inherit it.

Protected: Like accessing the private access level, it allows a class derived from that class to access the member, in addition to a point.

Internal: Internal members are visible to all class members within the same assembly, and the Assembly's external class members are not visible.

Protected internal: Accessible to all members that inherit from the class or to classes inside that assembly.

    1. Abstract member: Refers to a function member that is designed to override

Characteristics:

① must be a function member. In other words, fields and constants cannot be abstract members;

② must be marked with the abstract modifier;

③ cannot have code blocks. The code for the abstract member is represented by a semicolon.

④ must be overwritten, using override.

You can declare 4 types of methods, properties, events, and indexes.

    1. Abstract class: Refers to the inherited class, which can only be used as the base class for other inheriting classes. An abstract class can contain abstract members or ordinary non-abstract members. The members of an abstract class can be any combination of abstract members and members of an ordinary implementation. You cannot create an instance of an abstract class. and inherit the abstract must overwrite.

Cases:

Abstract class base{

public void Printfbase () {

Console.Write ("I am Your father!");}

Abstract void printfderived ();

}

Class derivedclass:base{

Override printfderived () {

Console.Write ("I am Your Father too!");}

Class prograg{

Static void Main () {

BaseClass base=new BaseClass ();

DerivedClass dc=new derivedclass ();

Base.printfbase ();

Dc.printfderived ();}

}

Output:

I am Your father!

I am your father too

8. Sealed class: Sealed can only be used as a standalone class, not as a base class.

9. Expand the method, declaring that the expanded method must declare static, and that the extension method itself must declare static. The extension method must contain the keyword this as the first parameter type.

Fox Example:

Namaspace ex{

Sealed class mydata{

Private double d1,d2,d3;

Public MyData (double d1,double d2,double D3) {

D1=D1;

D2=D2;

D3=D3;}

public double sum () {

Return D1+d2+d3;}

Static class extenmydata{

public static double Average (this MyData data) {

return DATA.SUM/3;}

}

Class pragram{

Static void Main () {

MyData data=new MyData (3,4,5);

Console.WriteLine ("Sum is: {0}", mydata.sum);

Console.WriteLine ("Average is:{0}", Mydata.average ();}

}

    1. Naming conventions

Pascal Convention identifier Each word capitalized applies to: classes, methods, namespaces, properties, and public fields

Camel except the first word other words first capitalization applies to: Name of local variable and method life's parameter name

Underline camel size identifiers that begin with a camel underline apply to: private and protected fields

Classes and inheritance

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.