字段(field)设置成static就是静态变量,
或者说是类变量。
属性(property)就是为获取私有变量的
但是如果属性(property)也设置成static
是什么意思?
难道说字段(field)设置成static以后
获取该字段的属性(property)必须置成static吗?还有就是属性(property)就是为获取私有变量服务的
但是如果属性(property)也设置成private,
这也可以吗?
那属性(property)还有什么作用?
感觉跟Java的概念完全不一样,迷糊中......
或者说是类变量。
属性(property)就是为获取私有变量的
但是如果属性(property)也设置成static
是什么意思?
难道说字段(field)设置成static以后
获取该字段的属性(property)必须置成static吗?还有就是属性(property)就是为获取私有变量服务的
但是如果属性(property)也设置成private,
这也可以吗?
那属性(property)还有什么作用?
感觉跟Java的概念完全不一样,迷糊中......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); new Y();
} static int X
{
get
{
return 100;
} set
{
MessageBox.Show("X");
}
}
class Y
{
public Y()
{
X = 10;
}
}
}
}
属性定义为private时,没什么意义。
属性的另一个重要作用就是有参属性(索引器)。
其实我感觉属性只是微软为了增加代码的可读性而加入的,完全可以用方法代替。虽然它也是方法,但与字段相似。
public class People
{
public string Name{get;set;}//名字人人都有但是各个不同 public static bool isHaveHead=true;//头人人都有一个
}