所有类里的变量都要写成private,不要写成public;

    class Person
    {
        public int Height;
        public int Age;
        private string Name;
        //Name设置成私有的,这样外界访问的时候,只能通过下面的GiveName来进行赋值,并且还不能取值,这样就有一定的安全性;可以对外界赋的值进行决断;
        public void GiveName(string names)
        {
            if (names == "jerry")
            {
                return;
            }
            else
            {
                this.Name = names;
            }
        }
    }
View Code

相关文章:

  • 2022-03-01
  • 2021-09-23
  • 2022-01-07
  • 2021-10-12
  • 2021-12-09
  • 2022-01-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-04
  • 2021-06-25
  • 2021-05-20
  • 2021-12-17
  • 2021-08-18
  • 2022-12-23
  • 2021-09-21
相关资源
相似解决方案