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
*/
}
}
}