C#入门基础——抽象和非抽象成员
本示例主要是介绍了抽象和非抽象成员,其中用到了抽象属性,有兴趣的朋友可以试一试。namespace 抽象类的另一个例子
{
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.PrintStuff("This is a string.");
mc.MyInt = 28;
Console.WriteLine(mc.MyInt);
Console.WriteLine("Perimeter Length: {0}", mc.PerimeterLength());
Console.ReadKey();
}
}
abstract class MyBase //抽象和非抽象成员的组合
{
public int SideLength = 10; //数据成员
const int TriangleSideCount = 3;//数据成员
abstract public void PrintStuff(string s);//抽象方法
abstract public int MyInt{ get; set; } //抽象属性
public int PerimeterLength() //普通的非抽象方法
{
return TriangleSideCount * SideLength;
}
}
class MyClass : MyBase
{
public override void PrintStuff(string s) //覆盖抽象方法
{
Console.WriteLine(s);
}
private int myInt;
public override int MyInt //覆盖抽象属性
{
get
{
returnmyInt;
}
set
{
myInt = value;
}
}
}
}
输出结果:
Thisisa string.
28
Perimeter Length:30
{:5_105:}
页:
[1]