题〇:
【考察基本类的定义和使用】
编写一个矩形类,具有长(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); } }