接口

None.gif//接口
None.gif    
//    接口与抽象类有些相似,因为它们都提供了用来创建新类的模板.它们之间的不同在于接口不提供任何实现的
None.gif    
//类成员,而抽象类可以实现派生类共用的成员.
None.gif    
//    比起类而言,接口更像类型.当您在一个类中实现某个特定的接口时,这个类的实例可以用于被声明为该接口的
None.gif    
//参数或变量.
None.gif    
//例如下面的代码为前面创建的Shape对象声明了一个接口.
None.gif

None.gif
using System;
None.gif
None.gif
None.gif
None.gif
namespace FlashCards
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif    
public interface IFigure
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
float Top
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get;
InBlock.gif            
set;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
float Left
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get;
InBlock.gif            
set;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
float Area();
InBlock.gif        
float Perimeter();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 使用这个接口前,需要在下面的类中实现它
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public abstract class Shape:IFigure
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Shape()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
IFigure 成员#region IFigure 成员
InBlock.gif
InBlock.gif        
public abstract float Top
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get;
InBlock.gif            
InBlock.gif            
set;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract float Left
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get;
InBlock.gif            
InBlock.gif            
set;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public abstract float Area();
InBlock.gif        
InBlock.gif
InBlock.gif        
public abstract float Perimeter();
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
//覆盖抽象类成员的每一个成员定义都需要 Overrides(vb.net) 或 override(c#)  关键字.
InBlock.gif
    public class CirCle:Shape
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
float fxCenter,fyCenter,fRadius;
InBlock.gif        
public CirCle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fxCenter
=0;
InBlock.gif            fyCenter
=0;
InBlock.gif            fRadius
=0;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override float Top
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return fxCenter-fRadius;} //fx
ExpandedSubBlockStart.gifContractedSubBlock.gif
            setdot.gif{fxCenter=value+fRadius;}
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override float Left
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return fyCenter-fRadius;} //fy
ExpandedSubBlockStart.gifContractedSubBlock.gif
            setdot.gif{fyCenter=value+fRadius;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public override float Area()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (float)(2*System.Math.PI *Math.Pow((double)fRadius,2  ));
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override float Perimeter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return 2*fRadius*(float)System.Math.PI; 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public float Radius
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return fRadius;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{fRadius=value;}
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
public virtual void Center(float x,float y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fxCenter
=x;
InBlock.gif            fyCenter
=y;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class Sphere:CirCle //球体,圆
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif      
float fCenter;
InBlock.gif        
public Sphere()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fCenter
=0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public override float Area()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (float)(4*Math.PI *Math.Pow((double)base.Radius ,2) );  //圆的面积
InBlock.gif

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public new void Center(float x,float y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Center (x,y,0);
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Center(float x,float y,float z)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.Center(x,y) ;
InBlock.gif            fCenter
=z;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public float Volume()  //体积
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
return (float)((4/3)*System.Math.PI*Math.Pow((double)base.Radius,3));
ExpandedSubBlockEnd.gif        }

InBlock.gif                 
InBlock.gif        
public float Front
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return fCenter-base.Radius ;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{fCenter=value+base.Radius ;}
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
//    这个示例中的Shape抽象类实现了IFigure接口,所有从Shape派生而来的类都继承了实现的IFigure.
None.gif        
//这就是说,类型CirCle和Sphere的对象是从Shape派生而来,它们能被用作IFigure类型的参数.
None.gif        
//    这里的关键是接口中定义的所有项必定存在于实现该接口的类中 .如果您省略了某个成员,Visual Studio
None.gif        
//将产生一个编译时错误.
None.gif
        private void Button4_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//创建一个圆
InBlock.gif
            CirCle myCircle=new CirCle();
InBlock.gif            myCircle.Radius 
=2;
InBlock.gif            myCircle.Center(
10,2);
InBlock.gif            
//创建一个球体
InBlock.gif
            Sphere mySphere=new Sphere();
InBlock.gif            mySphere.Radius 
=10;
InBlock.gif            mySphere.Center(
10,20,25) ;
InBlock.gif            
//显示每一种形状的相关信息
InBlock.gif
            ShowShapeInfo(mySphere);
InBlock.gif            ShowShapeInfo(myCircle);
InBlock.gif
InBlock.gif        
ExpandedBlockEnd.gif        }

None.gif
None.gif        
private void ShowShapeInfo(IFigure Shape)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
//因为Shape参数是IFigure,我们知道它有这些成员
InBlock.gif
            Response.Write("Shape    Top   :"+Shape.Top +"<br>" ) ;
InBlock.gif            Response.Write(
"         Left  :"+Shape.Left +"<br>" ) ;
InBlock.gif            Response.Write(
"         Area  : "+Shape.Area()+ "<br>" ) ;
InBlock.gif            Response.Write(
"         perimeter      : "+Shape.Perimeter() +"<br>" ) ;
InBlock.gif
ExpandedBlockEnd.gif        }

None.gif    }
//输出
Shape Top :0
Left :10
Area : 1256.637
perimeter : 62.83186
Shape Top :8
Left :0
Area : 25.13274
perimeter : 12.56637
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值