c#属性详解(写一个文章来回顾下知识点)
C#属性是对类中的字段(fields)的保护,像访问字段一样来访问属性。同时 也就封装了类的内部数据
using System;
using System.Collections.Generic;
using System.Text;

namespace Example_1
{
class age
{
private int _age;
public void SetAge(int a)
{
if (a < 0)
{
Console.WriteLine("must an +int");
}
else
{
_age = a;
}
}

public int GetAge()
{
return _age;
}
}

class TestAge
{
[STAThread]
static void main(string[] args)
{
age Age = new age();
Console.WriteLine("请输入年龄:");
Age.SetAge(Int32.Parse( Console.ReadLine()));
int a = Age.GetAge();
Console.WriteLine("{0}",a);
}
}
}
上面的例子是假如没有属性时候 ,我们限制对字段的访问。
感觉起来它很烦琐,因为每次访问的时候 1 个field就需调用2个方法。有了属性就不用这样麻烦了。
属性的类型:1.读写属性 2.只读属性 3只写属性(他的诞生是错误的) 4.静态属性
属性的声明:【访问符号】(static) 数据类型 属性名
{
set{};
get{};
}
属性的特点:每当赋值运算的时候自动调用set访问器,其他时候则调用get访问器。 以帕斯卡命名 不能冠以Get Set
注意:静态属性是通过类名调用的哦 下面的一个例子里面具体有展示 请仔细阅读
using System;

namespace Example_1
{
class SavingsAccount
{
//用于存储帐户号码、余额和已获利息的类字段。
private int _accountNumber;
private double _balance;
private double _interestEarned;
// 利率是静态的,因为所有的帐户都使用相同的利率
private static double _interestRate;

// 构造函数初始化类成员
public SavingsAccount(int accountNumber, double balance)
{
this._accountNumber = accountNumber;
this._balance = balance;
}

// AccountNumber只读属性
public int AccountNumber
{
get
{
return _accountNumber;
}
}

// Balance 只读属性
public double Balance
{
get
{
if (_balance < 0)
Console.WriteLine("无余额");
return _balance;
}
}

// InterestEarned 读/写属性
public double InterestEarned
{
get
{
return _interestEarned;
}

set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestEarned 不能为负数");
return;
}
_interestEarned = value;
}
}

// InterestRate 读/写属性为静态,
// 因为所有特定类型的帐户都具有相同的利率
public static double InterestRate
{
get
{
return _interestRate;
}

set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestRate 不能为负数");
return;
}
else
{
_interestRate = value / 100;
}
}
}
}

class TestSavingsAccount
{
/// <摘要>
/// 应用程序的主入口点。
/// </摘要>
[STAThread]
static void Main(string[] args)
{
// 创建 SavingsAccount 的对象
SavingsAccount objSavingsAccount = new SavingsAccount(12345, 5000);;
// 用户交互
Console.WriteLine("输入到现在为止已获得的利息和利率");
objSavingsAccount.InterestEarned = Int64.Parse(Console.ReadLine());
SavingsAccount.InterestRate = Int64.Parse(Console.ReadLine());
// 使用类名访问静态属性
objSavingsAccount.InterestEarned += objSavingsAccount.Balance * SavingsAccount.InterestRate;
Console.WriteLine("获得的总利息为: {0}", objSavingsAccount.InterestEarned);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Example_1
{
class age
{
private int _age;
public void SetAge(int a)
{
if (a < 0)
{
Console.WriteLine("must an +int");
}
else
{
_age = a;
}
}
public int GetAge()
{
return _age;
}
}
class TestAge
{
[STAThread]
static void main(string[] args)
{
age Age = new age();
Console.WriteLine("请输入年龄:");
Age.SetAge(Int32.Parse( Console.ReadLine()));
int a = Age.GetAge();
Console.WriteLine("{0}",a);
}
}
}
感觉起来它很烦琐,因为每次访问的时候 1 个field就需调用2个方法。有了属性就不用这样麻烦了。
属性的类型:1.读写属性 2.只读属性 3只写属性(他的诞生是错误的) 4.静态属性
属性的声明:【访问符号】(static) 数据类型 属性名
{
set{};
get{};
}
属性的特点:每当赋值运算的时候自动调用set访问器,其他时候则调用get访问器。 以帕斯卡命名 不能冠以Get Set
注意:静态属性是通过类名调用的哦 下面的一个例子里面具体有展示 请仔细阅读
using System;
namespace Example_1
{
class SavingsAccount
{
//用于存储帐户号码、余额和已获利息的类字段。
private int _accountNumber;
private double _balance;
private double _interestEarned;
// 利率是静态的,因为所有的帐户都使用相同的利率
private static double _interestRate;
// 构造函数初始化类成员
public SavingsAccount(int accountNumber, double balance)
{
this._accountNumber = accountNumber;
this._balance = balance;
}
// AccountNumber只读属性
public int AccountNumber
{
get
{
return _accountNumber;
}
}
// Balance 只读属性
public double Balance
{
get
{
if (_balance < 0)
Console.WriteLine("无余额");
return _balance;
}
}
// InterestEarned 读/写属性
public double InterestEarned
{
get
{
return _interestEarned;
}
set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestEarned 不能为负数");
return;
}
_interestEarned = value;
}
}
// InterestRate 读/写属性为静态,
// 因为所有特定类型的帐户都具有相同的利率
public static double InterestRate
{
get
{
return _interestRate;
}
set
{
// 验证数据
if (value < 0.0)
{
Console.WriteLine("InterestRate 不能为负数");
return;
}
else
{
_interestRate = value / 100;
}
}
}
}

class TestSavingsAccount
{
/// <摘要>
/// 应用程序的主入口点。
/// </摘要>
[STAThread]
static void Main(string[] args)
{
// 创建 SavingsAccount 的对象
SavingsAccount objSavingsAccount = new SavingsAccount(12345, 5000);;
// 用户交互
Console.WriteLine("输入到现在为止已获得的利息和利率");
objSavingsAccount.InterestEarned = Int64.Parse(Console.ReadLine());
SavingsAccount.InterestRate = Int64.Parse(Console.ReadLine());
// 使用类名访问静态属性
objSavingsAccount.InterestEarned += objSavingsAccount.Balance * SavingsAccount.InterestRate;
Console.WriteLine("获得的总利息为: {0}", objSavingsAccount.InterestEarned);
}
}
}


浙公网安备 33010602011771号