c#点线面(非继承)

1.把定义平面直角坐标系上的一个点的CPoint作为结构,描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造完整的程序。 提示:(1)可能用到的函数:math类中的函数 (2)注意数据类型的转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public interface zhaojiaxiang
    {
        double area();
    }
    public class CPoint
    {
        private double x;
        private double y;
        public CPoint() { }
        public CPoint(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
        public double getx()
        {
            return x;
        }
        public double gety()
        {
            return y;
        }

    }
    public class Cline
    {
        private CPoint n1;
        private CPoint n2;
        public Cline() { }
        public Cline(CPoint n1,CPoint n2)
        {
            this.n1=n1;
            this.n2=n2;
        }
        public double lineLength()
        {
            return System.Math.Sqrt((n1.getx() - n2.getx()) * (n1.getx() - n2.getx()) + (n1.gety() - n2.gety())*(n1.gety() - n2.gety()));
        }
    }
    public class Cert:zhaojiaxiang 
    {
        private Cline n1;
        private Cline n2;
        public Cert() { }
        public Cert(Cline n1,Cline n2)
        {
            this.n1 = n1;
            this.n2 = n2;
 
        }
        public double area()
        {
            return n1.lineLength() * n2.lineLength();
        }
        public double zhouchang()
        {
            return (n1.lineLength() + n2.lineLength()) * 2;
        }
        
    }

    class Program
    {
        static void Main(string[] args)
        {
            CPoint h = new CPoint(3, 5);
            CPoint h1 = new CPoint(6, 5);
            CPoint h2 = new CPoint(5, 5);
            Cline w1 = new Cline(h, h1);
            Cline w2 = new Cline(h, h2);
            Cert n = new Cert(w1,w2);
            Console.WriteLine("周长是:{0}",n.zhouchang());                                                                                                                                                                                                                                                                                                          
            Console.Write("面积是:{0}",n.area());
            Console.Read();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值