您的位置:首页 > 其它

类间聚集关系理解

2011-12-08 18:15 225 查看
C# code:

/// <summary>

///

/// </summary>

/// 这类似聊天室的聚集

class Human

{

private string name;

private Breath BreathState,DeathY;

public Human(string Name,Breath Air,Breath deathY)

{

name = Name;

BreathState = Air;

DeathY = deathY;

}

public override string ToString()

{

string result;

result = name + "\n";

result += "BreathState:" + BreathState + "\n";

result += "DeathState:" + DeathY + "\n";

return result;

}

}

class Breath

{

//类似聊天室里的文件共享功能模块!

private string breathState;//呼吸状况

private string deathState;

public Breath(string state,string State)

{

breathState = state;

deathState = State;

}

public override string ToString()

{

string result;

result = breathState + "\n";

result += deathState + "\n";

return result;

}

}

static void Main(string[] args)

{

//类的依赖关系 ,类Person调用类Air的方法ReleasePower();

/* Person me=new Person ();

while (true)

{

me.breath();

}*///

//类的聚集关系--人都需要呼吸,人作为结点加入Human这个聚集,Human对象是可以被加入到对象共享的;

Breath Sbreath=new Breath ("呼吸正常!","还没死!");

Breath Sdeath=new Breath ("活着!","很好!");

Human S = new Human("Steven", Sbreath, Sdeath);

Breath Wbreath = new Breath("呼吸困难!", "快死了");

Breath Wdeath = new Breath("死了!", "哭吧!");

Human W = new Human("WiFi", Wbreath, Wdeath);

Console.WriteLine(S);

Console.WriteLine();

Console.WriteLine(W);

Console.ReadKey();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: