C # collection series (2): Attributes

Source: Internet
Author: User

 

1. Here we mainly demonstrate the inheritance and overwrite of attributes.

2. Understand attributes as methods. In fact, the compiler is to generate attributes.

Example:

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. LINQ;

UsingSystem. text;

 

NamespaceNettest

{

 

Public Class Testperpoerty

{

 

Class Derivedclass:Baseclass

{

Private StringName ="Name-derivedclass";

Private StringId ="ID-derivedclass";

 

New Public StringName

{

Get

{

ReturnName;

}

 

// Using "protected" wocould make the set accessor not accessible.

Set

{

Name =Value;

}

}

 

// Using private on the following property hides it in the main class.

// Any assignment to the property will use ID in baseclass.

New Private StringID

{

Get

{

ReturnID;

}

Set

{

Id =Value;

}

}

}

 

 

Class Baseclass

{

Private StringName ="Name-baseclass";

Private StringId ="ID-baseclass";

 

Public StringName

{

Get{ReturnName ;}

Set{}

}

 

Public StringID

{

Get{ReturnID ;}

Set{}

}

}

 

PublicVoidTest ()

{

BaseclassB1 =New Baseclass();

DerivedclassD1 =New Derivedclass();

 

B1.name ="Mary";

D1.name ="John";

 

B1.id ="Mary123";

D1.id ="John123";// The baseclass. ID property is called.

 

System.Console. Writeline ("Base: {0}, {1 }", B1.name, b1.id );

System.Console. Writeline ("Derived: {0}, {1 }", D1.name, d1.id );

 

/*

Output:

Name and ID in the base class: Name-baseclass, ID-baseclass

Name and ID in the derived class: John, ID-baseclass

*

--------------------------------

Note: If you replace the new private string ID with the new Public String ID, the following output is obtained:

Name and ID in the base class: Name-baseclass, ID-baseclass

 

Name and ID in the derived class: John, john123

*/

}

}

 

}

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.