当前位置:   article > 正文

C# 随机得到十个人的身份_c#,生成随机身份证

c#,生成随机身份证

创建一个person类在里面写出一个方法persondoing;

之后student类,man类,women类,zjr类都以person类为父类;

再写出各自的方法;(这里不使用多态)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace 随机得到十个人的身份
  7. {
  8. public class person
  9. {
  10. public void persondoing()
  11. {
  12. Console.WriteLine("我是人类");
  13. }
  14. }
  15. public class student : person
  16. {
  17. public void studentdoing()
  18. {
  19. Console.WriteLine("我是学生");
  20. }
  21. }
  22. public class man : person
  23. {
  24. public void mandoing()
  25. {
  26. Console.WriteLine("我是男人");
  27. }
  28. }
  29. public class women : person
  30. {
  31. public void womendoing()
  32. {
  33. Console.WriteLine("我是女人");
  34. }
  35. }
  36. public class zjr : person
  37. {
  38. public void zjrdoing()
  39. {
  40. Console.WriteLine("我是神");
  41. }
  42. }
  43. }

main方法里,创建父类person对象per[]数组, 

  1. using System;
  2. namespace 随机得到十个人的身份
  3. {
  4. internal class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. // person p = new person();
  9. // student s = new student();
  10. // man m = new man();
  11. // women w = new women();
  12. // zjr z = new zjr();
  13. person[] per = new person[10];
  14. Random r = new Random();
  15. for (int i = 0; i < per.Length; i++)
  16. {
  17. int ran = r.Next(1, 6);
  18. switch (ran)
  19. {
  20. case 1:
  21. per[i] = new person();
  22. break;
  23. case 2:
  24. per[i] = new student();
  25. break;
  26. case 3:
  27. per[i] = new man();
  28. break;
  29. case 4:
  30. per[i] = new women();
  31. break;
  32. case 5:
  33. per[i] = new zjr();
  34. break;
  35. }
  36. }
  37. for (int i = 0; i < per.Length; i++)
  38. {
  39. if (per[i] is student)
  40. {
  41. ((student)per[i]).studentdoing();
  42. }
  43. else if (per[i] is man)
  44. {
  45. ((man)per[i]).mandoing();
  46. }
  47. else if (per[i] is women)
  48. {
  49. ((women)per[i]).womendoing();
  50. }
  51. else if (per[i] is zjr)
  52. {
  53. ((zjr)per[i]).zjrdoing();
  54. }
  55. else
  56. per[i].persondoing();
  57. }
  58. }
  59. }
  60. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/638015
推荐阅读
相关标签
  

闽ICP备14008679号