构造函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _Test { class Program { static void Main( string [] args) { //Student s = new Student("张三", 100, 100, 100); //Student zsStudent = new Student("张三", 18, '男', 100, 100, 100); //zsStudent.SayHello(); //zsStudent.ShowScore(); //Console.ReadKey(); // Student xlStudent = new Student("小兰",78,79,89); Student xlStudent = new Student( "小兰" ,19, '女' ); xlStudent.SayHello(); xlStudent.ShowScore(); Console.ReadKey(); } } public class Student { //字段、属性、方法、构造函数 //析构函数 构造函数 //当程序结束的时候 析构函数才执行 //帮助我们释放资源 //GC垃圾回收器一般自动调用,极少有手动调用的。 //~Student() //{ // Console.WriteLine("我是析构函数"); //} //构造函数 public Student( string name, int age, char gender, int chinese, int math, int english) { this .Name = name; this .Age = age; this .Gender = gender; this .Chinese = chinese; this .Math = math; this .English = english; } public Student( string name, int chinese, int math, int english) : this (name, 0, 'c' , chinese, math, english) { //使用this后下面代码可省了。 //this.Name = name; //this.Chinese = chinese; //this.Math = math; //this.English = english; } //构造函数重载 public Student( string name, int age, char gender) { this .Name = name; if (age < 0 || age > 100) { age = 0; } this .Age = age; this .Gender = gender; } //当我们不主动使用构造函数时,系统会自动生成如下构造函数 public Student() { } //私有字段 private string _name; //共有属性 public string Name { //get控制是否可读 get { return _name; } //set控制是否可写 set { _name = value; } } private int _age; public int Age { get { return _age; } set { if (value < 0 || value > 100) { value = 0; } _age = value; } } private char _gender; public char Gender { get { if (_gender != '男' && _gender != '女' ) { return _gender = '男' ; } return _gender; } set { _gender = value; } } private int _chinese; public int Chinese { get { return _chinese; } set { _chinese = value; } } private int _math; public int Math { get { return _math; } set { _math = value; } } private int _english; public int English { get { return _english; } set { _english = value; } } public void SayHello() { Console.WriteLine( "我叫{0},我几年{1}岁了,我是{2}生" , this .Name, this .Age, this .Gender); } public void ShowScore() { Console.WriteLine( "我叫{0},我的总成绩是{1},平均成绩是{2}" , this .Name, this .Chinese + this .Math + this .English, ( this .Chinese + this .Math + this .English) / 3); } } } |
树立目标,保持活力,gogogo!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具