C# get set方法

时间:2021-10-27 12:23:41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GetSetTest
{
class A {
private String username = "username";
private String password; public String Username
{
get
{
return username;
}
} public String Password
{
get
{
return password;
}
set
{
this.password = value;
}
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
String username = a.Username;
//a.Username = "11";//出错,因为没有提供set方法
Console.WriteLine(username);
//可以改变password
a.Password = "";
Console.WriteLine(a.Password);
Console.Read();
}
}
}