| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1141 人关注过本帖, 1 人收藏
标题:求助:基类、派生类,伤透脑筋?这个如何编写?
只看楼主 加入收藏
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
结帖率:96.15%
收藏(1)
 问题点数:0 回复次数:9 
求助:基类、派生类,伤透脑筋?这个如何编写?
一、题目
【5-12】把定义平面直角坐标系上的一个点的类clsPoint作为基类,派生出描述一个直线的类clsLine,再派生出一个矩形类clsRect类。要求方法能求出两点间的距离、矩形的周长和面积等。设计一个测试程序,并构造出完整的程序。

二、我的努力
namespace 书本练习题
{
    class clsPoint
    {
        /// <summary>
        /// 定义属性
        /// </summary>
        int x;
        public int X
        {
            get { return x; }
            set
            {
                if (value >= 60)
                {
                    x = value;
                }
                else
                {
                    x = 60;
                }
            }
        }
        int y;
        public int Y
        {
            get { return y; }
            set
            {
                if (value >= 75)
                {
                    y = value;
                }
                else
                {
                    y = 75;
                }
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xx"></param>
        /// <param name="yy"></param>
        public clsPoint(int xx, int yy)
        {
            this.x = xx;
            this.y = yy;
        }
        public clsPoint()
        {
            this.x = 60;
            this.y = 75;
        }
        /// <summary>
        /// 定义方法
        /// </summary>
        public void display()
        {
            Console.WriteLine("x,y的坐标值为({0},{1})", x, y);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            clsPoint Dian = new clsPoint();
            Dian.display();

            clsPoint DianTwo = new clsPoint(78, 79);
            DianTwo.display();

            Console.ReadKey();
        }
    }
}

三、我的困难
    1、我不知道如何派生出 clsLine类 和 clsRect类;  
    2、要有方法能计算两点之间的距离;要有方法能计算矩形的周长和面积;

此题困扰了我整整三天了,希望大侠高手们拔刀相助,小虾在此不胜感谢!
搜索更多相关主题的帖子: public 练习题 return 坐标系 如何 
2014-08-20 20:38
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
收藏
得分:0 
大家好,有哥们百度了这道题目的答案,在此,不再麻烦大家了。
代码如下:
源代码:
ClsPoint.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace __12
{
    //***************************基类clsPoint*****************************
    public class clsPoint
    {
        public double x;
        public double y;
        public clsPoint()
        {
            this.x = 0;
            this.y = 0;
        }
        public void setPoint(double _x,double _y)
        {
            this.x = _x;
            this.y = _y;
        }
        public void display()
        {
            Console.Write(" ({0},{1})", this.x, this.y);
        }
    }


    //*********************派生类clsLine*********************************
    public class clsLine : clsPoint
    {
        public clsPoint Point_start;
        public clsPoint Point_end;
        public clsLine(clsPoint Point_start, clsPoint Point_end)
        {
            this.Point_start=Point_start;
            this.Point_end = Point_end;
        }
        public double longness()
        {
            double l;
            l = System.Math.Sqrt((this.Point_start.y - this.Point_end.y) * (this.Point_start.y - this.Point_end.y) + (this.Point_start.x - this.Point_end.x) * (this.Point_start.x - this.Point_end.x));
            return l;
        }
    }


    //*********************派生类clsRect*********************************
    public class clsRect : clsPoint
    {
        public clsPoint point1;
        public clsPoint point2=new clsPoint();
        public clsPoint point3;
        public clsPoint point4=new clsPoint();
        double l1,l2;
        public clsRect(clsPoint point1,clsPoint point2)
        {
            this.point1 = point1;
            this.point2.setPoint(point1.x, point2.y);
            this.point3 = point2;
            this.point4.setPoint(point2.x, point1.y);
            this.l1=point2.x-point1.x;
            this.l2=point2.y-point1.y;
        }
        public double girth()        //周长
        {
            return ((this.l1 + this.l2) * 2);
        }
        public double area()        //面积
        {
            return (this.l1 * this.l2);
        }
    }
}
Program.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace __12
{
    class Program
    {
        static void Main(string[] args)
        {
            clsPoint point1 = new clsPoint();
            clsPoint point2 = new clsPoint();
            point1.setPoint(0,0);
            point2.setPoint(10,10);
            clsLine line1 = new clsLine(point1,point2);
            clsRect rect1 = new clsRect(point1,point2);
            Console.Write("由");
            line1.Point_start.display();
            line1.Point_end.display();
            Console.WriteLine("构成的线段的长度为:{0:f}", line1.longness());
            Console.Write("由");
            rect1.point1.display();
            rect1.point2.display();
            rect1.point3.display();
            rect1.point4.display();
            Console.WriteLine("构成的矩形周长为:{0:f},面积为:{1:f}", rect1.girth(),rect1.area());
        }
    }
}

艰难地走在C#自学的路上…………
2014-08-20 22:40
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
收藏
得分:0 
感谢信
---------------
在这里,感谢这位哥们。

艰难地走在C#自学的路上…………
2014-08-20 22:40
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 

Maybe
2014-08-21 07:49
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
这是我写的,
class Program
    {
        public abstract class Point
        {
            private int x;
            private int y;

            public int X
            {
                get
                {
                    return x;
                }
                set
                {
                    x = value;
                }
            }

            public int Y
            {
                get
                {
                    return y;
                }
                set
                {
                    y = value;
                }
            }

        }

        public class Line : Point
        {
            public double CalLength(Line pStartPoint,Line pEndPoint)
            {
                int dx=pStartPoint.X - pEndPoint.X;
                int dy=pStartPoint.Y - pEndPoint.Y;
                return Math.Round(Math.Sqrt(dx *dx+dy*dy),2);
            }
        }

        public class Rectangle : Point
        {
            public double CalLength(Rectangle pLeftBottomPoint, Rectangle pRightTopPoin)
            {
                double dx=pRightTopPoin.X-pLeftBottomPoint.X;
                double dy=pRightTopPoin.Y-pLeftBottomPoint.Y;
                return Math.Round(dy*2+dx*2,2);
            }

            public double CalArea(Rectangle pLeftBottomPoint, Rectangle pRightTopPoin)
            {
                double dx = pRightTopPoin.X - pLeftBottomPoint.X;
                double dy = pRightTopPoin.Y - pLeftBottomPoint.Y;
                return Math.Round(dx * dy, 2);
            }
        }
        static void Main(string[] args)
        {
            //创建线类
            Line pStarPoint = new Line();
            Line pEndPoint = new Line();
            pStarPoint.X = 0;
            pStarPoint.Y = 0;
            pEndPoint.X = 10;
            pEndPoint.Y = 10;
            
            //输出线的长度
            Console.WriteLine(pStarPoint.CalLength(pStarPoint, pEndPoint));


            Rectangle pLeftBottomPoint = new Rectangle();      ////左下角点
            Rectangle pRightTopPoin = new Rectangle();         ////右上角点
            pLeftBottomPoint.X = 0;
            pLeftBottomPoint.Y = 0;
            pRightTopPoin.X = 20;
            pRightTopPoin.Y = 20;

            Console.WriteLine(pLeftBottomPoint.CalLength(pLeftBottomPoint, pRightTopPoin));
            
            Console.WriteLine(pLeftBottomPoint.CalArea(pLeftBottomPoint,pRightTopPoin));
        }
    }

Maybe
2014-08-21 08:35
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
收藏
得分:0 
该题增加一个派生圆类clsPark,求以两点为半径的半径、周长和面积。

我照猫画虎,完成了这个派生类的编写:
namespace 书本练习题
{
    public class clsPoint
    {

        //***************************基类clsPoint*****************************
        public double x;
        public double y;
        public clsPoint()
        {
            this.x = 0;
            this.y = 0;
        }
        public void setPoint(double _x, double _y)
        {
            this.x = _x;
            this.y = _y;
        }
        public void display()
        {
            Console.Write("({0},{1})", this.x, this.y);
        }

        //*********************派生类clsLine*********************************
        public class clsLine : clsPoint
        {
            public clsPoint Point_start;
            public clsPoint Point_end;
            public clsLine(clsPoint Point_start, clsPoint Point_end)
            {
                this.Point_start = Point_start;
                this.Point_end = Point_end;
            }      //此处接收实例对象的传递
            public double longness()
            {
                double l;
                l = System.Math.Sqrt((this.Point_start.y - this.Point_end.y) * (this.Point_start.y - this.Point_end.y) + (this.Point_start.x - this.Point_end.x) * (this.Point_start.x - this.Point_end.x));
                return l;
            }
        }

        //*********************派生类clsRect*********************************
        public class clsRect : clsPoint
        {
            public clsPoint point1;
            public clsPoint point2=new clsPoint();
            public clsPoint point3;
            public clsPoint point4=new clsPoint();
            double l1,l2;
            public clsRect(clsPoint point1,clsPoint point2)
            {
                this.point1 = point1;
                this.point2.setPoint(point1.x, point2.y);
                this.point3 = point2;
                this.point4.setPoint(point2.x, point1.y);
                this.l1=Math.Abs ( point2.x-point1.x);
                this.l2=Math.Abs ( point2.y-point1.y);
            }
           public double girth()        //周长
           {
                return ((this.l1 + this.l2) * 2);
           }
           public double area()        //面积
           {
               return (this.l1 * this.l2);
           }
        }

       //*********************派生类clsPark*********************************
           public class clsPark : clsPoint
           {
               public clsPoint PointOne;
               public clsPoint PointTwo;

               public clsPark(clsPoint _PointOen, clsPoint _PointTwo)
               {
                   this.PointOne = _PointOen;
                   this.PointTwo = _PointTwo;
               }

               double dbradii;

               public double radiiTwo()  // 半径
               {
                   clsLine line1 = new clsLine(this .PointOne ,this .PointTwo );
                   double Radii=line1.longness ();
                   this.dbradii = Radii;
                   return Radii;
               }

               public double girthTwo()  //周长
               {
                   double Cr = Math.PI * this.dbradii * 2;
                   return Cr;
               }

               public double areaTwo()  //面积
               {
                   double Sr = Math.PI * this.dbradii * this.dbradii;
                   return Sr;
               }
               
               
           }

    }

    class Program
    {
        static void Main(string[] args)
        {
            //计算线段的长度
            clsPoint point1 = new clsPoint();
            clsPoint point2 = new clsPoint();
            point1.setPoint(0, 0);
            point2.setPoint(10, 10);
            clsPoint .clsLine line1 = new clsPoint .clsLine(point1, point2); //此处把实例对象作为参数进行传递
            Console.Write("由");
            line1.Point_start.display();
            line1.Point_end.display();
            Console.WriteLine("构成的线段的长度为:{0:f}", line1.longness()); //线段的长度

            //计算矩形周长和面积
            clsPoint.clsRect rect1 = new clsPoint.clsRect(point1, point2); //此处把实例对象作为参数进行传递
            Console.Write("由");
            rect1.point1.display();
            rect1.point2.display();
            rect1.point3.display();
            rect1.point4.display();
            Console.WriteLine("构成的矩形周长为:{0:f},面积为:{1:f}", rect1 .girth (), rect1.area());

            //计算圆的半径、周长和面积
            clsPoint.clsPark park1= new clsPoint .clsPark (point1,point2);
            Console.Write("由");
            park1.PointOne.display();
            park1.PointTwo.display();
            Console.WriteLine("构成的圆的半径为:{0:f},周长为:{1:f},面积为:{2:f}", park1.radiiTwo (),park1 .girthTwo (),park1.areaTwo ());

            Console.ReadKey();
        }
    }
}

艰难地走在C#自学的路上…………
2014-08-22 10:10
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
收藏
得分:0 
感谢信
--------
感谢各位大侠的帮忙!

艰难地走在C#自学的路上…………
2014-08-22 10:12
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:0 
public class clsPoint
    {

        //***************************基类clsPoint*****************************
        public double x;
        public double y;
        public clsPoint()
        {
            this.x = 0;
            this.y = 0;
        }
        public void setPoint(double _x, double _y)
        {
            this.x = _x;
            this.y = _y;
        }
        public void display()
        {
            Console.Write("({0},{1})", this.x, this.y);
        }

这里的改下更好,我认为的,既然是基类, public abstract class Point这样是不是更好点,里面的成员可以利用属性进行设置,不要过多的函数,属性设为private类型好点

Maybe
2014-08-22 11:27
gdc123
Rank: 1
等 级:新手上路
帖 子:26
专家分:7
注 册:2008-8-8
收藏
得分:0 
学无止境,学习了。
2014-10-26 00:31
天冰天降
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-7-4
收藏
得分:0 
学习了
2014-10-30 10:59
快速回复:求助:基类、派生类,伤透脑筋?这个如何编写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025113 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved