C#語法學習(屬性[property])
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 08:29
* 屬性(property)
* 屬性是類和結構,接口的命名成員.
* 它們提供了通過訪問器(accessor)讀,
* 寫或計算私有字段值的靈活機制.
*
* 屬性是這樣的成員:
* 它們扯供靈活的機制來讀取,編寫或是計算私有字段的
* 值.可以像使用公共數據成員一樣使用屬性,但實際上
* 它們是稱為"訪問器"的特殊方法.這使得數據在可被輕
* 鬆訪問的同時.仍能提供方法的安全性和靈活性.
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string name;
private string sex;
public void SetName(string values)
{
name=values;
}
public string GetName()
{
return name;
}
public void SetSex(string values)
{
if(values=="男" || values=="女")
{
sex=values;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
public string GetSex()
{
return sex;
}
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Sex
{
get
{
return sex;
}
set
{
if(value=="男" || value=="女")
{
sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.SetName("張三");
zs.SetSex("男");
Console.WriteLine("姓名:"+zs.GetName()+" 性別:"+zs.GetSex());
zs.Name="李四";
zs.Sex="牛";
Console.WriteLine("姓名:"+zs.Name+" 性別:"+zs.Sex);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 08:29
* 屬性(property)
* 屬性是類和結構,接口的命名成員.
* 它們提供了通過訪問器(accessor)讀,
* 寫或計算私有字段值的靈活機制.
*
* 屬性是這樣的成員:
* 它們扯供靈活的機制來讀取,編寫或是計算私有字段的
* 值.可以像使用公共數據成員一樣使用屬性,但實際上
* 它們是稱為"訪問器"的特殊方法.這使得數據在可被輕
* 鬆訪問的同時.仍能提供方法的安全性和靈活性.
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string name;
private string sex;
public void SetName(string values)
{
name=values;
}
public string GetName()
{
return name;
}
public void SetSex(string values)
{
if(values=="男" || values=="女")
{
sex=values;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
public string GetSex()
{
return sex;
}
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public string Sex
{
get
{
return sex;
}
set
{
if(value=="男" || value=="女")
{
sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.SetName("張三");
zs.SetSex("男");
Console.WriteLine("姓名:"+zs.GetName()+" 性別:"+zs.GetSex());
zs.Name="李四";
zs.Sex="牛";
Console.WriteLine("姓名:"+zs.Name+" 性別:"+zs.Sex);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:03
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string _name;
private string _sex;
public string name
{
get
{
return _name;
}
set
{
_name=value;
}
}
public string sex
{
get
{
return _sex;
}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="牛";
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:03
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string _name;
private string _sex;
public string name
{
get
{
return _name;
}
set
{
_name=value;
}
}
public string sex
{
get
{
return _sex;
}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="牛";
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:03
* 屬性的四個种類:
* 1,可讀寫(read/write)屬性:需實現get/set訪問器
* 2,只讀(readonly)屬性:需實現get訪問器
* 3,只寫(writeonly)屬性:需實現set訪問器
* 4,static屬性:只能訪問靜態數據
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string _name;
private string _sex;
public string name
{
get{return _name;}//讀取
set{_name=value;}//寫入
}
public string sex
{
get{return _sex;}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="牛";
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:03
* 屬性的四個种類:
* 1,可讀寫(read/write)屬性:需實現get/set訪問器
* 2,只讀(readonly)屬性:需實現get訪問器
* 3,只寫(writeonly)屬性:需實現set訪問器
* 4,static屬性:只能訪問靜態數據
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class User
{
private string _name;
private string _sex;
public string name
{
get{return _name;}//讀取
set{_name=value;}//寫入
}
public string sex
{
get{return _sex;}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="牛";
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class User
{
private string _name;
private string _sex;
private DateTime _Birthday;
public DateTime Birthday//只寫屬性
{
set
{
if(value<Convert.ToDateTime("1900-1-1") || value.Year>DateTime.Now.Year-3)
MessageBox.Show("年齡不合法");
else
_Birthday=value;
}
}
public int Age//只讀屬性
{
get
{
return DateTime.Now.Year-_Birthday.Year;
}
}
public string name//讀寫屬性
{
get{return _name;}//讀取
set{_name=value;}//寫入
}
public string sex//讀寫屬性
{
get{return _sex;}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="男";
zs.Birthday=Convert.ToDateTime("1911-1-1");
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex + " 年齡:" + zs.Age);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class User
{
private string _name;
private string _sex;
private DateTime _Birthday;
public DateTime Birthday//只寫屬性
{
set
{
if(value<Convert.ToDateTime("1900-1-1") || value.Year>DateTime.Now.Year-3)
MessageBox.Show("年齡不合法");
else
_Birthday=value;
}
}
public int Age//只讀屬性
{
get
{
return DateTime.Now.Year-_Birthday.Year;
}
}
public string name//讀寫屬性
{
get{return _name;}//讀取
set{_name=value;}//寫入
}
public string sex//讀寫屬性
{
get{return _sex;}
set
{
if(value=="男" || value=="女")
{
_sex=value;
}
else
{
Console.WriteLine("性別只能為男或是女");
}
}
}
}
class Property
{
static void Main()
{
User zs=new User();
zs.name="李四";
zs.sex="男";
zs.Birthday=Convert.ToDateTime("1911-1-1");
Console.WriteLine("姓名:"+zs.name+" 性別:"+zs.sex + " 年齡:" + zs.Age);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class User
{
private static int _LoginCount;//統計訪問者人數
public User()
{
_LoginCount++;
}
public static int LoginCount//靜態只讀屬性,只能訪問靜態成員
{
get
{
return _LoginCount;
}
}
}
class Property
{
static void Main()
{
User zs=new User();
User ls=new User();
User ww=new User();
User ml=new User();
Console.WriteLine(User.LoginCount);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 09:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class User
{
private static int _LoginCount;//統計訪問者人數
public User()
{
_LoginCount++;
}
public static int LoginCount//靜態只讀屬性,只能訪問靜態成員
{
get
{
return _LoginCount;
}
}
}
class Property
{
static void Main()
{
User zs=new User();
User ls=new User();
User ww=new User();
User ml=new User();
Console.WriteLine(User.LoginCount);
}
}
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。