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();
        }
    }
}

 

posted @ 2014-05-09 17:00  无忧之路  阅读(1200)  评论(0编辑  收藏  举报
无忧之路