类与继承练习题.docx

上传人:h**** 文档编号:1372294 上传时间:2019-02-23 格式:DOCX 页数:9 大小:26KB
下载 相关 举报
类与继承练习题.docx_第1页
第1页 / 共9页
类与继承练习题.docx_第2页
第2页 / 共9页
类与继承练习题.docx_第3页
第3页 / 共9页
类与继承练习题.docx_第4页
第4页 / 共9页
类与继承练习题.docx_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、题:【考察基本类的定义和使用】编写一个矩形类,具有长(len)宽(wid )两个字段,默认构造函数为 len 和 wid 设置默认值,均为 0,有参数构造函数根据传入参数的值为 len 和 wid 设置具体值。此外类包含以下几个方法:取长度、取宽度、计算周长、计算面积、修改矩形的长度、修改矩形的宽度等方法。题一:【考察静态方法、静态字段的意义和使用方式】设计一个男女人数统计程序。定义一个 Person 类,包含姓名、性别、年龄三个私有字段。另包含 males 和 females 两个私有静态字段成员,用来记录男、女学生的人数;还有NumberMales 和 NumberFemales 两个公有

2、静态方法成员,这两个方法通过对静态字段males、females 的访问返回男生总人数、女生总人数。设计其构造函数,要求完成每个Person 对象的赋值和男女总人数的累加。【答案】using System;namespace Exp0202public enum Gender 男, 女;public class Person/私有静态字段,分别统计男女人数private static int males;private static int females;/公有字段,描述个人信息public string Name;public Gender Sex;public int Age;/构造函数

3、,用来初始化对象public Person (string name, Gender sex, int age)Name = name;Sex = sex;Age = age;if (sex = Gender.男)males+; /男生人数加 1if (sex = Gender.女)females+; /女生人数加 1/返回男生人数public static int NumberMales() return males;/返回女生人数public static int NumberFemales()Return females; /class Person 结束class Programsta

4、tic void Main (String args)/创建 Person 型的数组对象,用来记录 6 个人的信息Person ps = new Person 6;ps0 = new Person (“李伟峰”, Gender.男, 20);ps1 = new Person (“郭小雨”, Gender.女, 19);ps2 = new Person (“赵荣”, Gender.男, 22);ps3 = new Person (“刘恒”, Gender.男, 21);ps4 = new Person (“陈晶晶”, Gender.女, 21);ps5 = new Person (“张馨”, G

5、ender.女, 20);int NumOfMales = Person.NumberMales();int NumOfFemales = Person.NumberFemales();Console.WriteLine(“男生人数: 0”,NumOfMales);Console.WriteLine(“女生人数: 0”,NumOfFemales);Console.WriteLine(“学生名单如下: ”);foreach (Person p in ps)Console.Write(“0t”, p.Name);Console.Write(n);Console.ReadLine();题二:【考察具

6、有继承和派生的程序的编写基本形式、乘除法应用加减法的实现逻辑】编写一个程序,包含两个类,分别用于提供两个整数的加减运算和乘除运算功能,要求具有乘除运算功能的类派生自具有加减运算功能的类,且乘法和除法的实现都不可以使用 C#的自有运算符“* ”和“” 。主函数通过对定义的类的对象的生产完成两个整数 100 和 23的加、减、乘、除,并输出运算结果。【答案】/JiajianChengchu.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Calculatecl

7、ass Jiajian/protected int x, y;/*public Jiajian(int xValue, int yValue)x = xValue;y = yValue;*/public int myadd(int x, int y)return x + y;public int mysub(int x, int y)return x - y;class Chengchu : Jiajian/private int prod;/private int quot;/*public Chengchu(int xValue, int yValue):base(xValue, yVal

8、ue) */public int myMul(int x, int y)int prod = 0;for(int i=0; i= y)balance -= y;quot+;return quot;/Program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Calculateclass Programstatic void Main(string args)int a = 100;int b = 23;Jiajian jiajianObj = new

9、Jiajian();int sum = jiajianObj.myadd(a, b);Console.WriteLine(“100+23=0“, sum);int dif = jiajianObj.mysub(a, b);Console.WriteLine(“100-23=0“, dif);Chengchu chengchuObj = new Chengchu();int prod = chengchuObj.myMul(a, b);Console.WriteLine(“100*23=0“, prod);int quot = chengchuObj.myDiv(a, b);Console.Wr

10、iteLine(“100/23=0“, quot);题三:【考察继承与派生,protected 与 private 的继承性,virtual 方法与 override 功能】编写一个学生和教师的信息打印程序。具体要求:定义一个 Person 类,包含编号、姓名两个字段,以及打印虚方法 infoPrint。定义由 Person 类派生出得两个类: Teacher 类和Student 类。其中 Teacher 类额外包含职称、部门两个字段,及一个 infoPrint 的重写方法,用于打印 teacher 的全部信息;Student 类额外包含班级号、成绩两个字段,和一个infoPrint 的重写方

11、法,用于打印 Student 的全部信息。主函数中定义一个 Student 类对象,姓名:夏雨,编号:101,班级号:2 ,成绩:90 ;和一个 Teacher 类对象,姓名:王宁,编号:86,职称:教授,部门:计算机。并打印各自的信息。考虑以下两种情况的编程细节差别:(1 ) Person 中的编号和姓名为 protected 类型;(2 ) Person 中的编号和姓名为 private 类型;【答案】:情况一:/Person.csusing System;using System.Collections.Generic;using System.Linq;using System.Tex

12、t;namespace TeacherAndStudentclass Personprotected int id;protected string name;public Person(int pid, string pname)id = pid;name = pname;public virtual void infoPrint()Console.WriteLine(“Persons Id: 0“, id);Console.WriteLine(“Persons Name: 0“, name);class Teacher : Personprivate string title;privat

13、e string department;public Teacher(int tid, string tname, string ttitle, string tdep): base(tid, tname)title = ttitle;department = tdep;public override void infoPrint()Console.WriteLine(“ Teachers Id: 0“, id);Console.WriteLine(“ Teachers Name: 0“, name);Console.WriteLine(“ Teachers Title: 0“, title)

14、;Console.WriteLine(“ Teachers Department: 0“, department);class Student : Personprivate int classNum ;private int score;public Student(int tid, string tname, int cNum, int sco): base(tid, tname)classNum = cNum;score = sco;public override void infoPrint()Console.WriteLine(“ Students ID: 0“, id);Conso

15、le.WriteLine(“ Students Name: 0“, name);Console.WriteLine(“ Students Class number: 0“, classNum);Console.WriteLine(“ Students Score: 0“, score);/Program.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TeacherAndStudentclass Programstatic void Main(string

16、 args)Console.WriteLine(“A Teacher:“);Teacher t = new Teacher(86, “WangNing“, “professor“, “CS“);t.infoPrint();Console.WriteLine(“A Student:“);Student s = new Student(101, “XiaYu“, 2, 90);s.infoPrint();情况二:/Person.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;na

17、mespace TeacherAndStudentclass Personprivate int id;private string name;public Person(int pid, string pname)id = pid;name = pname;public virtual void infoPrint()Console.WriteLine(“Id: 0“, id);Console.WriteLine(“Name: 0“, name);class Teacher : Personprivate string title;private string department;publ

18、ic Teacher(int tid, string tname, string ttitle, string tdep): base(tid, tname)title = ttitle;department = tdep;public override void infoPrint()base.infoPrint();Console.WriteLine(“Title: 0“, title);Console.WriteLine(“Department: 0“, department);class Student : Personprivate int classNum ;private int

19、 score;public Student(int tid, string tname, int cNum, int sco): base(tid, tname)classNum = cNum;score = sco;public override void infoPrint()base.infoPrint();Console.WriteLine(“Class number: 0“, classNum);Console.WriteLine(“Score: 0“, score);/Program.csusing System;using System.Collections.Generic;u

20、sing System.Linq;using System.Text;namespace TeacherAndStudentclass Programstatic void Main(string args)Console.WriteLine(“A Teacher:“);Teacher t = new Teacher(86, “WangNing“, “professor“, “CS“);t.infoPrint();Console.WriteLine(“A Student:“);Student s = new Student(101, “XiaYu“, 2, 90);s.infoPrint();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 试题真题

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。