Csharp/CSharp Tutorial/Class/override

Материал из .Net Framework эксперт
Версия от 12:16, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new method: not override

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();
   }
}
B.DoSomething
A.DoSomething

Overridden Equals()

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");
    }
}
P1 and P2 have same state

Override without "override" keyword

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();
    }
}
A::SomeMethod

Overriding Virtual Methods

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();
    }
}

PrintValue() Added to the BaseClass Class

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();
    }
}

Resolving Ambiguity with the new Keyword

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();
    }
}

Resolving Ambiguity with the override Modifier

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();
    }
}

Three-level Override methods

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();
   }
}
This is the second derived class.
This is the second derived class.

Virtual and override member function

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();
        }
    }
Hello from Class1 and hello from Class2 too