• 基于net的应用开发技术-上机三


    题目一:请定义一个交通工具(Vehicle)的类,其中有:

    ⦁ 字段:速度(speed),体积(size);

    ⦁ 方法:移动(move()),设置速度(setSpeed(int speed)),设置体积(setSize(int size))加速speedUp(),减速speedDown();

    在测试类Vehicle中的main()中实例化一个交通工具对象,通过方法给它初始化speed,size的值,并打印出来。另外,调用加速,减速的方法对速度进行改变。

    源程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace MyExer3
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Vehicle v1 = new Vehicle();
    13. v1.SetSpeed(10);
    14. v1.SetSize(50);
    15. Console.WriteLine("初始化:speed={0},size={1}", v1.Speed, v1.Size);
    16. v1.SpeedUp(5);
    17. Console.WriteLine("加速后:" + v1.Speed);
    18. v1.SpeedDown(9);
    19. Console.WriteLine("减速后:" + v1.Speed);
    20. Console.ReadLine();
    21. }
    22. }
    23. class Vehicle { //定义一个交通工具类
    24. private int speed;
    25. private int size;
    26. public int Speed {
    27. get { return speed; }
    28. set { speed = value; }
    29. }
    30. public int Size {
    31. get { return size; }
    32. set { size = value; }
    33. }
    34. public void Move() {
    35. }
    36. public void SetSpeed(int speed) {
    37. this.speed = speed;
    38. Console.WriteLine("速度为:"+speed);
    39. }
    40. public void SetSize(int size)
    41. {
    42. this.Size = size;
    43. Console.WriteLine("体积为:" + size);
    44. }
    45. public void SpeedUp(int a) {
    46. speed += a;
    47. }
    48. public void SpeedDown(int a) {
    49. speed -= a;
    50. }
    51. }
    52. }

    运行结果:

    题目二:试编写一个函数compute(int a,int b,int sum,int sub,int mul,int quo ),其中sum、sub、mul、quo分别是a,b的和、差、积、商。并编写主函数测试上述函数。提示:和、差、积、商分别采用ref和out形参两种方式实现。

    源程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace exer3
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. int a = 10;
    13. int b = 5;
    14. int sum = 0;
    15. int sub = 0;
    16. int mul = 0;
    17. int quo = 0;
    18. Console.WriteLine("以下采用ref方式:");
    19. compute(2,ref b,sum,sub,mul,quo);
    20. Console.WriteLine("以下采用out方式:");
    21. compute2(a, out b, sum, sub, mul, quo);
    22. Console.ReadLine();
    23. }
    24. //ref方法
    25. public static void compute(int a,ref int b, int sum, int sub, int mul, int quo) {
    26. sum = a + b;
    27. sub = a - b;
    28. mul = a * b;
    29. quo = a / b;
    30. string s = "和:" + sum + " 差:" + sub + " 积:" + mul + " 商:" + quo;
    31. Console.WriteLine(s);
    32. }
    33. public static void compute2(int a,out int b, int sum, int sub, int mul, int quo)
    34. {
    35. b = 3;
    36. sum = a + b;
    37. sub = a - b;
    38. mul = a * b;
    39. quo = a / b;
    40. string s = "和:" + sum + " 差:" + sub + " 积:" + mul + " 商:" + quo;
    41. Console.WriteLine(s);
    42. }
    43. }
    44. }

    运行结果:

    题目三:定义一个接口,它含有两个方法:一个方法用于实现两个数中求最小的数,一个方法用于实现在2个数中求最大的数。定义一个类实现这个接口,再定义派生类中,给出方法Max()、方法Min()的实现; 试在主函数中通过委托MyDelegate的对象md来分别调用上述两个方法Max、Min求一个一维数组的最大值及最小值。

    源程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace exer2
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. MyTest myTest = new MyTest();
    13. MyDelegate md1 = new MyDelegate(myTest.Max);
    14. MyDelegate md2 = new MyDelegate(myTest.Min);
    15. Console.WriteLine("在一维数组[90,45,27]中最大值为:{0},最小值为:{1}" ,md1(90,45,27), md2(90, 45, 27));
    16. Console.ReadLine();
    17. }
    18. }
    19. public interface Sort {
    20. int Max(int a,int b,int c);
    21. int Min(int a,int b,int c);
    22. }
    23. public delegate int MyDelegate(int a,int b,int c);
    24. public class MyTest : Sort
    25. {
    26. public int Max(int a,int b,int c)
    27. {
    28. int max = 0;
    29. if (a > b)
    30. {
    31. max = a;
    32. }
    33. else {
    34. max = b;
    35. }
    36. if (max > c)
    37. {
    38. return max;
    39. }
    40. else {
    41. return c;
    42. }
    43. }
    44. public int Min(int a, int b, int c)
    45. {
    46. int min = 0;
    47. if (a < b)
    48. {
    49. min = a;
    50. }
    51. else
    52. {
    53. min = b;
    54. }
    55. if (min < c)
    56. {
    57. return min;
    58. }
    59. else
    60. {
    61. return c;
    62. }
    63. }
    64. }
    65. }

    运行结果:

    题目四:定义一个抽象的"Role"类,有用户名,密码,权限等成员变量

    1)要求尽可能隐藏所有变量,再通过属性对各变量进行读写。再定义一个具有抽象的play()方法,

    该方法只输出用户名,不返回任何值,同时至少定义两个构造方法。

    2)从Role类派生出一个"Student"类,该类具有Role类的所有成员(构造方法除外),并扩展number成员变量,同时增加一个静态成员变量“telephone”。

    同样要有至少两个构造方法,并定义覆盖play()方法。

    3)在Main()方法中生成Student的对象,并测试这些对象的方法。

    源程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace exer4
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Student student = new Student("biubiu","@lwy",false,105,12345);
    13. student.play();
    14. student.printStudent();
    15. Console.ReadLine();
    16. }
    17. }
    18. public abstract class Role {
    19. private string name;
    20. private string pwd;
    21. private bool authority;//权限
    22. public Role() {
    23. }
    24. public Role(string name,string pwd,bool authority) {
    25. this.name = name;
    26. this.pwd = pwd;
    27. this.authority = authority;
    28. }
    29. public string Name {
    30. get { return name; }
    31. set { name = value; }
    32. }
    33. public string Pwd {
    34. get { return pwd; }
    35. set { pwd = value; }
    36. }
    37. public bool Authority
    38. {
    39. get { return authority; }
    40. set { authority = value; }
    41. }
    42. public abstract void play();
    43. }
    44. public class Student : Role {
    45. private int number;
    46. static int telephone;
    47. public Student() {
    48. }
    49. public Student(Role role, int number)
    50. {
    51. Role = role;
    52. this.number = number;
    53. }
    54. public Student(string name, string pwd, bool authority,int number,int tel) {
    55. Name = name;
    56. Pwd = pwd;
    57. Authority = authority;
    58. this.number = number;
    59. telephone = tel;
    60. }
    61. public Role Role {
    62. get { return Role; }
    63. set { Role = value; }
    64. }
    65. public int Number {
    66. get { return number; }
    67. set{ number = value; }
    68. }
    69. public override void play() {
    70. Console.WriteLine("已执行play()");
    71. }
    72. public void printStudent() //打印输出Student类
    73. {
    74. Console.WriteLine("学生信息: 姓名:{0},密码:{1},是否具有权限:{2},学号:{3},电话:{4}", Name, Pwd, Authority, number, telephone);
    75. }
    76. }
    77. }

    运行结果:

    题目五:创建C#控制台应用程序,建立一个点类CzPoint,为其定义两个double类型的私有字段成员x和y,分别表示点的横坐标和纵坐标;对CzPoint类进行相等和不等操作符重载。注:两个坐标点相等,则指它们的横坐标和纵坐标都相等。

    在Main()方法中对两个坐标点是否相同进行测试。

    源程序:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace exer5
    7. {
    8. internal class Program
    9. {
    10. static void Main(string[] args)
    11. {
    12. Console.Write("请输入第一个点的横坐标:");
    13. double x1=double.Parse(Console.ReadLine());
    14. Console.Write("请输入第一个点的纵坐标:");
    15. double y1 = double.Parse(Console.ReadLine());
    16. Console.Write("请输入第二个点的横坐标:");
    17. double x2 = double.Parse(Console.ReadLine());
    18. Console.Write("请输入第二个点的纵坐标:");
    19. double y2 = double.Parse(Console.ReadLine());
    20. CzPoint c1 = new CzPoint(x1, y1);
    21. CzPoint c2 = new CzPoint(x2, y2);
    22. Console.WriteLine(c1 == c2);
    23. Console.ReadLine();
    24. }
    25. }
    26. class CzPoint
    27. { //判断坐标是否相等
    28. private double x;
    29. private double y;
    30. public double X
    31. {
    32. get { return x; }
    33. set { x = value; }
    34. }
    35. public double Y
    36. {
    37. get { return y; }
    38. set { y = value; }
    39. }
    40. public CzPoint()
    41. {
    42. }
    43. public CzPoint(double x, double y)
    44. {
    45. this.x = x;
    46. this.y = y;
    47. }
    48. //重载相等
    49. public static string operator ==(CzPoint c1, CzPoint c2)
    50. {
    51. if (c1.x == c2.x && c1.y == c2.y)
    52. {
    53. return "两点相等";
    54. }
    55. else
    56. {
    57. return "两点不等";
    58. }
    59. }
    60. //重载不等
    61. public static string operator !=(CzPoint c1, CzPoint c2)
    62. {
    63. if (c1.x == c2.x || c1.y == c2.y)
    64. {
    65. return "两点相等";
    66. }
    67. else
    68. {
    69. return "两点不等";
    70. }
    71. }
    72. }
    73. }

    运行结果:

    测试相等:

    测试不等:

  • 相关阅读:
    Python基础入门篇【18】--python中的流程控制之条件判断
    【Html】交通灯问题
    C#基于BytesIO程序包的TCP Client客户端窗体程序
    IT运维面试必须具备的排障知识题库
    CentoS7 安装篇十二:mysql主从搭建(xtrackbackup不停机搭建)
    从零搭建基于SpringCloud Alibaba 鉴权中心服务(详细教程)
    测试左移:传统测试方式该如何过渡
    【C#】RestSharp踩坑日记
    编写Dockerfile
    【云IDE】尝试着操作了云IDE感觉蛮好用的操作步骤总结了一下来看看,提提建议
  • 原文地址:https://blog.csdn.net/qq_61727355/article/details/126262619