code is as follows, mainly looking at the last
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpClassTest
{
/*
* :https://blog.csdn.net/tc_1337/article/details/80957140-sharpcommentsedit
*/
class A
{
public static string StaticStr = "A ";
public string NonStaticStr = "A ";
public string NoStaticProperty { get; set; } = "A ";
public static void StaticMethod()
{
Console.WriteLine("A ");
}
public void NonStaticMethod()
{
Console.WriteLine($"A :A | A :{NonStaticStr} | {NoStaticProperty}");
}
}
class B : A
{
public static string StaticStr = "B ";
public string NonStaticStr = "B ";
public static void StaticMethod()
{
Console.WriteLine("B ");
}
public void NonStaticMethod()
{
Console.WriteLine($"B :{NonStaticStr}");
}
}
class C : A
{
}
class D : A
{
public string NonStaticStr = "D ";
}
class E : A
{
public string NoStaticProperty { get; set; } = "E ";
}
class Program
{
static void Main(string[] args)
{
C c = new C();
Console.WriteLine(" AC :");
Console.WriteLine(c.NonStaticStr); // A
c.NonStaticMethod(); // A :A | A
Console.WriteLine("-------------------------------\r\n");
A c1 = new C();
Console.WriteLine(" CA :");
Console.WriteLine(c1.NonStaticStr); // A
c1.NonStaticMethod(); // A :A | A
Console.WriteLine("-------------------------------\r\n");
B b = new B();
Console.WriteLine(" AB :");
Console.WriteLine(b.NonStaticStr); // B
b.NonStaticMethod(); // B :B
Console.WriteLine("-------------------------------\r\n");
A b1 = new B();
Console.WriteLine(" BA :");
Console.WriteLine(b1.NonStaticStr); // A
b1.NonStaticMethod(); // A :A | A
Console.WriteLine("-------------------------------\r\n");
D d = new D();
Console.WriteLine(" AD :");
Console.WriteLine(d.NonStaticStr); // D
d.NonStaticMethod(); // A :A | A
Console.WriteLine("-------------------------------\r\n");
E e = new E();
Console.WriteLine(" AE :");
Console.WriteLine(e.NoStaticProperty); // E
e.NonStaticMethod(); // A :A | A
Console.WriteLine("-------------------------------\r\n");
}
}
}
inspired by @ Woody :
class AA
{
public virtual string NoStaticProperty { get; set; } = "AA ";
public void NonStaticMethod()
{
Console.WriteLine($"AA : :{NoStaticProperty}");
}
}
class BB : AA
{
public override string NoStaticProperty { get; set; } = "BB ";
}
class CC : AA
{
public new string NoStaticProperty { get; set; } = "CC ";
}
//...
BB bb = new BB();
Console.WriteLine(" AABB :");
Console.WriteLine(bb.NoStaticProperty); // BB
bb.NonStaticMethod(); // AA : :BB
Console.WriteLine("-------------------------------\r\n");
CC cc = new CC();
Console.WriteLine(" newAACC :");
Console.WriteLine(cc.NoStaticProperty); // CC
cc.NonStaticMethod(); // AA : :AA
Console.WriteLine("-------------------------------\r\n");
AA ab = new BB();
Console.WriteLine(" BBAA :");
Console.WriteLine(ab.NoStaticProperty); // BB
ab.NonStaticMethod(); // AA : :BB
Console.WriteLine("-------------------------------\r\n");