注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

C# Properties  

2014-06-14 17:12:42|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

    Properties are named members of classes, structures and interfaces.

    Member variables or methods in a class or structures are called Fields. Properties are an extension of fields and are accessed using the same syntax. They use accessors through which the values of the private fields can be read, written or manipulated.

    Properties do not name the storage locations. Instead, they have accessors that read, write, or compute their values.


    属性:是类、结构和接口中被命名的成员,这里的“成员”包括成员变量和成员函数。

    类和结构中的成员变量和成员函数被称为,属性是域概念的扩展,这些属性由相同的语法进行访问。这些私有域的值可以通过“访问器”进行读、写、操作。

    属性不命名存储位置,但它们有访问器对变量进行读、写并计算其值。

using System;

class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
public string Code
{
set
{
code = value;
}
get
{
return code;
}
}
public string Name
{
set
{
name = value;
}
get
{
return name;
}
}
public int Age
{
set
{
age = value;
}
get
{
return age;
}
}
public static void Main()
{
Student s = new Student();

s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student Info: " + "Code = " + s.Code

+ ", Name = " + s.Name + ", Age = " + s.Age);
s.Age += 1;
Console.WriteLine("Student Info: " + "Code = " + s.Code

+ ", Name = " + s.Name + ", Age = " + s.Age);
}
}

上述示例程序实际是下面程序简化:

using System;
class Student
{
private string code = "N.A";
private string name = "not known";
private int age = 0;
public void SetCode(string s)
{
code = s;
}
public string GetCode()
{
return code;
}
public void SetName(string s)
{
name = s;
}
public string GetName()
{
return name;
}
public void SetAge(int a)
{
age = a;
}
public int GetAge()
{
return age;
}
public void AgeRise()
{
age += 1;
}
public static void Main()
{
Student s = new Student();
s.SetCode("001");
s.SetName("Zara");
s.SetAge(9);

Console.WriteLine("Student Code = " + s.GetCode()
+ ", Name = " + s.GetName()
+ ", Age = " + s.GetAge());

s.AgeRise();

Console.WriteLine("Student Code = " + s.GetCode()
+ ", Name = " + s.GetName()
+ ", Age = " + s.GetAge());
}
}

  评论这张
 
阅读(81)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017