get,set总结

在许多面向对象程序设计语言中,属性 (property) 是指对象的特征和状态,具体地说就是指对象的数据成员。程序员可以指定数据成员能否被外界直接访问,如果数据成员被指定为 public 的,外界就可以用 对象名 . 公有数据成员名 访问该成员。 c# 是完全面向对象的语言, c# 倡导一种新途径,对数据成员能够更好地封装和保护,同时又向外界提供更有效的访问形式。 c# 中用来达到这个目标的就是 属性 ,而那些数据成员,在 c# 中称为 字段


  属性的定义和使用
  属性由两个部分组成:属性头和存储器。存储器分为get访问器和set访问器。声明属性的一般形式为:

修饰符  学校名称不可修改
        private string stuName = "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">阿会楠";
        
private int stuAge = 22;
        
public string studentName
        {
            
get { return stuName; }
            
set { stuName = value; }
        }
        
public int studentAge
        {
            
get { return stuAge; }
            
set { stuAge = value; }
        }
        
public string studentCollege
        {
            
get { return stuCollege; }
        }
        
public string studentInfo
        {
            
get { return "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">学校:" + stuCollege + "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">名字:" + stuName + "<span courier="" new";="" mso-hansi-font-family:"courier="" new";mso-bidi-font-family:"courier="" color:maroon;mso-font-kerning:0pt;"="" style="line-height: 21px; font-size: 9pt; font-family: 宋体; ">岁数:" + stuAge;}
        }
    }

    class Program
    {
        
static void Main(string[] args)
        {
            Student stu = 
new Student();
            Console.Write(stu.studentCollege + 
"\n");
            stu.studentAge = 
25;
            Console.Write(stu.studentInfo);
            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值