C#怎么定义一个点类Point,从点类派生一个圆类

2025-04-07 16:55:14
推荐回答(1个)
回答1:

// Point类
public class Point
{
    private int x;
    private int y;
    
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public int X 
    { 
        get{ return x;}
        set{ x = value;}
    }
    
    public int Y
    {
        get{ return y;}
        set{ y = value;}
    }        
}

// Circle类, 继承Point类作为圆心
public class Circle : Point
{
    float r; //半径
    public Circle(int x, int y, float r):base(x, y)
    {
        this.r = r;
    }
    
    // 获取或设置圆心
    public Point Center
    {
        get { return this;}
        set 
        {
          this.X = value.X;
          this.Y = value.Y;
        }  
    }
    
    // 获取或设置半径
    public float Radius
    {
        get{ return r;}
        set { r = value;}
    }
    
    // 其他属性、方法……
}