首页 > 代码库 > C# get set方法

C# get set方法

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 = "123";
            Console.WriteLine(a.Password);
            Console.Read();
        }
    }
}