当前位置: 首页 > news >正文

C# 面对对象基础 枚举,Enum.TryParse的使用

代码:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace Student_c_
{enum Week : int{Mon,Tus,Wed,Thu,Fri,Sat,Sun,}public class Student{public static string Course = "面向对象程序设计!";public static string Where = "G2-407";public string Name { get; set; }public string ClassName { get; set; }private int Id;// 班级的最大容量,这是一个常量  public const int MaxNum = 39;public bool flag = true;// 学生的ID  public int id{get{return Id;}set{if(value >MaxNum||value <0){Console.WriteLine("学号输入错误");flag = !flag;}else{Id = value;}}}// 构造函数  public Student( int id,string name, string className){this.id=id;Name = name;ClassName = className;}internal class Program{public static void GotoClass(Student stu){if(stu.flag){Console.WriteLine("学号是:{2} 班级是:{0} 姓名是:{1}", stu.ClassName, stu.Name, stu.Id);}else{Console.WriteLine(" 班级是:{0} 姓名是:{1}", stu.ClassName, stu.Name);}}static void Main(string[] args){string a, b;int c;string day;Console.WriteLine("输入学号:");c = int.Parse(Console.ReadLine());Console.WriteLine("输入班级:");a = Console.ReadLine();Console.WriteLine("输入姓名:");b = Console.ReadLine();Console.WriteLine("今天是星期几:");day = Console.ReadLine();Console.WriteLine("\n");//   Week worday=(Week)Enum.Parse(typeof(Week), day);if (Enum.TryParse<Week>(day, true, out Week workday))//{if (workday == Week.Sat||workday==Week.Sun){Console.WriteLine("Today is weekend!!!");}else{Console.WriteLine("Today is {0}", day);}}Student stu = new Student(c, b, a);GotoClass(stu);Console.WriteLine($"课程是:{Course} 教室是:{Where}");Console.ReadLine();}}}
}

注意:这里的对应关系


http://www.mrgr.cn/news/36174.html

相关文章:

  • 共享单车轨迹数据分析:以厦门市共享单车数据为例(七)
  • ②无需编程 独立通道 Modbus主站EtherNet/IP转ModbusRTU/ASCII工业EIP网关串口服务器
  • 使用C计算数码管段码
  • 单词搜索问题(涉及递归等)
  • 【Linux学习】1-2 新建虚拟机ubuntu环境
  • C++ 机器人相关面试点
  • PyTorch框架安装
  • 嵌入式QT学习
  • 【HDU-2669 Romantic】
  • python里面的单引号和双引号的区别
  • 【学习笔记】UWB技术定位原理
  • 【Java 问题】基础——面相对象
  • 浅克隆与深克隆
  • 【测试项目】——个人博客系统自动化测试
  • 脏读查询SQL SELECT查询配置(DM8:达梦数据库)
  • 第L2周:机器学习|线性回归模型 LinearRegression:1. 简单线性回归模型
  • QT创建线程,QT多线程的创建和使用,QT线程池
  • Kolmogorov-Arnold——代替 MLP以提高模型的代表性和性能
  • 替换jar包中class文件
  • 去年营收增速仅1.4%,成长性恐不足,这家批发业公司终止了