类与继承练习题
更新时间:2024-06-11 03:57:01 阅读量: 综合文库 文档下载
- 类的继承推荐度:
- 相关推荐
题〇:
【考察基本类的定义和使用】
编写一个矩形类,具有长(len)宽(wid)两个字段,默认构造函数为len和wid设置默认值,均为0,有参数构造函数根据传入参数的值为len和wid设置具体值。此外类包含以下几个方法:取长度、取宽度、计算周长、计算面积、修改矩形的长度、修改矩形的宽度等方法。
题一:
【考察静态方法、静态字段的意义和使用方式】
设计一个男女人数统计程序。定义一个Person类,包含姓名、性别、年龄三个私有字段。另包含males和females两个私有静态字段成员,用来记录男、女学生的人数;还有NumberMales和NumberFemales两个公有静态方法成员,这两个方法通过对静态字段males、females的访问返回男生总人数、女生总人数。设计其构造函数,要求完成每个Person对象的赋值和男女总人数的累加。 【答案】
using System;
namespace Exp0202 {
public enum Gender {男, 女}; public class Person {
//私有静态字段,分别统计男女人数 private static int males; private static int females; //公有字段,描述个人信息 public string Name; public Gender Sex; public int Age;
//构造函数,用来初始化对象
public Person (string name, Gender sex, int age) {
Name = name; Sex = sex; Age = age;
if (sex == Gender.男)
males++; //男生人数加1 if (sex == Gender.女)
females++; //女生人数加1
}
//返回男生人数
public static int NumberMales() {
return males; }
//返回女生人数
public static int NumberFemales() {
Return females; }
} //class Person结束
class Program {
static void Main (String[ ] args) {
//创建Person型的数组对象,用来记录6个人的信息 Person[ ] ps = new Person [6];
ps[0] = new Person (“李伟峰”, Gender.男, 20); ps[1] = new Person (“郭小雨”, Gender.女, 19);
ps[2] = new Person (“赵荣”, Gender.男, 22); ps[3] = new Person (“刘恒”, Gender.男, 21); ps[4] = new Person (“陈晶晶”, Gender.女, 21); ps[5] = new Person (“张馨”, Gender.女, 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(“{0}\\t”, p.Name); }
Console.Write(‘\\n’); Console.ReadLine(); } } }
题二:
【考察具有继承和派生的程序的编写基本形式、乘除法应用加减法的实现逻辑】
编写一个程序,包含两个类,分别用于提供两个整数的加减运算和乘除运算功能,要求具有乘除运算功能的类派生自具有加减运算功能的类,且乘法和除法的实现都不可以使用C#的
自有运算符“*”和“\\”。主函数通过对定义的类的对象的生产完成两个整数100和23的加、减、乘、除,并输出运算结果。 【答案】
//JiajianChengchu.cs
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace Calculate {
class 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, yValue) { }*/
public int myMul(int x, int y) {
int prod = 0; for(int i=0; i prod = myadd(prod, x); } return prod; } public int myDiv(int x, int y) { int balance = x; int quot = 0; while (balance >= y) { balance -= y; quot++; } return quot; } } } //Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculate { class Program { static void Main(string[] args) { int a = 100; int b = 23; Jiajian jiajianObj = new Jiajian(); int sum = jiajianObj.myadd(a, b); Console.WriteLine(\, sum); int dif = jiajianObj.mysub(a, b); Console.WriteLine(\, dif); Chengchu chengchuObj = new Chengchu(); int prod = chengchuObj.myMul(a, b); Console.WriteLine(\, prod); int quot = chengchuObj.myDiv(a, b); Console.WriteLine(\, quot); } } } 题三: 【考察继承与派生,protected与private的继承性,virtual方法与override功能】 编写一个学生和教师的信息打印程序。具体要求:定义一个Person类,包含编号、姓名两个字段,以及打印虚方法infoPrint。定义由Person类派生出得两个类:Teacher类和Student类。其中Teacher类额外包含职称、部门两个字段,及一个infoPrint的重写方法,用于打印teacher的全部信息;Student类额外包含班级号、成绩两个字段,和一个infoPrint的重写方法,用于打印Student的全部信息。 主函数中定义一个Student类对象,姓名:夏雨,编号:101,班级号:2,成绩:90;和一个Teacher类对象,姓名:王宁,编号:86,职称:教授,部门:计算机。并打印各自的信息。 考虑以下两种情况的编程细节差别: (1) Person中的编号和姓名为protected类型; (2) Person中的编号和姓名为private类型; 【答案】: 情况一: //Person.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TeacherAndStudent { class Person { protected int id; protected string name; public Person(int pid, string pname) { id = pid; name = pname; } public virtual void infoPrint() { Console.WriteLine(\, id); Console.WriteLine(\, name); } } class Teacher : Person { private string title; private 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(\ Teacher's Id: {0}\, id); Console.WriteLine(\ Teacher's Name: {0}\, name); Console.WriteLine(\ Teacher's Title: {0}\, title); Console.WriteLine(\ Teacher's Department: {0}\, department); } } class Student : Person { private 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(\ Student's ID: {0}\, id); Console.WriteLine(\ Student's Name: {0}\, name); Console.WriteLine(\ Student's Class number: {0}\, classNum); Console.WriteLine(\ Student's Score: {0}\, score); } } } //Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TeacherAndStudent { class Program { static void Main(string[] args) { Console.WriteLine(\); Teacher t = new Teacher(86, \, \, \); t.infoPrint(); Console.WriteLine(\); Student s = new Student(101, \, 2, 90); s.infoPrint(); } } } 情况二: //Person.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TeacherAndStudent { class Person { private int id; private string name; public Person(int pid, string pname) { id = pid; name = pname; } public virtual void infoPrint() { Console.WriteLine(\, id); Console.WriteLine(\, name); } } class Teacher : Person { private string title; private string department; public 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); Console.WriteLine(\, department); } } class Student : Person { private 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() { base.infoPrint(); Console.WriteLine(\, classNum); Console.WriteLine(\, score); } } } //Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TeacherAndStudent { class Program { static void Main(string[] args) { Console.WriteLine(\); Teacher t = new Teacher(86, \, \, \); t.infoPrint(); Console.WriteLine(\); Student s = new Student(101, \, 2, 90); s.infoPrint(); } } }
正在阅读:
类与继承练习题06-11
自助加油站安全管理问题的探讨05-16
班委会的建立和工作情况反思03-06
三年级下册科学教学反思青岛版05-04
合同管理专员岗位职责04-26
售后专员岗位职责108-11
法务专员岗位职责02-25
人事专员岗位职责07-18
第六章 信用证案例09-30
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 练习题
- 继承
- 作文的题材和体裁
- 计算机三级100上机题
- 神经科常用评分
- 2013年房地产经纪人《制度与政策》答题技巧每日一讲(4月11日)
- 给水工艺流程和给水构筑物的工艺选择 大作业
- 吉林省公务员考试申论热点:大人应该学学小学生怎么过马路
- 广州市“三旧”改造中的城市更新发展研究——猎德村
- 蜡烛教案
- 2010年的爱心家教策划书
- 广东省创新杯说课大赛计算机类一等奖作品:《光纤熔接》说课稿
- 篮球兴趣小组教案 - 图文
- 章程规则制度
- 2018年高处安装、维护、拆除作业题库
- java软件开发——顶岗实习周记25篇
- 公共基础知识汇总
- 安徽省2018年安全员B证考核考试试题
- 苏州市2017-2018学年第二学期初三年级数学学科二模考试试卷
- 管理信息系统习题5
- 小学一年级下学期家长会方案(1)(3页)
- 法硕考研科目知识总结