Methoden der Klasse Object
A10.cs
using System;
abstract class Animal {
public abstract string Name { get; }
public abstract void WhoAreYou();
public abstract bool Likes(string food);
public abstract void Speak();
}
class Dog : Animal {
private string name;
public Dog(string name) {
this.name = name;
}
public override string Name {
get { return name; }
}
public override void WhoAreYou() {
Console.WriteLine("I am a dog");
}
public override bool Likes(string food) {
return food == "bones" || food == "meat";
}
public override void Speak() {
Console.WriteLine("Wooff");
}
public override string ToString() {
return "dog " + name;
}
public override bool Equals(object obj) {
Dog other = (Dog)obj;
return name == other.name;
}
public override int GetHashCode() {
int res = 0;
foreach (char ch in name) res = res ^ ch;
return res;
}
public Dog ShallowCopy() {
return (Dog)MemberwiseClone();
}
}
class Test {
public static void Main() {
Dog lupo = new Dog("lupo");
Dog clone = lupo.ShallowCopy();
if (lupo.Equals(clone))
Console.WriteLine(lupo + " has the code " + lupo.GetHashCode());
}
}
|
Damit zwei Dog-Objekte miteinander verglicher werden können, wurde in
Dog ein Feld name eingeführt, daß einen Hund identifiziert.
|