解释一下这行代码public string Name { get => _name; set => _name = value; }\n
时间: 2023-06-03 13:04:38 浏览: 53
这行是 C# 的属性,其中 _name 是一个私有字段,通过 get 和 set 访问器(accessor)公开了该字段。当调用 Name 属性的 get 访问器时,_name 的值将被返回;当调用 set 访问器时,_name 的值将被设置为传入的 value。
相关问题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WinFormsApp16 { public class student { private int stuno; private string stuname; private int stuage; private string stusex; private string stuspeciality; public student(int myno ,string myname, int myage ,string mysex,string myspecility) { this.Stuno = myno; this.Stuname = myname; this.Stuage = myage; this.Stusex = mysex; this.Stuspeciality = myspecility; } public int Stuno { get => stuno; } public string Stuname { get => stuname; } public string Stusex { get => stusex; } public string Stuspeciality { get => stuspeciality; set => stuspeciality = value; } public int Stuage { get => stuage; set => stuage = value; } } }把这段代码优化成输入信息后再只读
这段代码是 C# 语言的代码,可以通过在 Windows 系统上安装 Visual Studio 编辑器进行修改和运行。
以下是将该代码优化为输入信息后再只读的代码示例:
```csharp
using System;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class Form1 : Form
{
private int stuno;
private string stuname;
private int stuage;
private string stusex;
private string stuspeciality;
public Form1()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
stuno = int.Parse(txtStuno.Text);
stuname = txtStuname.Text;
stuage = int.Parse(txtStuage.Text);
stusex = txtStusex.Text;
stuspeciality = txtStuspeciality.Text;
// 显示信息
txtInfo.Text = $"学号:{stuno}\r\n姓名:{stuname}\r\n年龄:{stuage}\r\n性别:{stusex}\r\n专业:{stuspeciality}";
// 设置文本框只读
txtStuno.ReadOnly = true;
txtStuname.ReadOnly = true;
txtStuage.ReadOnly = true;
txtStusex.ReadOnly = true;
txtStuspeciality.ReadOnly = true;
}
}
}
```
这段代码实现了一个简单的 Windows 窗体应用程序,包含了一个输入学生信息的表单,用户在填写完信息后,点击提交按钮,会将学生信息显示在一个文本框中,并将输入框设置为只读状态,不允许再次编辑。
将代码补充完整using System; using System.Collections.Generic; using System.Text; namespace one { /// <summary> /// 学生类 /// </summary> class Student { private string _code;//学号 private string _name;//姓名 public Student() { } public Student(string Code, string Name) { _code = Code; _name = Name; } public string code { get { return _code; } set { _code = value; } } public string Name { get { return _name; } set { _name = value; } } } }
以上是一个C#中的学生类,包含学号和姓名两个属性。下面是将其使用的代码补充完整:
using System;
using System.Collections.Generic;
using System.Text;
namespace one
{
class Program
{
static void Main(string[] args)
{
Student s = new Student("001", "小明");
Console.WriteLine("学号:" + s.code + ",姓名:" + s.Name);
Console.ReadLine();
}
}
}
在Main函数里,我们创建了一个学生对象s,并且将学号和姓名传入构造函数中初始化。最后使用Console.WriteLine方法打印出学号和姓名信息。运行程序后,可以在控制台窗口中看到输出的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)