logo search
CSharp_Prog_Guide

Определение абстрактных свойств

В следующем примере показано, как определить абстрактные свойства. Если объявление абстрактного свойства не предоставляет реализацию методов доступа к свойствам, оно объявляет, что класс поддерживает свойства, но оставляет реализацию метода доступа производному классу. В следующем примере показано, как реализовать абстрактные свойства, унаследованные от базового класса.

Этот пример состоит из трех файлов, каждый из которых компилируется отдельно, и на результирующую сборку которого ссылается следующая компиляция.

Для компиляции примера используйте следующую команду:28

csc abstractshape.cs shapes.cs shapetest.cs

Будет создан исполняемый файл shapetest.exe.

Пример

Этот файл объявляет класс Shape, содержащий свойство Area типа double.

// compile with: csc /target:library abstractshape.cs

public abstract class Shape

{

private string m_id;

public Shape(string s)

{

// calling the set accessor of the Id property.

Id = s;

}

public string Id

{

get

{

return m_id;

}

set

{

m_id = value;

}

}

// Area is a read-only property - only a get accessor is needed:

public abstract double Area

{

get;

}

public override string ToString()

{

return Id + " Area = " + string.Format("{0:F2}", Area);

}

}

-----Пример

The following code shows three subclasses of Shape and how they override the Area property to provide their own implementation.

// compile with: csc /target:library /reference:abstractshape.dll shapes.cs

public class Square : Shape

{

private int m_side;

public Square(int side, string id)

: base(id)

{

m_side = side;

}

public override double Area

{

get

{

// Given the side, return the area of a square:

return m_side * m_side;

}

}

}

public class Circle : Shape

{

private int m_radius;

public Circle(int radius, string id)

: base(id)

{

m_radius = radius;

}

public override double Area

{

get

{

// Given the radius, return the area of a circle:

return m_radius * m_radius * System.Math.PI;

}

}

}

В следующем коде показано три вложенных класса Shape и переопределение или свойства Area для предоставления собственной реализации.

---

public class Rectangle : Shape

{

private int m_width;

private int m_height;

public Rectangle(int width, int height, string id)

: base(id)

{

m_width = width;

m_height = height;

}

public override double Area

{

get

{

// Given the width and height, return the area of a rectangle:

return m_width * m_height;

}

}

}

The following code shows a test program that creates a number of Shape-derived objects and prints out their areas.

// compile with: csc /reference:abstractshape.dll;shapes.dll shapetest.cs

class TestClass

{

static void Main()

{

Shape[] shapes =

{

new Square(5, "Square #1"),

new Circle(3, "Circle #1"),

new Rectangle( 4, 5, "Rectangle #1")

};

System.Console.WriteLine("Shapes Collection");

foreach (Shape s in shapes)

{

System.Console.WriteLine(s);

}

}

}

Shapes Collection

Square #1 Area = 25.00

Circle #1 Area = 28.27

Rectangle #1 Area = 20.00

В следующем коде показана тестовая программа, создающая ряд производных от Shape объектов и печатающая их области.

------

Polymorphism

Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism. In C#, every type is polymorphic. Types can be used as their own type or as a Object instance, because any type automatically treats Object as a base type.

Polymorphism is important not only to the derived classes, but to the base classes also. Anyone using the base class could, in fact, be using an object of the derived class that has been cast to the base class type. Designers of a base class can anticipate the aspects of their base class that are likely to change for a derived type. For example, a base class for cars might contain behavior that is subject to change when the car in question is a minivan or a convertible. A base class can mark those class members as virtual, which enables derived classes that represent convertibles and minivans to override that behavior.

Polymorphism Overview

When a derived class inherits from a base class, it gains all the methods, fields, properties and events of the base class. To change the data and behavior of a base class, you have two choices: you can replace the base member with a new derived member, or you can override a virtual base member.

Replacing a member of a base class by using a new derived member requires the new keyword. If a base class defines a method, field, or property, the new keyword is used to create a new definition of that method, field, or property on a derived class. The new keyword is put before the return type of a class member that is being replaced. For example:

public class BaseClass

{

public void DoWork() { }

public int WorkField;

public int WorkProperty

{

get { return 0; }

}

}

public class DerivedClass : BaseClass

{

public new void DoWork() { }

public new int WorkField;

public new int WorkProperty

{

get { return 0; }

}

}