//Листинг 8.5. Перегрузка методов класса object
using System;
namespace Pavlovs185
{
class Monster
{
public Monster(int health, int ammo, string name)
{
this.health = health;
this.ammo = ammo;
this.name = name;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
Monster temp = (Monster)obj;
return health == temp.health && // Что это за преобразование
ammo == temp.ammo && // какого рода ?
name == temp.name;
}
public override int GetHashCode()
{
return health.GetHashCode();
}
public override string ToString()
{
return string.Format( "Monster {0} \t health = {1} ammo = { 2 } " ,
name, health, ammo );
}
string name;
int health, ammo;
}
class Class1
{
static void Main()
{
Monster X = new Monster(80, 80, "Вася");
Monster Y = new Monster(80, 80, "Вася");
Monster Z = X;
if (X == Y) Console.WriteLine(" X == Y ");
else Console.WriteLine(" X != Y ");
if (X == Z) Console.WriteLine("X == Z ");
else Console.WriteLine("X != Z" ) ;
if (X.Equals(Y)) Console.WriteLine("X Equals Y");
else Console.WriteLine("X not Equals Y ");
Console.WriteLine(X.GetType());
Console.ReadKey();
}
}
}
using System;
namespace Pavlovs185
{
class Monster
{
public Monster(int health, int ammo, string name)
{
this.health = health;
this.ammo = ammo;
this.name = name;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
Monster temp = (Monster)obj;
return health == temp.health && // Что это за преобразование
ammo == temp.ammo && // какого рода ?
name == temp.name;
}
public override int GetHashCode()
{
return health.GetHashCode();
}
public override string ToString()
{
return string.Format( "Monster {0} \t health = {1} ammo = { 2 } " ,
name, health, ammo );
}
string name;
int health, ammo;
}
class Class1
{
static void Main()
{
Monster X = new Monster(80, 80, "Вася");
Monster Y = new Monster(80, 80, "Вася");
Monster Z = X;
if (X == Y) Console.WriteLine(" X == Y ");
else Console.WriteLine(" X != Y ");
if (X == Z) Console.WriteLine("X == Z ");
else Console.WriteLine("X != Z" ) ;
if (X.Equals(Y)) Console.WriteLine("X Equals Y");
else Console.WriteLine("X not Equals Y ");
Console.WriteLine(X.GetType());
Console.ReadKey();
}
}
}