黑马程序员_c#面向对象基础:属性
来源:互联网 发布:人工智能股票龙头科大 编辑:程序博客网 时间:2024/05/16 15:23
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
属性:
惯用法:属性开头字母要大写,字段开头字母要小写.
例如:
class Person
{
private int age; //字段private int Age //属性mz
{
get { return age; }
set { age = value; }
}public void SayHello() //方法
{
Console.WriteLine("我的年龄是:{0}",Age);
}
}
- 只用set,或者只用get就可以定义只读或者只写属性(只写的不常见).
- 可以为set或get设置访问级别.
- 例子,限制非法值的设置.
- (.net3.x)简化了set,get.:public int Age{ set;get},适合于set,get中没有特殊逻辑代码的情况.
- 字段和属性的区别是:属性看似字段不是字段,可以进行非法值控制,可以设置只读.
属性练习:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyTryParse
{
class Program
{
public static void Main(string[] args)
{
Person person1 = new Person();
person1.Age = 30;
person1.Name = "张三";
Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",person1.Name,person1.Age);
Person person2 = new Person();
person2.Age = 26;
person2.Name = "李四";
Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",person2.Name, person2.Age);
Person person3 = new Person();
person3.Age = 28;
person3.Name = "王五";
Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",person3.Name, person3.Age);
PersonCheck personchk =new PersonCheck();
personchk.Age = -1; //此处赋了一个非法的值,返回值为0;
personchk.Name = "马六";
Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",personchk.Name, personchk.Age);
PersonNew personnew =new PersonNew();
personnew.Age = 19;
personnew.Name = "钱七";
Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",personnew.Name, personnew.Age);
PersonReadOnly personread =new PersonReadOnly();
personread.IncAge();
personread.IncAge();
Console.WriteLine("我今年{0}岁了.", personread.Age);
Console.ReadKey();
}
}
class Person
{
private int age; //字段
private string name;
public int Age //属性
{
set { this.age = value; } //取值
get { return this.age; } //赋值
}
public string Name
{
set { this.name = value; }
get { return this.name; }
}
}
class PersonCheck
{
private int age;
private string name;
public int Age //Age 没有保存数据,都是保存在age中.
{
set
{
if (value < 0 ||value > 100) //属性可以对非法值进行判断
{
return;
}
else
{
this.age = value; //value 代表用户赋过来的值
}
}
get
{
return this.age;
}
}
public string Name
{
set
{
this.name = value;
}
get
{
return this.name;
}
}
}
//.net3.x属性新特性没有代码块
class PersonNew
{
public int Age
{
set;
get;
}
public string Name
{
set;
get;
}
}
//一个不常见的例子
class PersonX
{
public int Age
{
set
{
}
get
{
return 4; // 属性本身不保存值,无论赋的值是多少,都将返Ì回4.
}
}
}
//一个错误的赋值方式
class PersonErroe
{
private int age;
private string name;
public int Age
{
set
{
this.Age = value; //这样赋值将进入一个死循环,自己调用自己.
}
get
{
return this.Age;
}
}
}
// 一个只读的属性.
class PersonReadOnly
{
private int age;
public int Age
{
get { return age; }
}
public void IncAge()
{
age++;
}
}
}
此代码运行结果为:
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima/
- 黑马程序员_c#面向对象基础:属性
- 黑马程序员_C#面向对象(概念,访问级别,属性)
- 黑马程序员_c#面向对象基础:聊天机器人
- 黑马程序员_C#面向对象学习笔记
- 黑马程序员_C#面向对象总结
- 黑马程序员_C#面向对象总结
- 黑马程序员_C#面向对象学习知识总结1
- 黑马程序员_C#面向对象学习知识总结2
- 黑马程序员_C#面向对象知识总结3
- 黑马程序员_C#基础总结(面向过程)
- 黑马程序员--面向对象基础
- 黑马程序员-------面向对象基础
- 黑马程序员_C#编程基础
- 黑马程序员_C#编程基础
- 黑马程序员_C#基础回顾
- 黑马程序员_C#基础1
- 黑马程序员_C#基础2
- 黑马程序员_C#基础3
- 李开复:算法的力量
- asp.net<Web版> ---将excel表数据导入到数据库问题<一>---未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序
- 确定比赛名次
- 第三篇 菜单、图标与光标的使用
- java的getClass()函数
- 黑马程序员_c#面向对象基础:属性
- 用p6spy完整显示hibernate的SQL语句_转
- [Leetcode] Combination Sum II
- http mock service
- 面向对象
- 解决OpenCv中没有找到tbb_debug.dll的方法+OpenCv项目环境配置
- java_选择排序
- java中replaceAll和split的反斜杠("\")问题
- priority_queue的用法总结