1.静态和非静态的方法
静态成员是属于类所有的,而非静态成员则属于类的实例(对象)所有.
非静态方法可以访问类中的任何成员,而静态方法只能访问类中的静态成员.
看一个例子:
calss A
{
int x;
static int y;
static int F()
{
x = 1; //错误,这个静态方法F() 不能访问一个非静态成员
y = 2; //正确, 静态方法只能访问类中的静态成员
}
}
原因:
因为 x 是非静态成员,在类的每个实例中都占有一个存储, 而 静态方法是类所共享的,它无法
判断出当前的 x 是属于哪个类的实例,所以不知道应该到哪个地址去读取当前X的值. 而y 是非静态成
员,所有的类实例都公用一个副本,故可以访问.
但是又并是不说静态方法就无法识别非静态成员了,在C#中,我们可以灵活的采用 传递参数 的办
法.
下面看一个例子:
public string m_cation;
public static string GetWindowCaption(Window w)
{
return w.m_caption; //这里的m_caption是一个非静态成员"窗口的标题"
}
这里通过参数 w 将对象传递给方法执行,这样它就可以通过具体的类的实例指明调用的对象,这时它可以
访问具体实例中的成员,无论是静态还是非静态的.
对于只读字段,我们只能在字段的定义中和这强制性属类的构造函数中进行修改,在其他情况下,字段是"只
读"的.
public static readonly int x=1;
public const int x=1;
这两个是等效的.
实例我用的是控制台程序
Program.cs
using System.Collections.Generic;
using System.Text;
using testStudent;
using System.Net; // getIP类中要用到的命名空间;
namespace testStudent
... {
class Program
...{
...#region
//这个类是一个重载一元操作符的类事例
//一元操作符重载没有参数
//这个将演示操作符 ++ 重载
class Player
...{
public int neili;
public int tili;
public int jingyan;
public int neili_r;
public int tili_r;
public Player()
...{//构造函数
neili = 10;
tili = 50;
jingyan = 0;
neili_r = 50;
tili_r = 50;
}
public static Player operator ++(Player p)
...{//玩家角色升级时
p.neili = p.neili + 50;
p.tili = p.tili + 100;
p.jingyan += 100;
p.neili_r = p.neili;
p.tili_r = p.tili;
return p;
}
public void Show()
...{
Console.WriteLine("体力:{0}",tili);
Console.WriteLine("经验:{0}",jingyan);
Console.WriteLine("内力:{0}",neili);
Console.WriteLine("剩余体力:{0}",tili_r);
Console.WriteLine("剩余内力:{0}",neili_r);
}
#endregion
}
...#region
//下面这个将演示二元操作符 + 的重载
class DKR
...{ //笛卡尔积坐标相加
public int x, y, z;
public DKR(int vx,int vy,int vz)
...{
x = vx;
y = vy;
z = vz;
}
public static DKR operator +(DKR d1,DKR d2)
...{
DKR dkr = new DKR(0,0,0);
dkr.x = d1.x + d2.x;
dkr.y = d1.y + d2.y;
dkr.z = d1.z + d2.z;
return dkr;
}
public const int xxx = 1;
}
#endregion
class getIp
...{
IPAddress[] m_arrayIPs;
public void Resolve(string s_host)
...{
IPHostEntry ip = Dns.GetHostByName(s_host);
m_arrayIPs = ip.AddressList;
}
public IPAddress this[int nIndex]
...{
get ...{
return m_arrayIPs[nIndex];
}
}
public int IPLength
...{
get ...{
return m_arrayIPs.Length;
}
}
}
static void Main(string[] args)
...{
student s1 = new student("jiao",21,48);
student s2 = new student("wgf",23,65);
Console.Write("年龄最大的是:"+s1.max(s1.s_age,s2.s_age));
Console.Write("体重最大的是:"+s1.max(s1.s_weight,s2.s_weight));
Console.WriteLine(" 这里是一个玩家游戏:");
Player man = new Player();
man.Show();
man++;
Console.WriteLine("玩家升级后的属性显示:");
man.Show();
Console.WriteLine("下面这个是一个二元操作符重载的例子:");
DKR d1 = new DKR(1,2,3);
DKR d2 = new DKR(1,2,3);
DKR d3 = d1 + d2;
Console.WriteLine("d3的值:d1.x+d2.x={0},d1.y+d2.y={1},d1.z+d2.z={2}",d3.x,d3.y,d3.z);
Console.WriteLine("DKR中的常量xxx={0}",DKR.xxx);
Console.WriteLine();
Console.WriteLine("下面这里将通过域名获取IP:");
getIp getIp1 = new getIp();
getIp1.Resolve("www.sohu.com");
int n = getIp1.IPLength;
Console.WriteLine("现在将获取主机的IP:");
Console.WriteLine();
for (int i = 0; i < n;i++ )
...{
Console.WriteLine(getIp1[i]);
}
Console.ReadKey();
}
}
}
student.cs
using System.Collections.Generic;
using System.Text;
namespace testStudent
... {
class student
...{
public string s_name;
public int s_age;
public float s_weight;
public student(string n, int age, float weight)
...{
s_name = n;
s_age = age;
s_weight = weight;
}
public int max(int x, int y)
...{
if (x > y)
...{
return x;
}
else
...{
return y;
}
}
public float max(float x, float y)
...{
if (x > y)
...{
return x;
}
else
...{
return y;
}
}
}
}