Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Prog_Guide.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
6.22 Mб
Скачать

Запечатанные классы и члены классов

Классы можно объявлять в качестве запечатанных. Для этого в определении класса необходимо перед ключевым словом class поместить ключевое слово sealed. Пример.

public sealed class D

{

// Class members here.

}

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

Член класса, метод, поле, свойство или событие для производного класса, переопределяющего виртуальный член базового класса, может объявлять этот член как запечатанный. Это делает бесполезным виртуальный аспект члена для каждого последующего производного класса. Для этого в объявлении члена класса необходимо перед ключевым словом override поместить ключевое слово sealed. Пример.

public class D : C

{

public sealed override void DoWork() { }

}

How to: Define Abstract Properties

The following example shows how to define abstract properties. An abstract property declaration does not provide an implementation of the property accessors -- it declares that the class supports properties, but leaves the accessor implementation to derived classes. The following example demonstrates how to implement the abstract properties inherited from a base class.

This sample consists of three files, each of which is compiled individually and its resulting assembly is referenced by the next compilation:

  • abstractshape.cs: the Shape class that contains an abstract Area property.

  • shapes.cs: The subclasses of the Shape class.

  • shapetest.cs: A test program to display the areas of some Shape-derived objects.

To compile the example, use the following command:

csc abstractshape.cs shapes.cs shapetest.cs

This will create the executable file shapetest.exe.

Example

This file declares the Shape class that contains the Area property of the type double.

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

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

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

  • abstractshape.cs: класс Shape, содержащий абстрактное свойство Area.

  • shapes.cs: вложенные классы класса Shape.

  • shapetest.cs: Тестовая программа для отображения областей некоторых производных от Shape объектов.

Для компиляции примера используйте следующую команду: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);

}

}

  • Modifiers on the property are placed on the property declaration itself. For example:

    public abstract double Area

  • When declaring an abstract property (such as Area in this example), you simply indicate what property accessors are available, but do not implement them. In this example, only a get accessor is available, so the property is read-only.

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

  • Модификаторы свойства помещаются в самом объявлении свойства. Пример.

    public abstract double Area

  • При объявлении абстрактного свойства (такого как Area в этом примере) просто указываются имеющиеся методы доступа к свойствам, и их реализация не выполняется. В этом примере имеется только метод доступа get, поэтому свойство доступно только для чтения.

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

}

}