题目一:请定义一个交通工具(Vehicle)的类,其中有:
⦁ 字段:速度(speed),体积(size);
⦁ 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown();
在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。
源程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace MyExer3
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Vehicle v1 = new Vehicle();
- v1.SetSpeed(10);
- v1.SetSize(50);
- Console.WriteLine("初始化:speed={0},size={1}", v1.Speed, v1.Size);
- v1.SpeedUp(5);
- Console.WriteLine("加速后:" + v1.Speed);
- v1.SpeedDown(9);
- Console.WriteLine("减速后:" + v1.Speed);
- Console.ReadLine();
- }
- }
- class Vehicle { //定义一个交通工具类
-
- private int speed;
- private int size;
-
- public int Speed {
- get { return speed; }
- set { speed = value; }
- }
-
- public int Size {
- get { return size; }
- set { size = value; }
- }
-
- public void Move() {
- }
-
- public void SetSpeed(int speed) {
- this.speed = speed;
- Console.WriteLine("速度为:"+speed);
- }
-
- public void SetSize(int size)
- {
- this.Size = size;
- Console.WriteLine("体积为:" + size);
- }
-
- public void SpeedUp(int a) {
- speed += a;
- }
-
- public void SpeedDown(int a) {
- speed -= a;
- }
- }
- }
运行结果:

题目二:试编写一个函数compute(int a,int b,int sum,int sub,int mul,int quo ),其中sum、sub、mul、quo分别是a,b的和、差、积、商。并编写主函数测试上述函数。提示:和、差、积、商分别采用ref和out形参两种方式实现。
源程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace exer3
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int a = 10;
- int b = 5;
- int sum = 0;
- int sub = 0;
- int mul = 0;
- int quo = 0;
- Console.WriteLine("以下采用ref方式:");
- compute(2,ref b,sum,sub,mul,quo);
- Console.WriteLine("以下采用out方式:");
- compute2(a, out b, sum, sub, mul, quo);
- Console.ReadLine();
- }
-
- //ref方法
- public static void compute(int a,ref int b, int sum, int sub, int mul, int quo) {
- sum = a + b;
- sub = a - b;
- mul = a * b;
- quo = a / b;
- string s = "和:" + sum + " 差:" + sub + " 积:" + mul + " 商:" + quo;
- Console.WriteLine(s);
- }
-
- public static void compute2(int a,out int b, int sum, int sub, int mul, int quo)
- {
- b = 3;
- sum = a + b;
- sub = a - b;
- mul = a * b;
- quo = a / b;
- string s = "和:" + sum + " 差:" + sub + " 积:" + mul + " 商:" + quo;
- Console.WriteLine(s);
- }
- }
- }
运行结果:

题目三:定义一个接口,它含有两个方法:一个方法用于实现两个数中求最小的数,一个方法用于实现在2个数中求最大的数。定义一个类实现这个接口,再定义派生类中,给出方法Max()、方法Min()的实现; 试在主函数中通过委托MyDelegate的对象md来分别调用上述两个方法Max、Min求一个一维数组的最大值及最小值。
源程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace exer2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- MyTest myTest = new MyTest();
- MyDelegate md1 = new MyDelegate(myTest.Max);
- MyDelegate md2 = new MyDelegate(myTest.Min);
- Console.WriteLine("在一维数组[90,45,27]中最大值为:{0},最小值为:{1}" ,md1(90,45,27), md2(90, 45, 27));
- Console.ReadLine();
- }
- }
-
- public interface Sort {
- int Max(int a,int b,int c);
- int Min(int a,int b,int c);
- }
-
- public delegate int MyDelegate(int a,int b,int c);
- public class MyTest : Sort
- {
- public int Max(int a,int b,int c)
- {
- int max = 0;
- if (a > b)
- {
- max = a;
- }
- else {
- max = b;
- }
- if (max > c)
- {
- return max;
- }
- else {
- return c;
- }
- }
-
- public int Min(int a, int b, int c)
- {
- int min = 0;
- if (a < b)
- {
- min = a;
- }
- else
- {
- min = b;
- }
- if (min < c)
- {
- return min;
- }
- else
- {
- return c;
- }
- }
- }
- }
运行结果:

题目四:定义一个抽象的"Role"类,有用户名,密码,权限等成员变量
1)要求尽可能隐藏所有变量,再通过属性对各变量进行读写。再定义一个具有抽象的play()方法,
该方法只输出用户名,不返回任何值,同时至少定义两个构造方法。
2)从Role类派生出一个"Student"类,该类具有Role类的所有成员(构造方法除外),并扩展number成员变量,同时增加一个静态成员变量“telephone”。
同样要有至少两个构造方法,并定义覆盖play()方法。
3)在Main()方法中生成Student的对象,并测试这些对象的方法。
源程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace exer4
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Student student = new Student("biubiu","@lwy",false,105,12345);
- student.play();
- student.printStudent();
- Console.ReadLine();
- }
- }
-
- public abstract class Role {
- private string name;
- private string pwd;
- private bool authority;//权限
-
- public Role() {
- }
-
- public Role(string name,string pwd,bool authority) {
- this.name = name;
- this.pwd = pwd;
- this.authority = authority;
- }
-
- public string Name {
- get { return name; }
- set { name = value; }
- }
-
- public string Pwd {
- get { return pwd; }
- set { pwd = value; }
- }
-
- public bool Authority
- {
- get { return authority; }
- set { authority = value; }
- }
-
- public abstract void play();
- }
-
- public class Student : Role {
- private int number;
- static int telephone;
-
- public Student() {
- }
-
- public Student(Role role, int number)
- {
- Role = role;
- this.number = number;
- }
-
- public Student(string name, string pwd, bool authority,int number,int tel) {
- Name = name;
- Pwd = pwd;
- Authority = authority;
- this.number = number;
- telephone = tel;
- }
-
- public Role Role {
- get { return Role; }
- set { Role = value; }
- }
-
- public int Number {
- get { return number; }
- set{ number = value; }
- }
-
- public override void play() {
- Console.WriteLine("已执行play()");
- }
-
- public void printStudent() //打印输出Student类
- {
- Console.WriteLine("学生信息: 姓名:{0},密码:{1},是否具有权限:{2},学号:{3},电话:{4}", Name, Pwd, Authority, number, telephone);
- }
- }
- }
运行结果:

题目五:创建C#控制台应用程序,建立一个点类CzPoint,为其定义两个double类型的私有字段成员x和y,分别表示点的横坐标和纵坐标;对CzPoint类进行相等和不等操作符重载。注:两个坐标点相等,则指它们的横坐标和纵坐标都相等。
在Main()方法中对两个坐标点是否相同进行测试。
源程序:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace exer5
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Console.Write("请输入第一个点的横坐标:");
- double x1=double.Parse(Console.ReadLine());
- Console.Write("请输入第一个点的纵坐标:");
- double y1 = double.Parse(Console.ReadLine());
- Console.Write("请输入第二个点的横坐标:");
- double x2 = double.Parse(Console.ReadLine());
- Console.Write("请输入第二个点的纵坐标:");
- double y2 = double.Parse(Console.ReadLine());
- CzPoint c1 = new CzPoint(x1, y1);
- CzPoint c2 = new CzPoint(x2, y2);
- Console.WriteLine(c1 == c2);
- Console.ReadLine();
- }
- }
-
- class CzPoint
- { //判断坐标是否相等
- private double x;
- private double y;
-
- public double X
- {
- get { return x; }
- set { x = value; }
- }
-
- public double Y
- {
- get { return y; }
- set { y = value; }
- }
-
- public CzPoint()
- {
- }
-
- public CzPoint(double x, double y)
- {
- this.x = x;
- this.y = y;
- }
-
- //重载相等
- public static string operator ==(CzPoint c1, CzPoint c2)
- {
- if (c1.x == c2.x && c1.y == c2.y)
- {
- return "两点相等";
- }
- else
- {
- return "两点不等";
- }
- }
- //重载不等
- public static string operator !=(CzPoint c1, CzPoint c2)
- {
- if (c1.x == c2.x || c1.y == c2.y)
- {
- return "两点相等";
- }
- else
- {
- return "两点不等";
- }
- }
- }
- }
运行结果:
测试相等:

测试不等:
