P106:5-9、定义学生类,属性包括姓名、学号,c#、英语和数学成绩,方法包括设置姓名和学号、设置三门课的成绩和输出相关学生的信息,最后求出总成绩和平均成绩。 class student //创建学生类 {
public string name, stunumber; //姓名、学号属性 public double csharp,math,english;//三门成绩
public void setstudent(string name, string number) //设置姓名,学号 {
this.name = name;
this.stunumber = number; }
public void setscore(double csharp, double math, double english)//设置分数 {
this.csharp = csharp; this.math = math; this.english = english; }
public void show() //显示信息 {
Console.WriteLine(\the score of the student:\\nc#:{2},math:{3},english:{4}\ }
} /*创建学生类完成*/
static void Main(string[] args) //入口函数 { double fullscore; //总分 double avescore; //平均分
student s1 = new student(); //创建一个学生对象s1 s1.setstudent(\ s1.setscore(98,98,98.7); s1.show(); //现实学生信息
fullscore = s1.csharp + s1.math + s1.english; avescore = fullscore / 3; //算出平均分
Console.WriteLine(\student's fullscore :{0}\\naverage score :{1,6:f2} \ }
P106:5-10、定义一个人员类clsPerson,包括属性:姓名、编号、性别和用于输入输出地方法,在此基础上派生出学生类clsStudent(增加成绩)和教师类clsTeacher(增加教龄),并实现对学生和教师的信息的输入输出。 class clsPerson {
public string name,number,sex; //属性
public void input(string name, string number,string sex)//输入信息 {
this.name = name; this.number = number; this.sex = sex; }
public void output() {
Console.WriteLine(\输出信息 } }
class clsStudent:clsPerson//继承person {
public double score;
public void input(string name, string number, string sex, double score) {
base.input( name, number, sex);//继承基类输入 this.score = score;
}
public void output() {
base.output();//继承基类输出
Console.WriteLine(\ } }
class clsTeacher : clsPerson//继承person {
public int tchage;
public void input(string name, string number, string sex, int tchage) {
base.input(name, number, sex); this.tchage = tchage;
}
public void output() {
base.output();
Console.WriteLine(\ } }
static void Main(string[] args)//入口函数 {
clsStudent s1 = new clsStudent(); //创建一个学生类对象s1 clsTeacher t1 = new clsTeacher(); //创建一个教师类对象t1
s1.input(\范朦\ s1.output();
t1.input(\ t1.output(); }
(复习重点)P106:5-11、设有一个描述坐标点的clsPoint类,其私有变量x和y代表一个点的x,y坐标值。编写程序实现以下功能:利用构造函数传递参数,并设其默认参数值为60和75,利用方法display()输出这一默认的值;利用公有方法setPoint()将坐标值修改为(100,120),并利用方法输出修改后的坐标值。
class clsPoint//创建类 {
int x, y;//属于私有属性,此处private 省略
public clsPoint(int x, int y)//构造函数,就是对一个对象的初始化 {
this.x = x; this.y = y; }
public void setpoint(int x,int y)//公有方法设置坐标值 {
this.x=x; this.y=y; }
public void show()//显示坐标 {
Console.WriteLine(\ } }
static void Main(string[] args)//入口函数 {
clsPoint p1 = new clsPoint(60,75);//与int i=0作用相同,就是使对象有初始值
p1.show();
p1.setpoint(100,200);
Console.WriteLine(\ p1.show(); }
(复习重点)P106:5-12、把定义平面直角坐标系上的一个点的类clsPoint作为基类,派生出描述一条直线的类clsLine,再派生出一个矩形类clsRect。要求方法能求出两点间的距离,矩形的周长和面积等。设计一个测试程序,并构造出完整的程序。(考察get()set()和继承) class clsPoint //创建类 {
protected double x, y; public double X {
get //get\\set 的作用是将保护类型的属性转换为隐形可继承 用对象继承(考点) {
return this.x; } set {
this.x=value; } }
public double Y {
get {
return this.y; } set {
this.y= value; } }
public clsPoint() { }//定义空的构造函数,增加程序严谨性 public clsPoint(double x, double y) {
this.x = x; this.y = y; }
public void show() {
Console.WriteLine(\//输出点坐标 } }
class clsLine:clsPoint//创建clsLine类,继承clsPoint {
public clsPoint p = new clsPoint(); //定义一个点对象
public clsLine() { }
public clsLine(double x, double y, double x1, double y1) : base(x, y) {
p.X = x1; p.Y = y1; }
public double count1() //计算线段长度 {
double longth;
longth = Math.Sqrt((p.X - x) * (p.X - x) + (p.Y- y) * (p.Y - y)); return longth; }
public void show1() //输出相关信息 {
base.show();
Console.WriteLine(\
Console.WriteLine(\线段长度:{0,6:f2}\ } }
class clsRec : clsLine {
public clsRec(){}
public clsRec(double x, double y, double x1, double y1):base(x,y,x1,y1){}//继承线段构造函数
protected double getcs(out double s) //计算面积周长方法 {
double c;
c=2*(Math.Abs((x-base.p.X))+Math.Abs((y-base.p.Y))); //矩形周长 s=Math.Abs((x-base.p.X))*Math.Abs((y-base.p.Y)); //矩形面积 return c; }