| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
本示例主要是介绍了抽象和非抽象成员,其中用到了抽象属性,有兴趣的朋友可以试一试。 
 
 
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 
            { 
                return  myInt; 
            } 
 
            set 
            { 
                myInt = value; 
            } 
        } 
    } 
} 
 
输出结果: 
This  is  a string. 
28 
Perimeter Length:30 
 |   
 
 
 
 |