抽象类
/*
* 如果希望一个类作为基类来派生其他类,则考虑吧这个类定义成抽象类
* 语法:
* [访问修饰符] abstract class 类名{}
* 1.抽象类包含零个和多个抽象方法,也可以包含零个和多个非抽象方法
* 2.定义抽象方法目的在于指定派生类必须实现这一方法,定义抽象方法也是使用abstract关键字
* 3.抽象方法只指明方法的返回值类型,方法名称和参数,而不提供方法的实现
* 4.一个类只要有一个抽象方法,该类就必须定义为抽象类
* 5.抽象类不能被实例化
* 6.使用override关键字可在派生类中实现抽象方法,方法的签名必须与基类方法相同
*
*
*/
class Program
{
//抽象类
abstract class Person
{
public int age;
public string name;
//抽象方法,没有代码实现
public abstract void Speak();
}
class Student : Person
{
public int stuid;
//子类必须重写父类抽象类的抽象方法
public override void Speak()
{
Console.WriteLine("你好,我是{0},还在读书,很高兴认识你",this.name);
}
}
class Teacher : Person
{
public int teacherid;
public override void Speak()
{
Console.WriteLine("你好,我是{0},我是老师,很高兴认识你", this.name);
}
}
static void Main(string[] args)
{
//抽象类不能被实例化
//Person p = new Person();
Student s = new Student();
s.name = "张三";
s.Speak();
Teacher t = new Teacher();
t.name = "李四";
t.Speak();
Console.ReadKey();
}
}
继承练习
/*
* 抽象类可以有构造方法,但该构造方法不能直接被调用而只能由子类的构造方法调用
* 抽象类不能实例化
*
*/
//抽象类Animal
abstract class Animal
{
protected float weight;//体重
//构造方法
protected Animal(float w)
{
this.weight = w;
Console.WriteLine("父类");
}
//描述动物奔跑的抽象方法
public abstract void Run();
public void Eat()
{
Console.WriteLine("动物都要进食,否则无法生存");
}
}
//派生的袋鼠类
class Kanggaroo : Animal
{
//构造方法
public Kanggaroo(float w) : base(w)
{
Console.WriteLine("子类");
}
//实现积基类的抽象方法
public override void Run()
{
Console.WriteLine("袋鼠用跑跳的方式奔跑!");
}
public void ShowInfo()
{
Console.WriteLine("袋鼠的体重是{0}千克",this.weight);
}
}
class Program
{
static void Main(string[] args)
{
Kanggaroo k = new Kanggaroo(80.5F);
k.Eat();
k.Run();
k.ShowInfo();
Console.ReadKey();
}
}
接口
/*
* 接口是一个只说明应该做什么,但不能指定如何做的“更加纯粹的抽象类”
* 1.抽象类中可以有已经实现的方法,但接口中不能包含任何实现了的方法
* 2.接口中的所有方法都必须在子类中实现
* 3.接口的作用在于指明实现此特定接口的类必须实现该类口列出的所有成员,它指明了一个类必须具有哪些功能
* 语法:
* [修饰符] interface 接口名{接口主体;}
*
* C#中接口名以大写字母I开头
* 1.类实现接口和继承一样,需要使用冒号运算符
* 2.与抽象类不同的是,类是实现了接口中的方法而不是重写,所以实现接口的方法时不需要override关键字
*
*/
//画图接口
interface IDraw
{
void Draw();
}
class Circle : IDraw
{
//构造方法
public Circle(float r)
{
this.radius = r;
}
private float radius;//圆的半径
public float Radius
{
get
{
return this.radius;
}
set
{
this.radius = value;
}
}
//实现接口的方法
public void Draw()
{
Console.WriteLine("画一个半径为{0}厘米的圆!", this.radius);
}
}
class Program
{
static void Main(string[] args)
{
Circle c = new Circle(10);
c.Draw();
Console.ReadKey();
}
}
继承基类并实现接口
/*
* 一个类既可以派生自另一个类,还可以同时实现某个接口
* 一个类继承基类同时又实现接口是,基类名要写在接口名前面
*
* 2.多重接口实现:
* C#不允许多重继承,也就是说一个类不能同时派生自多个子类,但C#允许多重接口实现,也就是说一个类可以同时实现多个接口
*
* 3知道一个实例是不是另一个类型,可以使用is关键字,as关键字
* is后面可以接类、接口、基本数据类型,结构和枚举等
*
* 4.接口绑定
* 接口绑定就是把不同的接口合在一起变成一个新的接口
*
* 5.接口作为方法的参数,实际上要接受的是一个实现这个接口的对象
*/
//画图接口
interface IDraw
{
void Draw();
}
//2 多接口实现
//打印接口
interface IPrint
{
void Print();
}
interface IDraw3D
{
void Draw3D();
}
//接口绑定
interface IDrawAndPrint : IDraw, IPrint
{
void Show();
}
class Point
{
//构造方法
public Point(int x,int y)
{
this.pointx = x;
this.pointy = y;
}
protected int pointx, pointy;//点的横纵坐标
public int Pointx //存取横坐标的属性
{
get
{
return this.pointx;
}
set
{
this.pointx = value;
}
}
public int Pointy //存取纵坐标的属性
{
get
{
return this.pointy;
}
set
{
this.pointy = value;
}
}
}
class Circle : Point, IDraw,IPrint
{
public Circle(int x,int y,float r) : base(x, y)
{
this.radius = r;
}
private float radius; //半径
public float Radius
{
get
{
return this.radius;
}
set
{
this.radius = value;
}
}
//实现IDraw接口
public void Draw()
{
Console.WriteLine("画一个坐标原点为{0}和{1},半径为{2}厘米的圆!",this.pointx,this.pointy,this.radius);
}
//实现IPrint接口方法
public void Print()
{
Console.WriteLine("把圆打印在纸上!");
}
}
//4.接口绑定测试类
class Test:IDrawAndPrint
{
public void Show()
{
Console.WriteLine("这是绑定接口的方法");
}
public void Draw()
{
Console.WriteLine("这个IDraw接口的方法");
}
public void Print()
{
Console.WriteLine("这个是IPrint接口方法");
}
}
class Program
{
static void Main(string[] args)
{
Circle c = new Circle(2, 3, 10);
// c.Draw();
c.Print();
//3.c is IDraw返回true,说明c是IDraw类型的实例,is关键字后面可以接类、接口、基本数据类型、结构、枚举
//if(c is IDraw)
//{
// IDraw d = (IDraw)c;
// d.Draw();
//}
//if (c is IDraw3D)
//{
// IDraw3D d3 = (IDraw3D)c;
// d3.Draw3D();
//}
//3.as关键字在测试的同时把实例转换为另一个类型,如果转换不成功,则返回null
IDraw d = c as IDraw;
if (d != null)
{
d.Draw();
}
IDraw3D d3 = c as IDraw3D;
if (d3 != null)
{
d3.Draw3D();
}
//4.接口绑定测试
Test t = new Test();
t.Show();
t.Draw();
t.Print();
Console.ReadKey();
}
}
网友评论