public object UnityEnghine { get; private set; }在C#中什么意思
时间: 2024-04-19 09:27:28 浏览: 95
在C#中,这行代码定义了一个名为 UnityEnghine 的属性,其类型为 object。这个属性具有一个公共的 getter 方法和一个私有的 setter 方法。换句话说,外部代码可以读取 UnityEnghine 属性的值,但只有类内部的代码可以修改它的值。这种访问控制方式通常用于限制对属性的写入权限,以确保只有类内部的方法可以修改属性的值。
相关问题
Use c # to complete the following code,Creating a Car class A class is a self-contained piece of software that is able to function on it own. We will model a car in software. Car Class Properties Year : int «private set» Manufacturer : string «private set» Model : string «private set» IsDrivable : bool «private set» Price : double «private set» Methods«constructor» Car(year : int, manufacturer : string, model : string, price : double, isDrivable: bool = true) PrintInfo() : void Description of field members: 1. year: this private member represents the year value of this object 2. manufacturer: this private member represents the manufacturer of this car 3. model: this private member represents the model of this car 4. isDrivable: this private member indicates if this car be driven 5. price: this private member represents the selling price of this car Constructor: 1. public Car(int year, string manufacturer, string model, double price, bool isDrivable = true): this is the constructor of this class. It takes five arguments and assigns them to the appropriate fields. You will assume that all the arguments will be sane (i.e. the values will not break your program). Apology: UML and some newer languages specify the type after the identifier. C# on the other hand requires that you specify the type and then the identifier. To restrict access fields are normally decorated with the private modifier. Programming II Car: fields, constructor, ToString() n.k.p Page 2 of 2 Description of action member: 1. public void PrintInfo(): this method does not take any argument but it will print all information about this object. You get to decide how the output will look like. It is expected that all the values be displayed. In your main method write the code to do the following: 1. Create at least four cars and print them. Remember to call the constructor with 4 or 5 parameters.
Here's the code to create a Car class in C#:
```
using System;
class Car
{
private int year;
private string manufacturer;
private string model;
private bool isDrivable;
private double price;
public Car(int year, string manufacturer, string model, double price, bool isDrivable = true)
{
this.year = year;
this.manufacturer = manufacturer;
this.model = model;
this.price = price;
this.isDrivable = isDrivable;
}
public void PrintInfo()
{
Console.WriteLine("Year: " + year);
Console.WriteLine("Manufacturer: " + manufacturer);
Console.WriteLine("Model: " + model);
Console.WriteLine("Is Drivable: " + isDrivable);
Console.WriteLine("Price: " + price);
}
}
```
In your main method, you can create four Car objects and print their information like this:
```
static void Main(string[] args)
{
Car car1 = new Car(2021, "Toyota", "Camry", 25000.0);
Car car2 = new Car(2019, "Honda", "Civic", 20000.0, false);
Car car3 = new Car(2018, "Ford", "Mustang", 35000.0);
Car car4 = new Car(2020, "Tesla", "Model S", 80000.0);
car1.PrintInfo();
car2.PrintInfo();
car3.PrintInfo();
car4.PrintInfo();
}
```
This will output the information for all four cars.
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 窗体应用程序,包含了一个输入学生信息的表单,用户在填写完信息后,点击提交按钮,会将学生信息显示在一个文本框中,并将输入框设置为只读状态,不允许再次编辑。
阅读全文