C#只读属性

2021-01-25 09:16

阅读:392

标签:collect   只读   rri   namespace   pre   ext   first   面向对象   span   

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

C#只读属性

标签:collect   只读   rri   namespace   pre   ext   first   面向对象   span   

原文地址:https://www.cnblogs.com/jestin/p/12024826.html


评论


亲,登录后才可以留言!