C#只读属性

using System;
using System.Collections.Generic;
using System.Text;

namespace 面向对象
{
    class Person
    {
        //属性不可更改
        public string FirstName { get;  }
        public string LastName { get;  }
        //属性可更改
        //public string FirstName { get; set; }
        //public string LastName { get; set; }

        public Person(string first, string last)
        {
            FirstName = first;
            LastName = last;
        }

        public override string ToString()
        {
            return FirstName + " " + LastName;
        }

        public string AllCaps()
        {
          //属性不可更改
            return ToString().ToUpper();
            //属性可更改
            //FirstName = FirstName.ToUpper();
            //LastName = LastName.ToUpper();
            //return ToString();
        }
    }

    public class Statr
    {
        public static void Main()
        {
            var p = new Person("Bill", "Wagner");
            Console.WriteLine("The name, in all caps: " + p.AllCaps());
            Console.WriteLine("The name: " + p);
        }
    }
}
posted @ 2019-12-11 20:01  王者2  阅读(2630)  评论(0编辑  收藏  举报