类的覆盖

None.gif using  System;
None.gif
None.gif
None.gif
//  继承关键字
None.gif
None.gif
// virtual : 声明基类的某个成员在派生类中能被覆盖
        派生类继承了其基类的成员.如果派生类用同样的签名定义了一个成员,这个派生类成员就会覆盖基类成员.成员的签名包括它的名称,参数列表,参数类型和返回的类型.
       如果一个派生类用同样的名称定义了一个成员,但是定义了与基类不同的参数列表,参数类型或返回类型,派生成员将会重载或者遮蔽该基类成员.如果某个基类成员仍然可用,另一个成员将会重载这个成员.如果派生成员代替了基类成员,另一个成员就会遮蔽这个成员.
        能被覆盖的成员必须被声明为virtual(1).
None.gif

None.gif
None.gif
namespace  FlashCards
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CirCle 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CirCle
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
float fxCenter,fyCenter,fRadius;
InBlock.gif
InBlock.gif        
public CirCle()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
InBlock.gif
            fxCenter=0;
InBlock.gif            fyCenter
=0;
InBlock.gif            fRadius
=0;
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        
public 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        
public virtual float Area()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (float)(System.Math.PI *Math.Pow((double)fRadius,2)  );
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public float Perimeter()//计算周长
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            
return 2*fRadius*(float)System.Math.PI; 
ExpandedSubBlockEnd.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)  //virtual : 声明基类的某个成员在派生类中能被覆盖
ExpandedSubBlockStart.gifContractedSubBlock.gif
        dot.gif{
InBlock.gif            fxCenter
=x;
InBlock.gif            fyCenter
=y;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.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
private   void  Button3_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            CirCle myCircle
=new CirCle();
InBlock.gif            myCircle.Radius 
=2;
InBlock.gif            myCircle.Center(
10,2);
InBlock.gif            Response.Write(
"圆的面积: " +myCircle.Radius.ToString()  +"<br>");
InBlock.gif            Response.Write(
"圆的面积: " +myCircle.Area()+"<br>"); 
InBlock.gif            Response.Write(
"圆的周长: "+myCircle.Perimeter()+"<br>" ) ;
InBlock.gif            Sphere mySphere
=new Sphere();
InBlock.gif            mySphere.Radius 
=10;
InBlock.gif            mySphere.Center(
10,20,25) ;
InBlock.gif            Response.Write(
"mySphere Top   :"+mySphere.Top +"<br>" ) ;
InBlock.gif            Response.Write(
"         Left  :"+mySphere.Left +"<br>" ) ;
InBlock.gif            Response.Write(
"         Front :"+mySphere.Front +"<br>" ) ;
InBlock.gif            Response.Write(
"         volume:"+mySphere.Volume() +"<br>" ) ;
InBlock.gif            Response.Write(
"         surface area  : "+mySphere.Area()+ "<br>" ) ;
InBlock.gif            Response.Write(
"         circumference : "+mySphere.Perimeter() +"<br>" ) ;
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif        }
posted on 2006-01-16 15:57 DotNet编程 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/furenjun/archive/2006/01/16/318307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值