抽象类: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;
}
}
}
}