第5章 面向对象程序设计基础部分答案
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using System; using System.Collections.Generic; using System.Text;
namespace Inheritance {
public class clsPerson {
string strName; string strPNo; string strPSex; public void SetPersonInfo(string _strName, string _strPNo, string _strPSex) {
第 5 章 面向对象程序设计基础
5-9
//定义一个描述学生基本信息的类, //属性包括姓名、学号,C#、英语和数学成绩, //方法包括设置姓名和学号、设置三门课的成绩和输出相关学生的信息, //最后求出总成绩和平均成绩。
using System; using System.Collections.Generic; using System.Text;
}
5-12
//把定义平面直角坐标系上的一个点的类 clsPoint 作为基类, //派生出描述一条直线的类 clsLine, //再派生出一个矩形类 clsRect。 //要求方法能求出两点间的距离、矩形的周长和面积等。 //设计一个测试程序,并构造出完整的程序。
//根号下(|X1-X2|的平方+|Y1-Y2|的平方)这个就是两点间距离公式
dblX = _dblX; dblY = _dblY; } public void display() { Console.WriteLine("坐标点的 x 坐标为:" + dblX + ";y 坐标为:" + dblY); } public void setPoint(double _dblX, double _dblY) { dblX = _dblX; dblY = _dblY; } } class Test { static void Main() { clsPoint point = new clsPoint(60.00, 75.00); point.display(); point.setPoint(100.00, 120.00); point.display(); Console.Read(); } }
{ strName = _strName; strStuNo = _strStuNo; strCSharpScore = _strCShapeScore; strEnglishScore = _strEnglishScore; strMathsScore = _strMathsScore;
}
public void DisplayStudentInfo() {
clsRect rect = new clsRect(line1, line2, line3, line4);
double dblDistOfLine; dblDistOfLine = line1.getDistance(); Console.WriteLine("两点之间的距离为:" + dblDistOfLine);
public double getDistance() {
double dblDistance, dblDistance1, dblDistance2; dblDistance1 = Math.Pow( point1.dblX - point2.dblX,2.0); dblDistance2 = Math.Pow( point1.dblY - point2.dblY,2.0); dblDistance = Math.Sqrt(dblDistance1 + dblDistance2); return dblDistance; } } public class clsRect : clsLine { clsLine line1, line2, line3, line4; public clsRect(clsLine _line1,clsLine _line2,clsLine _line3,clsLine _line4) { line1 = _line1; line2 = _line2; line3 = _line3; line4 = _line4; }
namespace clsStudent {
class clsStudent {
string strName; string strStuNo; int strCSharpScore; int strEnglishScore; int strMathsScore;
public clsStudent(string _strName,string _strStuNo, int _strCShapeScore,int _strEnglishScore,int _strMathsScore)
int nScore; public void SetStudentScore(int _nScore) {
nScore = _nScore; }
public void DisplayScore() {
Console.WriteLine("成绩为:" + nScore); } } public class clsTeacher : clsPerson { int nSeniority; public void SetSeniority(int _nSeniority) {
double dblPerimeterOfRect, dblAreaOfRect; dblPerimeterOfRect = rect.getPerimeter(); dblAreaOfRect = rect.getArea(); Console.WriteLine("矩形的周长为:" + dblPerimeterOfRect); Console.WriteLine("矩形的面积为:" + dblAreaOfRect); } } }
clsTeacher teacher = new clsTeacher(); teacher.SetPersonInfo("李老师", "20000304", "男"); teacher.SetSeniority(15); teacher.Display(); teacher.DisplaySeniority(); } } }
5-11
//设有一个描述坐标点的 clsPoint 类, //其私有变量 x 和 y 代表一个点的 x、y 坐标值。 //编写程序实现以下功能: //利用构造函数传递参数,并设其默认参数值为 60 和 75, //利用方法 display()输出这一默认的值; //利用公有方法 setPoint()将坐标值修改为(100,120), //并利用方法输出修改后的坐标值。
public double getPerimeter() {
double dblPerimeter; dblPerimeter = 2 * (line1.getDistance() + line2.getDistance()); return dblPerimeter; }
public double getArea() {
strName = _strName; strPNo = _strPNo; strPSex = _strPSex; } public void Display() { Console.WriteLine("姓名为:" +
strName + ";编号为:" + strPNo + ";性别为:" + strPSex); } } public class clStudent : clsPerson {
} } class Test {
static void Main() {
clsStudent stu = new clsStudent("李三", "0900101", 90, 89, 99); stu.DisplayStudentInfo(); } } }
5-10
//定义一个人员类 clsPerson, //包括属性:姓名、编号、性别和用于输入输出的方法。 //在此基础上派生出学生类 clsStudent(增加成绩)和 //教师类 clsTeacher(增加教龄), //并实现对学生和教师信息的输入输出。
clsLine line1 = new clsLine(point2, point1);
clsLine line2 = new clsLine(point3, point2); clsLine line3 = new clsLine(point4, point3); clsLine line4 = new clsLine(point1, point4);
double dblArea; dblArea = line1.getDistance() * line2.getDistance(); return dblArea; } } class Test { static void Main() { clsPoint point1=new clsPoint(0.0,10.0); clsPoint point2=new clsPoint(20.0,10.0); clsPoint point3 = new clsPoint(20.0, 0); clsPoint point4 = new clsPoint(0.0, 0.0);
Console.WriteLine("该生的信息为:" ); Console.WriteLine("-------------------------------------"); Console.WriteLine("姓名为:" + strName); Console.WriteLine("学号为:" + strStuNo); Console.WriteLine("C#的成绩为:" + strCSharpScore); Console.WriteLine("英语的成绩为:" + strEnglishScore); Console.WriteLine("数学的成绩为:" + strMathsScore);
dblX = _dblX; dblY = _dblY; } } public class clsLine : clsPoint { clsPoint point1, point2; public clsLine() { } public clsLine(clsPoint _clsPoint1, clsPoint _clsPoint2) { point1 = _clsPoint1; point2 = _clsPoint2; }
//矩形的周长:(长+宽 )乘以 2 //矩形的面积:
using System; using System.Collections.Generic; using System.Text;
nameБайду номын сангаасpace Inheritance {
public class clsPoint {
public double dblX; public double dblY; public clsPoint() { } public clsPoint(double _dblX, double _dblY) {
using System; using System.Collections.Generic; using System.Text;
namespace Inheritance {
class clsPoint {
private double dblX; private double dblY; public clsPoint(double _dblX, double _dblY) {
nSeniority = _nSeniority; }
public void DisplaySeniority() {
Console.WriteLine("教龄为:" + nSeniority); } } public class clsTest { static void Main() {
clStudent stu = new clStudent(); stu.SetPersonInfo("张三", "20090807", "男"); stu.SetStudentScore(900); stu.Display(); stu.DisplayScore();
namespace Inheritance {
public class clsPerson {
string strName; string strPNo; string strPSex; public void SetPersonInfo(string _strName, string _strPNo, string _strPSex) {
第 5 章 面向对象程序设计基础
5-9
//定义一个描述学生基本信息的类, //属性包括姓名、学号,C#、英语和数学成绩, //方法包括设置姓名和学号、设置三门课的成绩和输出相关学生的信息, //最后求出总成绩和平均成绩。
using System; using System.Collections.Generic; using System.Text;
}
5-12
//把定义平面直角坐标系上的一个点的类 clsPoint 作为基类, //派生出描述一条直线的类 clsLine, //再派生出一个矩形类 clsRect。 //要求方法能求出两点间的距离、矩形的周长和面积等。 //设计一个测试程序,并构造出完整的程序。
//根号下(|X1-X2|的平方+|Y1-Y2|的平方)这个就是两点间距离公式
dblX = _dblX; dblY = _dblY; } public void display() { Console.WriteLine("坐标点的 x 坐标为:" + dblX + ";y 坐标为:" + dblY); } public void setPoint(double _dblX, double _dblY) { dblX = _dblX; dblY = _dblY; } } class Test { static void Main() { clsPoint point = new clsPoint(60.00, 75.00); point.display(); point.setPoint(100.00, 120.00); point.display(); Console.Read(); } }
{ strName = _strName; strStuNo = _strStuNo; strCSharpScore = _strCShapeScore; strEnglishScore = _strEnglishScore; strMathsScore = _strMathsScore;
}
public void DisplayStudentInfo() {
clsRect rect = new clsRect(line1, line2, line3, line4);
double dblDistOfLine; dblDistOfLine = line1.getDistance(); Console.WriteLine("两点之间的距离为:" + dblDistOfLine);
public double getDistance() {
double dblDistance, dblDistance1, dblDistance2; dblDistance1 = Math.Pow( point1.dblX - point2.dblX,2.0); dblDistance2 = Math.Pow( point1.dblY - point2.dblY,2.0); dblDistance = Math.Sqrt(dblDistance1 + dblDistance2); return dblDistance; } } public class clsRect : clsLine { clsLine line1, line2, line3, line4; public clsRect(clsLine _line1,clsLine _line2,clsLine _line3,clsLine _line4) { line1 = _line1; line2 = _line2; line3 = _line3; line4 = _line4; }
namespace clsStudent {
class clsStudent {
string strName; string strStuNo; int strCSharpScore; int strEnglishScore; int strMathsScore;
public clsStudent(string _strName,string _strStuNo, int _strCShapeScore,int _strEnglishScore,int _strMathsScore)
int nScore; public void SetStudentScore(int _nScore) {
nScore = _nScore; }
public void DisplayScore() {
Console.WriteLine("成绩为:" + nScore); } } public class clsTeacher : clsPerson { int nSeniority; public void SetSeniority(int _nSeniority) {
double dblPerimeterOfRect, dblAreaOfRect; dblPerimeterOfRect = rect.getPerimeter(); dblAreaOfRect = rect.getArea(); Console.WriteLine("矩形的周长为:" + dblPerimeterOfRect); Console.WriteLine("矩形的面积为:" + dblAreaOfRect); } } }
clsTeacher teacher = new clsTeacher(); teacher.SetPersonInfo("李老师", "20000304", "男"); teacher.SetSeniority(15); teacher.Display(); teacher.DisplaySeniority(); } } }
5-11
//设有一个描述坐标点的 clsPoint 类, //其私有变量 x 和 y 代表一个点的 x、y 坐标值。 //编写程序实现以下功能: //利用构造函数传递参数,并设其默认参数值为 60 和 75, //利用方法 display()输出这一默认的值; //利用公有方法 setPoint()将坐标值修改为(100,120), //并利用方法输出修改后的坐标值。
public double getPerimeter() {
double dblPerimeter; dblPerimeter = 2 * (line1.getDistance() + line2.getDistance()); return dblPerimeter; }
public double getArea() {
strName = _strName; strPNo = _strPNo; strPSex = _strPSex; } public void Display() { Console.WriteLine("姓名为:" +
strName + ";编号为:" + strPNo + ";性别为:" + strPSex); } } public class clStudent : clsPerson {
} } class Test {
static void Main() {
clsStudent stu = new clsStudent("李三", "0900101", 90, 89, 99); stu.DisplayStudentInfo(); } } }
5-10
//定义一个人员类 clsPerson, //包括属性:姓名、编号、性别和用于输入输出的方法。 //在此基础上派生出学生类 clsStudent(增加成绩)和 //教师类 clsTeacher(增加教龄), //并实现对学生和教师信息的输入输出。
clsLine line1 = new clsLine(point2, point1);
clsLine line2 = new clsLine(point3, point2); clsLine line3 = new clsLine(point4, point3); clsLine line4 = new clsLine(point1, point4);
double dblArea; dblArea = line1.getDistance() * line2.getDistance(); return dblArea; } } class Test { static void Main() { clsPoint point1=new clsPoint(0.0,10.0); clsPoint point2=new clsPoint(20.0,10.0); clsPoint point3 = new clsPoint(20.0, 0); clsPoint point4 = new clsPoint(0.0, 0.0);
Console.WriteLine("该生的信息为:" ); Console.WriteLine("-------------------------------------"); Console.WriteLine("姓名为:" + strName); Console.WriteLine("学号为:" + strStuNo); Console.WriteLine("C#的成绩为:" + strCSharpScore); Console.WriteLine("英语的成绩为:" + strEnglishScore); Console.WriteLine("数学的成绩为:" + strMathsScore);
dblX = _dblX; dblY = _dblY; } } public class clsLine : clsPoint { clsPoint point1, point2; public clsLine() { } public clsLine(clsPoint _clsPoint1, clsPoint _clsPoint2) { point1 = _clsPoint1; point2 = _clsPoint2; }
//矩形的周长:(长+宽 )乘以 2 //矩形的面积:
using System; using System.Collections.Generic; using System.Text;
nameБайду номын сангаасpace Inheritance {
public class clsPoint {
public double dblX; public double dblY; public clsPoint() { } public clsPoint(double _dblX, double _dblY) {
using System; using System.Collections.Generic; using System.Text;
namespace Inheritance {
class clsPoint {
private double dblX; private double dblY; public clsPoint(double _dblX, double _dblY) {
nSeniority = _nSeniority; }
public void DisplaySeniority() {
Console.WriteLine("教龄为:" + nSeniority); } } public class clsTest { static void Main() {
clStudent stu = new clStudent(); stu.SetPersonInfo("张三", "20090807", "男"); stu.SetStudentScore(900); stu.Display(); stu.DisplayScore();