Csharp/C Sharp/Class Interface/Object Cast

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

Casting Objects

<source lang="csharp"> using System; public class MotorVehicle {

   public string model;
   public MotorVehicle(string model) {
       this.model = model;
   }
   public void Start() {
       Console.WriteLine(model + " started");
   }

} public class Product : MotorVehicle {

   public bool convertible;
   public Product(string model, bool convertible) :
       base(model) {
       this.convertible = convertible;
   }

}

public class Motorcycle : MotorVehicle {

   public bool sidecar;
   public Motorcycle(string model, bool sidecar) :
       base(model) {
       this.sidecar = sidecar;
   }
   public void PullWheelie() {
       Console.WriteLine(model + " pulling a wheelie!");
   }

} class MainClass {

   public static void Main() {
       Product myProduct = new Product("MR2", true);
       MotorVehicle myMotorVehicle = (MotorVehicle)myProduct;
       Console.WriteLine("myMotorVehicle.model = " + myMotorVehicle.model);
       myMotorVehicle.Start();
       Motorcycle myMotorcycle = new Motorcycle("V-Rod", true);
       MotorVehicle myMotorVehicle2 = (MotorVehicle)myMotorcycle;
       Console.WriteLine("myMotorVehicle2.model =" + myMotorVehicle2.model);
       myMotorVehicle2.Start();
       Motorcycle myMotorcycle2 = (Motorcycle)myMotorVehicle2;
       Console.WriteLine("myMotorcycle2.model = " + myMotorcycle2.model);
       Console.WriteLine("myMotorcycle2.sidecar = " + myMotorcycle2.sidecar);
       myMotorcycle2.Start();
       myMotorcycle2.PullWheelie();
   }

}

</source>


Casting objects: downcast

<source lang="csharp"> using System;

public class CPU {

 public string model;
  
 public CPU(string model) {
   this.model = model;
 }
  
 public void Start() {
   Console.WriteLine(model + " started");
 }

}

public class Intel : CPU {

 public bool convertible;
  
 public Intel(string model, bool convertible) : base(model) {
   this.convertible = convertible;
 }

}

public class AMD : CPU {

 public bool sidecar;
  
 public AMD(string model, bool sidecar) : base(model) {
   this.sidecar = sidecar;
 }
  
 public void PullWheelie() {
   Console.WriteLine(model + " pulling a wheelie!");
 }
  

}


class Test {

 public static void Main() {
   Intel myIntel = new Intel("MR2", true);
  
   // create a AMD object
   AMD myAMD = new AMD("V-Rod", true);
  
   // cast myAMD to CPU (upcast)
   CPU myCPU2 = (CPU) myAMD;
  
  
   // cast myCPU2 to AMD (downcast)
   AMD myAMD2 = (AMD) myCPU2;
  
   // myMotorCycle2 has access to all members of the AMD class
   Console.WriteLine("myAMD2.model = " + myAMD2.model);
   Console.WriteLine("myAMD2.sidecar = " + myAMD2.sidecar);
   myAMD2.Start();
   myAMD2.PullWheelie();
 }

}

      </source>


Casting objects: upcast

<source lang="csharp"> using System;

public class CPU {

 public string model;
  
 public CPU(string model) {
   this.model = model;
 }
  
 public void Start() {
   Console.WriteLine(model + " started");
 }

}

public class Intel : CPU {

 public bool convertible;
  
 public Intel(string model, bool convertible) : base(model) {
   this.convertible = convertible;
 }

}

public class AMD : CPU {

 public bool sidecar;
  
 public AMD(string model, bool sidecar) : base(model) {
   this.sidecar = sidecar;
 }
  
 public void PullWheelie() {
   Console.WriteLine(model + " pulling a wheelie!");
 }
  

}


class Test {

 public static void Main() {
   Intel myIntel = new Intel("MR2", true);
  
   // cast myIntel to CPU (upcast)
   CPU myCPU = (CPU) myIntel;
  
   Console.WriteLine("myCPU.model = " + myCPU.model);
   myCPU.Start();
 }

}

      </source>


Downcast will fail.

<source lang="csharp"> class Employee { }

class ContractEmployee : Employee { }

class CastExample3 {

   public static void Main ()
   {
       ContractEmployee ce = (ContractEmployee)new Employee(); 
   }

}

</source>


This code raises an exception at run time because of an invalid cast

<source lang="csharp"> using System;

public class Starter {

   public static void Main() {
       MyClass obj = new MyClass();
       // Fails at compile time
       // YClass alias=obj;
       // Fails at run time
       YClass alias = (YClass)obj;
       obj.MethodA();
       obj.MethodB();
   }

} public class MyClass {

   public virtual void MethodA() {
   }
   public virtual void MethodB() {
   }

} public class YClass : MyClass {

   public override void MethodA() {
   }

}

</source>