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


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

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



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

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

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

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

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

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

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

Полиморфизм

За счет наследования класс может использоваться в качестве нескольких типов; он может использоваться как собственный тип, любой базовый тип или тип любого интерфейса, если он реализует интерфейсы. Это называется полиморфизмом. В языке C# каждый тип обладает полиморфизмом. Типы могут использоваться в качестве экземпляров своего типа или типа Object, потому что любой тип автоматически использует тип Object в качестве базового.

Полиморфизм важен не только для производных классов, но и для базовых. В случае использования базового класса, фактически, может использоваться объект производного класса, приведенный к типу базового класса. Разработчики базового класса могут предполагать, какие аспекты их базового класса скорее всего будут изменены в производном типе. Например, базовый класс для машин может содержать поведение, которое меняется в зависимости от того, является рассматриваемая машина минивэном или кабриолетом. В базовом классе эти методы могут быть отмечены как виртуальные, что позволит в производных классах, представляющих кабриолеты и минивэны, переопределить это поведение.

Дополнительные сведения см. в разделе Наследование.



Поделиться:


Последнее изменение этой страницы: 2017-01-19; просмотров: 98; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.220.160.216 (0.008 с.)