继承和抽象类

接口:一种类型,一套语法规范,一个模板;
1.接口不可以实例化
2.接口的成员不可以有具体实现
3.接口继承接口不需要实现成员
4.子类实现接口必须重写成员;


定义:
interface 接口ming{
属性:
类型 属性名{get;set;}
方法:
返回值类型 方法名(参数列表);
}
子类实现接口必须重写接口中的成员;
class A:接口{
public 接口接口方法(){}
}
接口可以多实现;
class A:接口1,接口2,...{}


抽象类:
1.不可以实例化
2.可以拥有过抽象成员
3.子类继承抽象类必须重写抽象成员


定义:
abstract class 类名{
定义抽象成员:
abstract 属性类型 属性名{get;set;}
abstract 返回值类型 方法名(参数列表);
}
子类继承抽象类:
class A:抽象类{
//重写抽象成员
public override 抽象方法(){}
}
//子类继承父类实现接口
class A:父类,接口1,接口2,..{


}
抽象继承抽象类不需要重写抽象成员
抽象可以实现接口


类是单继承,接口是多实现;


门Door:
abstract class Door{
protected float height;
protected float width;
protected float weight;
protected  string material;
protected float price;
public Door(float height,float widht,float weight,float price,string material){
this.height=height;
this.weight=weight;
this.width=width;
this.price=price;
this.material=material;
}
public abstract void Open();
public abstract void Close();
public virtual string Info{
get{
return "高度:"+height+"宽度"+width;
}
}
interface IFire{
void FangFire();
}
class Buxiugang:Door,IFire{

public Buxiugang(float height,float width,float weight,float price):base(height,width,weight,price,"不锈钢"){

}
public override void Open(){

}
public override void Close(){

}
public void FangFire(){

}

}
class GlassDoor:Door{
public Buxiugang(float height,float width,float weight,float price):base(height,width,weight,price,"玻璃门"){

}
public override void Open(){

}
public override void Close(){

}
}
class WoodDoor:Door{
public Buxiugang(float height,float width,float weight,float price):base(height,width,weight,price,"木材"){

}
public override void Open(){

}
public override void Close(){

}
}
}

猜你喜欢

转载自blog.csdn.net/qq_36499416/article/details/80976417