属性是代表类的实例或者类中的一个数据项的成员。
属性是字段和方法的交集,指的是一组两个匹配的访问器方法。
下面是属性的基本形式
public 返回类型 标识符{
set 访问器为属性赋值;
get 访问器为属性获取值;
}
属性包含两个代码块,分别以get和set关键字开头。
set访问器和get访问器的特点如下:
public 这样的修饰符是可选的,可以选择私有的,这样就不可以在类外直接访问 set 和 get 访问器
通常将类中的字段声明private以封装该字段, 然后声明一个public的属性来控制从类的外部对该字段的访问。
与属性关联的字段称为后备字段。
一般习惯把私有方法和字段以小写字母开头,公共方法和字段以大写字母开头。
使用属性和使用字段是一样的语法
using System;
namespace PropertyDemo
{
internal class Program
{
static void Main(string[] args)
{
PropertyDemo propertyDemo = new PropertyDemo();
propertyDemo.Name = "yyrwkk";
Console.WriteLine(propertyDemo.Name);
Console.ReadKey();
}
}
public class PropertyDemo
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
}
可以简化语句
public string Name {
get => name;
set => name = value;
}
属性跟字段的相同点
属性和字段的不同点
允许只声明属性而不声明后备字段,编译器会创建隐藏的后备字段,并且自动挂接到get或者set访问器上。
自动实现属性的要点:
using System;
namespace PropertyDemo
{
internal class Program
{
static void Main(string[] args)
{
PropertyDemo propertyDemo = new PropertyDemo();
propertyDemo.Name = "yyrwkk";
Console.WriteLine(propertyDemo.Name);
Console.ReadKey();
}
}
public class PropertyDemo
{
//private string name;
public string Name{get;set;}
}
}
属性可以声明为static,静态属性的访问器和所有静态成员一样。
using System;
namespace PropertyDemo
{
internal class Program
{
static void Main(string[] args)
{
PropertyDemo.Name = "yyrwkk";
Console.WriteLine(PropertyDemo.Name);
Console.ReadKey();
}
}
public class PropertyDemo
{
//private string name;
public static string Name {get;set;}
}
}
可声明只含get访问器的属性,这称为只读属性,无法修改。
public string Name{
get => name;
}
声明只含set访问器的属性,称为只写属性,无法读取。
public string Name {
set => Name = value;
}
只写属性适用于对密码这样的数据进行保护。
声明属性时可以指定可访问性
在属性声明中,可以为set和get访问器单独指定可访问性,从而覆盖属性的可访问性
class PropertyDemo{
private string name;
public string Name {
get => name;
private set => name = value;
}
}
Name的get访问器就是public的,而Name的set访问器就是private的
在接口中可以声明属性
interface Position{
int X {get;set;}
int Y {get;set;}
}
实现接口的任何类或者结构都必须实现X和Y属性。
接口中的属性不能指定访问修饰符。
使用属性初始化对象
using System;
namespace PropertyDemo
{
internal class Program
{
static void Main(string[] args)
{
PropertyDemo propertyDemo = new PropertyDemo() { Age=19,Name="yyrwkk" };
Console.WriteLine(propertyDemo.Name);
Console.ReadKey();
}
}
public class PropertyDemo
{
public int Age { get; set; }
public string Name { get; set; }
}
}
调用对象初始化器,C#编译器会自动生成代码来调用默认构造器,然后调用每个具名属性的set访问器,把它们初始化成指定值。
还可以使用自定义的构造函数
using System;
namespace PropertyDemo
{
internal class Program
{
static void Main(string[] args)
{
PropertyDemo propertyDemo = new PropertyDemo(175) { Age=19,Name="yyrwkk" };
Console.WriteLine(propertyDemo.Name);
Console.ReadKey();
}
}
public class PropertyDemo
{
private int height;
public int Age { get; set; }
public string Name { get; set; }
public PropertyDemo(int height)
{
this.height = height;
}
}
}