1. 程式人生 > >C#之類的設計:點和三角形類的設計

C#之類的設計:點和三角形類的設計

描述點的類Point

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

namespace ClassStudy2
{
    class Point
    {
        private int x;
        private int y;
        private int z;

        public int X 
        {
            get { return this.x; }
            set { this.x = value; }
        }
        public int Y 
        {
            get { return this.y; }
            set { this.y = value; }
        }

        public Point()
        {
        }

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        public Point(int z)
            : this(z, z)
        {
        }

        public double DistanceFromOrgin()
        {
            return Math.Sqrt(x * x + y * y);
        }

        public double DistanceFromPoint(Point p)
        {
            return Math.Sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
        }

        public static double DistanceFromPoint(Point p1, Point p2)
        {
            return p1.DistanceFromPoint(p2);
        }

        public override string ToString()
        {
            return "[" + x + "," + y + "]";
        }
    }
}

描述三角形的類Triangle

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

namespace ClassStudy2
{
    class Triangle
    {
        private Point p1;
        private Point p2;
        private Point p3;

        public Point P1 
        {
            get { return this.p1; }
            set { this.p1 = value; }
        }
        public Point P2
        {
            get { return this.p2; }
            set { this.p2 = value; }
        }
        public Point P3
        {
            get { return this.p3; }
            set { this.p3 = value; }
        }

        public Triangle()
        {
        }

        public Triangle(Point p1, Point p2, Point p3)
        {
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            if (len12 + len13 <= len23 || len12 + len23 <= len13 || len13 + len23 <= len12)
            {
                Console.WriteLine(p1 + "," + p2 + "," + p3 + "這三點無法構成三角形,構造三角形失敗!");
            }
            else
            {
                this.p1 = p1;
                this.p2 = p2;
                this.p3 = p3;
            }
        }

        public double GetGirth()
        {
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            return (len13 + len23 + len12); 
        }
        public double GetArea()
        {
            //海倫公式
            double len12 = p1.DistanceFromPoint(p2);
            double len13 = p1.DistanceFromPoint(p3);
            double len23 = p2.DistanceFromPoint(p3);

            double p = (len13 + len23 + len12)/2;
            return Math.Sqrt(p * (p - len12) * (p - len13) * (p - len23));
        }

        public override string ToString()
        {
            return ("此三角形的三個點為"+ p1 + ","+ p2 + "," + p3);
        }
    }
}

呼叫的類Program

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

namespace ClassStudy2
{
    class Program
    {
        static void Main(string[] args)
        {
            Point p1 = new Point();
            Console.WriteLine(p1);

            Point p2 = new Point(-3, 15);
            Console.WriteLine(p2);

            Point p3 = new Point();
            p3.X = 4;
            p3.Y = 7;
            //4.0後的新語法:建構函式呼叫時初始化,Point2D p3 = new Point2D{X=4, Y=7};注意要和函式的命名呼叫區分開來,如果這裡是命名呼叫應該是Point3D p3 = new Point2D(x:4,y:7);

            Console.WriteLine(p3.X + "," + p3.Y);

            Point p4 = new Point(6);
            Console.WriteLine(p4);

            double dis1 = Point.DistanceFromPoint(p2, p3);
            double dis2 = p2.DistanceFromPoint(p3);
            double dis3 = p2.DistanceFromOrgin();

            Console.WriteLine(p2 + "到" + p3 + "的距離為" + dis1);
            Console.WriteLine(p2 + "到" + p3 + "的距離為" + dis2);
            Console.WriteLine(p2 + "到原點的距離為" + dis3);

            //下面是三角形的驗證程式碼,作為作業
            Triangle t = new Triangle(p1, p2, p3);
            Console.WriteLine(t + ",面積為" + t.GetArea() + ",周長為" + t.GetGirth());
            Console.ReadLine();

        }
    }
}