搜索: 标题内容作者  
  首页C#教程C#基础
背景:
阅读新闻

C#中的字段与属性

[日期:2008-09-23]   来源:互联网整理  作者:佚名   [字体: ]
    新闻简介: using System;
using System.Collections.Generic;
using System.Text;
namespace Example11_1 {
class Program {
static void Main(string[] args) {
Farmer farmer = new Farmer();
farmer.Name = "Liu";
farmer.Age = 226;
Console.WriteLine(farmer.Age);
Co
        关 键 词:  
using System;
using System.Collections.Generic;
using System.Text;

namespace Example11_1 {
class Program {
static void Main(string[] args) {
Farmer farmer = new Farmer();
farmer.Name = "Liu";
farmer.Age = 226;

Console.WriteLine(farmer.Age);

Console.ReadLine();
}
}

class Farmer {
/// <summary>
/// Farmer类的无参数构造函数
/// </summary>
public Farmer() {
}

/// <summary>
/// Farmer类的构造函数
/// </summary>
/// <param name="m_Name">Farmer的姓名参数</param>
public Farmer(string m_Name) {
name = m_Name;
}

/// <summary>
/// 姓名字段
/// </summary>
string name = string.Empty;

/// <summary>
/// max字段
/// </summary>
const int max = 150;

/// <summary>
/// min字段
/// </summary>
const int min = 0;

/// <summary>
/// 年龄字段
/// </summary>
int age = 0;

/// <summary>
/// Max属性
/// </summary>
public int Max {
get {
return max;
}
}

/// <summary>
/// Min属性
/// </summary>
public int Min
{
get
{
return min;
}
}

/// <summary>
/// Name属性
/// </summary>
public string Name {
set {
name = value;
}
}

/// <summary>
/// 年龄属性
/// </summary>
public int Age {
get {
return age;
}
set {
if ((value > min) && (value < max))
{
age = value;
}
else
{
try
{
Exception ex = new Exception("设置的值超出预设范围!");
throw (ex);
}
catch
{

Console.WriteLine("设置的值超出预设范围!");

}
}
}
}
}

}

If you believe an article violates your rights or the rights of others, please contact us.

收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻      
友情链接
本文评论   [发表评论] 全部评论 (0)
赞助商广告
热门评论