Csharp/CSharp Tutorial/Class/override — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
- 1 new method: not override
- 2 Overridden Equals()
- 3 Override without "override" keyword
- 4 Overriding Virtual Methods
- 5 PrintValue() Added to the BaseClass Class
- 6 Resolving Ambiguity with the new Keyword
- 7 Resolving Ambiguity with the override Modifier
- 8 Three-level Override methods
- 9 Virtual and override member function
new method: not override
<source lang="csharp">using System; class BaseClass {
virtual public void Print() { Console.WriteLine("This is the base class."); }
} class DerivedClass : BaseClass {
override public void Print() { Console.WriteLine("This is the derived class."); }
} class SecondDerived : DerivedClass {
new public void Print() { Console.WriteLine("This is the second derived class."); }
} class MainClass {
static void Main() { SecondDerived derived = new SecondDerived(); BaseClass mybc = (BaseClass)derived; derived.Print(); mybc.Print(); }
}</source>
B.DoSomething A.DoSomething
Overridden Equals()
<source lang="csharp">using System; using System.Text;
class Person {
public Person(string fname, string lname, string ssn, byte a) { FirstName = fname; LastName = lname; SSN = ssn; age = a; } public string FirstName; public string LastName; public string SSN; public byte age; public override bool Equals(object o) { Person temp = (Person)o; if(temp.FirstName == this.FirstName && temp.LastName == this.LastName && temp.SSN == this.SSN && temp.age == this.age) { return true; } else return false; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.AppendFormat("[FirstName= {0}", this.FirstName); sb.AppendFormat(" LastName= {0}", this.LastName); sb.AppendFormat(" SSN= {0}", this.SSN); sb.AppendFormat(" Age= {0}]", this.age); return sb.ToString(); } public override int GetHashCode() { return SSN.GetHashCode(); }
}
class MainClass {
public static void Main(string[] args) { Person p1 = new Person("Fred", "Jones", "222-22-2222", 98); Person p2 = new Person("Fred", "Jones", "222-22-2222", 98); if(p1.Equals(p2) && p1.GetHashCode() == p2.GetHashCode()) Console.WriteLine("P1 and P2 have same state\n"); else Console.WriteLine("P1 and P2 are DIFFERENT\n"); }
}</source>
P1 and P2 have same state
Override without "override" keyword
<source lang="csharp">using System; public class A {
public virtual void SomeMethod() { Console.WriteLine( "A::SomeMethod" ); }
} public class B : A {
public void SomeMethod() { Console.WriteLine( "B::SomeMethod" ); }
} public class MainClass {
static void Main() { B b = new B(); A a = b; a.SomeMethod(); }
}</source>
A::SomeMethod
Overriding Virtual Methods
<source lang="csharp">class Point2D {
public int X; public int Y; public virtual void PrintToConsole() { System.Console.WriteLine("({0}, {1})", X, Y); }
}
class Point3D : Point2D {
public int Z; public override void PrintToConsole() { System.Console.WriteLine("({0}, {1}, {2})", X, Y, Z); }
}
class MyMainClass {
public static void Main() { Point2D My2DPoint = new Point2D(); Point3D My3DPoint = new Point3D(); My2DPoint.X = 100; My2DPoint.Y = 200; My3DPoint.X = 150; My3DPoint.Y = 250; My3DPoint.Z = 350; My2DPoint.PrintToConsole(); My3DPoint.PrintToConsole(); }
}</source>
PrintValue() Added to the BaseClass Class
<source lang="csharp">using System;
public class BaseClass {
protected int Value; public BaseClass() { Value = 123; } public virtual void PrintValue() { Console.WriteLine("Value: " + Value); }
}
public class DerivedClass : BaseClass {
public void PrintValue() { Console.WriteLine("Value = " + Value); }
}
class MainClass {
public static void Main() { DerivedClass DerivedClassObject = new DerivedClass(); DerivedClassObject.PrintValue(); }
}</source>
Resolving Ambiguity with the new Keyword
<source lang="csharp">using System;
public class BaseClass {
protected int Value; public BaseClass() { Value = 123; } public void PrintValue() { Console.WriteLine("Value: " + Value); }
}
public class DerivedClass : BaseClass {
new public void PrintValue() { Console.WriteLine("Value = " + Value); }
}
class MainClass {
public static void Main() { DerivedClass DerivedClassObject = new DerivedClass(); DerivedClassObject.PrintValue(); }
}</source>
Resolving Ambiguity with the override Modifier
<source lang="csharp">using System;
public class BaseClass {
protected int Value; public BaseClass() { Value = 123; } public virtual void PrintValue() { Console.WriteLine("Value: " + Value); }
}
public class DerivedClass : BaseClass {
override public void PrintValue() { Console.WriteLine("Value = " + Value); }
}
class MainClass {
public static void Main() { DerivedClass DerivedClassObject = new DerivedClass(); DerivedClassObject.PrintValue(); }
}</source>
Three-level Override methods
<source lang="csharp">using System; class BaseClass {
virtual public void Print(){ Console.WriteLine("This is the base class."); }
} class DerivedClass : BaseClass {
override public void Print() { Console.WriteLine("This is the derived class."); }
} class SecondDerived : DerivedClass {
override public void Print() { Console.WriteLine("This is the second derived class."); }
} class MainClass {
static void Main() { SecondDerived derived = new SecondDerived(); BaseClass mybc = (BaseClass)derived; derived.Print(); mybc.Print(); }
}</source>
This is the second derived class. This is the second derived class.
Virtual and override member function
<source lang="csharp">class Class1
{ public virtual void Hello() { System.Console.Write( "Hello from Class1" ); } } class Class2 : Class1 { public override void Hello() { base.Hello(); System.Console.Write( " and hello from Class2 too" ); } public static void Main( string[] args ) { Class2 c2 = new Class2(); c2.Hello(); } }</source>
Hello from Class1 and hello from Class2 too