码迷,mamicode.com
首页 > 其他好文 > 详细

抽象类与抽象方法

时间:2015-05-25 00:54:04      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

抽象类:abstract关键词
抽象类不能造自己的实例对象,只能作为父类使用
抽象类中可以有抽象属性和抽象方法,必须要在子类中实现
有抽象方法和抽象属性的一定是抽象类
但抽象类不一定非得有抽象方法和抽象属性
抽象类仍然可以作为基类进行与子类之间的类型转换

using System;
using System.Collections.Generic;
using System.Text;

namespace 抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            Fruit f;//抽象类不能直接创建对象,一般用它来引用子类对象
            f = new Apple();
            f.chandi();
            f = new Pineapple();
            f.chandi();
            Console.ReadLine();
            //同一行代码“f.chandi()”,由于f所引用的对象不同而输出不同的结果。
             可以按以下公式编写代码
             抽象类 抽象类变量名=new 继承此抽象类的具体子类名();*/

            parent p;
            p = new child();
            p.Message = "Hello";//调用child中属性
            Console.WriteLine(p.Message);//打印属性
            Console.ReadLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace 抽象类
{
    abstract class parent
    {
        public abstract string Message //抽象属性
        {
            get;
            set;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace 抽象类
{
    class child:parent
    {
        private string _msg;
        public override string Message //抽象类中的抽象属性,要在子类中实现
        {
            get
            {
                return _msg;
            }
            set
            {
                _msg=value;
            }
        }
    }
}

 

抽象类与抽象方法

标签:

原文地址:http://www.cnblogs.com/happinesshappy/p/4526813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!