C#上机 第七周 任务1 继承
2012-10-14 19:58
399 查看
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:继承 * 作 者:薛广晨 * 完成日期:2011 年 10 月 14 日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: 把定义平面直角坐标系上的一个点的类CPoint作为基类, 派生出描述一条直线的类Cline,再派生出一个矩形类CRect。 要求成员函数能求出两点间的距离、矩形的周长和面积等。 设计一个测试程序,并构造完整的程序。 * 程序输出: * 程序头部的注释结束 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace xue { class Program { static void Main(string[] args) { CPoint cp1 = new CPoint(3,3); CPoint cp2 = new CPoint(); CPoint cp3 = new CPoint(); cp1.display_CPoint(); cp2.display_CPoint(); cp3.display_CPoint(); cp3.set_x(3); cp3.display_CPoint(); Cline cl1 = new Cline(); Console.WriteLine("两点之间的距离为:{0}", cl1.distance(cp1, cp3)); Cline cl2 = new Cline(); cl2.distance(cp2, cp3); CRect cr = new CRect(); Console.WriteLine("矩形的周长为:{0}", cr.perimeter(cl1, cl2)); Console.WriteLine("矩形的面积为:{0}", cr.area(cl1, cl2)); Console.ReadKey(true); } } class CPoint { private double x; private double y; public CPoint() { x = 0; y = 0; } public CPoint(double x, double y) { this.x = x; this.y = y; } public void set_x(double x) { this.x = x; } public void set_y(double y) { this.y = y; } public double get_x() { return this.x; } public double get_y() { return this.y; } public void display_CPoint() { Console.WriteLine("({0}, {1})", this.x, this.y); } } class Cline : CPoint { private double length; public double distance(CPoint c1, CPoint c2) { this.length = Math.Sqrt((c1.get_x() - c2.get_x()) * (c1.get_x() - c2.get_x()) + (c1.get_y() - c2.get_y()) * (c1.get_y() - c2.get_y())); return this.length; } public double getLength() { return this.length; } public void set_length(CPoint c1, CPoint c2) { this.length = distance(c1, c2); } } class CRect : Cline { public double perimeter(Cline c1, Cline c2) { double pm = c1.getLength() * 2 + c2.getLength() * 2; return pm; } public double area(Cline c1, Cline c2) { return c1.getLength() * c2.getLength(); } } }
运行结果:
相关文章推荐
- java上机 第七周 任务1 封装和继承
- C#上机 第七周 任务2 判断该物体是否会在水中下沉
- 第七周上机任务1-求两数正数差
- 第七周上机任务项目4-复数模板类
- 第七周作业:Lesson 3 上机练习题——继承
- 第十三周上机任务项目1-理解基类中成员的访问限定符和派生类的继承方式
- C#上机 第九周 任务2 实现字符串反转
- 第七周上机任务5-模板类中使用友元函数
- 第七周上机任务项目二
- 2013第七周上机任务【项目1-静态成员应用时间类】
- 2013第七周上机任务【项目4-复数模板类】
- C#上机 第十周 任务1 我的窗体实验
- 第七周上机任务项目四
- C#上机 第四周 任务1.2 输出其中的大写字母、小写字母、数字和其他字符的个数
- 第七周上机任务之最大值与最小值
- java上机 第八周 任务1 继承的练习
- 2013第七周上机任务【阅读程序并改错】
- C02-程序设计基础提高班(C++)第12周上机任务-类的继承
- 第七周上机任务二--理解成员函数、友元函数和一般函数的区别 并会熟练使用它们
- 第七周上机任务(求两个数的正差值)