Csharp/CSharp Tutorial/Class/Derived Class — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:16, 26 мая 2010

A base class reference can refer to a derived class object

using System; 
 
class BaseClass { 
  public int a; 
 
  public BaseClass(int i) { 
    a = i; 
  } 
} 
 
class DerivedClass : BaseClass { 
  public int b; 
 
  public DerivedClass(int i, int j) : base(j) { 
    b = i; 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    BaseClass x = new BaseClass(10); 
    BaseClass x2;  
    DerivedClass y = new DerivedClass(5, 6); 
 
    x2 = x; // OK, both of same type 
    Console.WriteLine("x2.a: " + x2.a); 
 
    x2 = y; // Ok because DerivedClass is derived from BaseClass 
    Console.WriteLine("x2.a: " + x2.a); 
 
    // BaseClass references know only about BaseClass members 
    x2.a = 19; // OK 
//    x2.b = 27; // Error, BaseClass doesn"t have a b member 
  } 
}
x2.a: 10
x2.a: 6

Illustrates member accessibility in subclass

using System;
public class Car
{
  private   string make;
  protected string model;
  public Car(string make, string model)
  {
    this.make = make;
    this.model = model;
  }
  public virtual void Start()
  {
    startCar();
    System.Console.WriteLine("Vehicle started");
  }
  private void startCar()
  {
    System.Console.WriteLine("Turning starter motor...");
  }
}
public class MyFirstCar : Car
{
  public MyFirstCar(string make, string model) : base(make, model)
  {
  }
  public override void Start()
  {
    Console.WriteLine("Starting " + model);  
    base.Start();  
  }
}
class MainClass
{
  public static void Main()
  {
    MyFirstCar myCar = new MyFirstCar("Toyota", "MR2");
    myCar.Start();
  }
}
Starting MR2
Turning starter motor...
Vehicle started

Illustrates member hiding

using System;
public class BaseClass
{
  public string lastName;
  public string firstName;
  public BaseClass(string lastName, string firstName)
  {
    Console.WriteLine("In BaseClass constructor");
    this.lastName = lastName;
    this.firstName = firstName;
    Console.WriteLine("this.lastName = " + this.lastName);
    Console.WriteLine("this.firstName = " + this.firstName);
  }
  public void DisplayfirstName()
  {
    Console.WriteLine("In BaseClass DisplayfirstName() method");
    Console.WriteLine("firstName = " + firstName);
  }
}
public class DerivedClass : BaseClass
{
  public new string firstName;
  public DerivedClass(string lastName, string firstName) : base(lastName, "Test")
  {
    Console.WriteLine("In DerivedClass constructor");
    this.firstName = firstName;
    Console.WriteLine("this.firstName = " + this.firstName);
  }
  public new void DisplayfirstName()
  {
    Console.WriteLine("In DerivedClass DisplayfirstName() method");
    Console.WriteLine("firstName = " + firstName);
    base.DisplayfirstName();
  }
}

class MainClass
{
  public static void Main()
  {
    Console.WriteLine("Creating a DerivedClass object");
    DerivedClass myDerivedClass = new DerivedClass("A", "B");
    Console.WriteLine("Back in Main() method");
    Console.WriteLine("myDerivedClass.lastName = " + myDerivedClass.lastName);
    Console.WriteLine("myDerivedClass.firstName = " + myDerivedClass.firstName);
    Console.WriteLine("Calling myDerivedClass.DisplayfirstName()");
    myDerivedClass.DisplayfirstName();
  }
}
Creating a DerivedClass object
In BaseClass constructor
this.lastName = A
this.firstName = Test
In DerivedClass constructor
this.firstName = B
Back in Main() method
myDerivedClass.lastName = A
myDerivedClass.firstName = B
Calling myDerivedClass.DisplayfirstName()
In DerivedClass DisplayfirstName() method
firstName = B
In BaseClass DisplayfirstName() method
firstName = Test