c#程序设计学习-面向对象编程

问题描述:

把点类CPoint作为基类,派生出描述一条直线的类CLine,再派生出矩形类CR。要求成员函数能求出两点间距离、矩形的周长和面积。


代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint p1 = new CPoint(0, 0);
            CPoint p2 = new CPoint(3, 4);
            CRect r = new CRect(p1, p2);
            CLine l2=new CLine();
            Console.WriteLine("直线长度:{0}", CLine.Length(p1,p2));
            Console.WriteLine("矩形周长:{0}", r.GetPerimeter());
            Console.WriteLine("矩形面积:{0}", r.GetArea(p1,p2));
            Console.ReadKey();

        }
    }
    class CPoint
    {
        public double x;
        public double y;
        public CPoint() { }
        public CPoint(double a, double b)
        {
            x = a; y = b;
        }
    }
    class CLine : CPoint
    {
        public static double Length(CPoint p1, CPoint p2)
        {
            return Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
        }
    }
    class CRect : CPoint
    {
        CPoint p1=new CPoint();
        CPoint p2=new CPoint();
        public CRect(CPoint a, CPoint b)
        {
            p1.x=a.x;p1.y=a.y;
            p2.x=b.x;p2.y=b.y;
        }
        public double GetPerimeter()
        {
            return (Math.Abs(p1.x-p2.x)*2+Math.Abs(p1.y-p2.y)*2);
        }
        public double GetPerimeter(CPoint p1, CPoint p2)
        {
            return (Math.Abs(p1.x - p2.x) * 2 + Math.Abs(p1.y - p2.y) * 2);
        }
        public double GetArea()
        {
            return (Math.Abs(p1.x - p2.x) * Math.Abs(p1.y - p2.y));
        }
        public double GetArea(CPoint p1, CPoint p2)
        {
            return (Math.Abs(p1.x - p2.x) * Math.Abs(p1.y - p2.y));
        }
    }
}


运行结果:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值