Csharp/CSharp Tutorial/Class/Abstract Class

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

abstract class with three virtual methods

<source lang="csharp">public abstract class MyOperations {

   public virtual void Operation1() {
   }
   public virtual void Operation2() {
   }
   public virtual void Operation3() {
   }

} public class MyClass : MyOperations {

   public override void Operation1() { }
   public override void Operation2() { }

} public class MainClass {

   public void DoWork( MyOperations ops ) {
       ops.Operation3();
   }

}</source>

Create an abstract class.

<source lang="csharp">using System;

abstract class Shape {

 double pri_width;  // private 
 double pri_height; // private 
 string pri_name;   // private 
 
 public Shape() {  
   width = height = 0.0;  
   name = "null";  
 }  
 
 public Shape(double w, double h, string n) {  
   width = w;  
   height = h;  
   name = n;  
 }  
 
 public Shape(double x, string n) {  
   width = height = x;  
   name = n;  
 }  
 
 public Shape(Shape ob) {  
   width = ob.width;  
   height = ob.height;  
   name = ob.name;  
 }  
 
 public double width { 
   get { return pri_width; } 
   set { pri_width = value; } 
 } 

 public double height { 
   get { return pri_height; } 
   set { pri_height = value; } 
 } 

 public string name { 
   get { return pri_name; } 
   set { pri_name = value; } 
 } 
 
 public void showDim() {  
   Console.WriteLine("Width and height are " +  
                      width + " and " + height);  
 }  
 
 // Now, area() is abstract. 
 public abstract double area(); 

}

class Triangle : Shape {

 string style; // private 
   
 public Triangle() {  
   style = "null";  
 }  
 
 public Triangle(string s, double w, double h) : 
   base(w, h, "triangle") {  
     style = s;   
 }  
 
 public Triangle(double x) : base(x, "triangle") {  
   style = "isosceles";   
 }  
 
 public Triangle(Triangle ob) : base(ob) {  
   style = ob.style;  
 }  
 
 public override double area() {  
   return width * height / 2;  
 }  
 
 public void showStyle() {  
   Console.WriteLine("Triangle is " + style);  
 }  

}

class Rectangle : Shape {

 public Rectangle(double w, double h) :  
   base(w, h, "rectangle"){ }  
 
 public Rectangle(double x) :  
   base(x, "rectangle") { }  
 
 public Rectangle(Rectangle ob) : base(ob) { }  
 
 public bool isSquare() {   
   if(width == height) 
      return true;   
   return false;   
 }   
    
 // Override area() for Rectangle. 
 public override double area() {   
   return width * height;   
 }   

}

class MainClass {

 public static void Main() {  
   Shape[] shapes = new Shape[4];  
 
   shapes[0] = new Triangle("right", 8.0, 12.0);  
   shapes[1] = new Rectangle(10);  
   shapes[2] = new Rectangle(10, 4);  
   shapes[3] = new Triangle(7.0);  
 
   for(int i=0; i < shapes.Length; i++) {  
     Console.WriteLine("object is " + shapes[i].name);  
     Console.WriteLine("Area is " + shapes[i].area());  
 
     Console.WriteLine();    
   }  
 }  

}</source>

object is triangle
Area is 48
object is rectangle
Area is 100
object is rectangle
Area is 40
object is triangle
Area is 24.5

Define abstract class and abstract method

<source lang="csharp">using System; abstract class Employee {

   public Employee(string name, float billingRate)
   {
       this.name = name;
       this.billingRate = billingRate;
   }
   
   virtual public float CalculateCharge(float hours)
   {
       return(hours * billingRate);
   }
   
   abstract public string TypeName();
   
   private string name;
   protected float billingRate;

} class Manager: Employee {

   public Manager(string name, float billingRate) : base(name, billingRate)
   {
   }
   
   override public float CalculateCharge(float hours)
   {
       if (hours < 1.0F)
       hours = 1.0F;        // minimum charge.
       return(hours * billingRate);
   }
   
   // This override is required, or an error is generated.
   override public string TypeName()
   {
       return("Manager");
   }

} class Clerk: Employee {

   public Clerk(string name, float billingRate) :
   base(name, billingRate)
   {
   }
   
   override public string TypeName()
   {
       return("Clerk");
   }

} class Test {

   public static void Main()
   {
       Employee[] earray = new Employee[2];
       earray[0] = new Manager("A", 40.0F);
       earray[1] = new Clerk("C", 45.0F);
       
       Console.WriteLine("{0} charge = {1}",
       earray[0].TypeName(),
       earray[0].CalculateCharge(2F));
       Console.WriteLine("{0} charge = {1}",
       earray[1].TypeName(),
       earray[1].CalculateCharge(0.75F));
   }

}</source>

Manager charge = 80
Clerk charge = 33.75

Override abstract method

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

 static void Main(string[] args)
 {
   B MyB = new C();
   MyB.Display();
 }

} abstract class A {

 public abstract void Display();

} class B: A {

 public override void Display()
 {
   Console.WriteLine("Class B"s Display Method");
 }

} class C: B {

 public override void Display()
 {
   Console.WriteLine("Class C"s Display Method");
 }

} class D: C {

 public override void Display()
 {
   Console.WriteLine("Class D"s Display Method");
 }

}</source>

Class C"s Display Method

Polymorphism and Virtual Functions

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

   public abstract void Play();

} public class RedClock: Clock {

   public override void Play() {
       Console.WriteLine("RedClock.Play()");
   }

} public class BlueClock: Clock{

   public override void Play() 
   {
       Console.WriteLine("BlueClock.Play()");
   }

} class MainClass {

   public static void CallPlay(Clock ms)
   {
       ms.Play();
   }
   public static void Main()
   {
       Clock ms = new RedClock();
       CallPlay(ms);
       ms = new BlueClock();
       CallPlay(ms);
   }

}</source>

RedClock.Play()
BlueClock.Play()

Using Abstract Classes

  1. An abstract method is created by specifying the abstract type modifier.
  2. An abstract method contains no body.
  3. An abstract method is not implemented by the base class.
  4. An abstract method is automatically virtual.
  5. A derived class must override it.
  6. The abstract modifier cannot be applied to static methods.
  7. Properties can also be abstract.
  8. A class containing abstract methods must be declared as abstract with the abstract specifier.
  9. There can be no objects of an abstract class.
  10. If a derived class doesn"t implement all of the abstract methods in the base class, then the derived class must also be specified as abstract.

To declare an abstract method, use this general form:


<source lang="csharp">abstract type name(parameter-list);</source>