可能关于存储问题,帮忙看看啊

ZSW53362465 2009-04-01 04:15:24
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace shejimoshi
{
class Program
{
// static string wepeanAction;
static void Main(string[] args)
{
GamePersonFactory facory = new GamePersonFactory();

facory.add("KING", "1", "Axe");
facory.add("KING", "2", "Knife");
facory.add("Fighter", "3", "Sword");
facory.add("KING", "4", "Knife");
facory.add("Fighter", "5", "BowAndArroy");
Console.Read();
facory.show("张仕威");
Console.Read();

}
}
public class personCollection
{
public List<GamePersonFactory> list = new List<GamePersonFactory>();
}
public class GamePersonFactory
{

Person person;
Weapon weapon;
string _person;
string _name;
string _weapon;
personCollection l;
public GamePersonFactory()
{
l = new personCollection();
}

public void add(string person, string name, string weapon)
{
this._person = person;
this._name = name;
this._weapon = weapon;
if (this._person == "KING")
{
this.person = new King(name);
}
if (this._person == "Fighter")
{
this.person = new Fighter(name);
}
if (this._weapon == "Sword")
{
this.weapon = new Sword();
}
if (this._weapon == "BowAndArroy")
{
this.weapon = new BowAndArroy();
}
if (this._weapon == "Knife")
{
this.weapon = new Knife();
}
if (this._weapon == "Axe")
{
this.weapon = new Axe();
}
Connect();
l.list.Add(this);
}

public void Connect()
{
this.person.setWeapon(this.weapon);
}

public void show(string name)
{
foreach (GamePersonFactory g in l.list)
{
if (g._name==name)
{
g.person.fight();
}
}
}


}
public class Person
{
public string _name;
public Weapon _weapon;
public Person()
{

}

public void setWeapon(Weapon weapon)
{
this._weapon = weapon;
}
public virtual void fight()
{

}
}
public class King:Person
{

public King(string name)
{
this._name = name;
}
public override void fight()
{
Console.WriteLine("I AM IS" + this._name);
Console.WriteLine("KING FIGHTING");
this._weapon.UseWeapon();
}
}
public class Fighter : Person
{
public Fighter(string name)
{
this._name = name;
}
public override void fight()
{
Console.WriteLine("I AM IS" + this._name);
Console.WriteLine("FIGHTER FIGHTING");
this._weapon.UseWeapon();
}
}
public class Weapon
{
public virtual void UseWeapon()
{

}
}
public class Sword:Weapon
{
public override void UseWeapon()
{
Console.WriteLine("Use Sword Fighting");
}
}
public class BowAndArroy:Weapon
{
public override void UseWeapon()
{
Console.WriteLine("Use BowAndArroy Fighting");
}
}
public class Knife :Weapon
{
public override void UseWeapon()
{
Console.WriteLine("Use Knife Fighting");
}
}
public class Axe : Weapon
{
public override void UseWeapon()
{
Console.WriteLine("Use Axe Fighting");
}
}

}


大家帮忙看下这个程序,真郁闷GamePersonFactory类中有个地方l.list.Add(this),5个类型创建后,你可以调试一下,最后list里面有5个相同的对象,这是怎么回事,如果听不懂粘下程序运行一下,监视l.list,告诉我这是怎么回事啊
...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kingsley726 2009-04-01
  • 打赏
  • 举报
回复
public void add(string person, string name, string weapon)
{
GamePersonFactory gpf = new GamePersonFactory();
gpf._person = person;
gpf._name = name;
gpf._weapon = weapon;
if (gpf._person == "KING")
{
gpf.person = new King(name);
}
if (gpf._person == "Fighter")
{
gpf.person = new Fighter(name);
}
if (gpf._weapon == "Sword")
{
gpf.weapon = new Sword();
}
if (gpf._weapon == "BowAndArroy")
{
gpf.weapon = new BowAndArroy();
}
if (gpf._weapon == "Knife")
{
gpf.weapon = new Knife();
}
if (gpf._weapon == "Axe")
{
gpf.weapon = new Axe();
}
l.list.Add(gpf);
}

这样在add方法里面新建一个对象再添加就可以了.
Kingsley726 2009-04-01
  • 打赏
  • 举报
回复
因为你每次add的时候实际上是改变了factory对象的属性.而你添加进list里面的是这个对象的引用,所以每当你改变一次factory那么之前的引用也会被改变.

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧