Wei-Meng Lee - C# 2008 Programmer's Reference

Здесь есть возможность читать онлайн «Wei-Meng Lee - C# 2008 Programmer's Reference» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Город: Indianapolis, Год выпуска: 2009, ISBN: 2009, Издательство: Wiley Publishing, Inc., Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

C# 2008 Programmer's Reference: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «C# 2008 Programmer's Reference»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

C# 2008 Programmers Reference provides a concise and thorough reference on all aspects of the language. Each chapter contains detailed code samples that provide a quick and easy way to understand the key concepts covered.

C# 2008 Programmer's Reference — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «C# 2008 Programmer's Reference», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

IPerson p = d as IPerson;

Console.WriteLine(p.Age()); //---38---

What's happened is that the instance of the IPersoninterface (p) uses the Age()method defined in the Employeeclass.

Summary

An interface defines the contract for a class — the various members that a class must have, the result returned for each method, and so on. However, an interface does not provide the implementation for a class; the actual implementation is left to the implementing classes. This chapter presented different ways in which you can work with interfaces — implementing multiple interfaces, extending interfaces, casting to an interface, and so forth.

Chapter 6

Inheritance

Inheritance is one of the fundamental concepts in object-oriented programming. Inheritance facilitates code reuse and allows you to extend the functionality of code that you have already written. This chapter looks at:

□ How inheritance works

□ Implementing inheritance in C#

□ Defining abstract methods and classes

□ Sealing classes and methods

□ Defining overloaded methods

□ The different types of access modifiers you can use in inheritance

□ Using inheritance in interfaces

Understanding Inheritance in C#

The following Employee class contains information about employees in a company:

public class Employee {

public string Name { get; set; }

public DateTime DateofBirth { get; set; }

public ushort Age() {

return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);

}

}

Manager is a class containing information about managers:

public class Manager {

public string Name { get; set; }

public DateTime DateofBirth { get; set; }

public ushort Age() {

return (ushort)(DateTime.Now.Year - this.DateofBirth.Year);

}

public Employee[] subordinates { get; set; }

}

The key difference between the Managerclass and the Employeeclass is that Managerhas an additional property, subordinates, that contains an array of employees under the supervision of a manager. In fact, a manager is actually an employee, except that he has some additional roles. In this example, the Managerclass could inherit from the Employeeclass and then add the additional subordinates property that it requires, like this:

public class Manager: Employee {

public Employee[] subordinates { get; set; }

}

By inheriting from the Employeeclass, the Manager class has all the members defined in the Employeeclass made available to it. The relationships between the Employeeand Managerclasses can be represented using a class diagram as shown in Figure 6-1.

Figure 61 Employeeis known as the base class and Manageris a derived class - фото 111

Figure 6-1

Employeeis known as the base class and Manageris a derived class. In object-oriented programming, inheritance is classified into two types: implementation and interface. This chapter explores both.

Implementation Inheritance

Implementation inheritance is when a class derives from another base class, inheriting all the base class's members. To add new members to a class, you can define another class that derives from the existing base class. Using implementation inheritance, the new derived class inherits all of the implementation provided in the base class.

To understand how inheritance works in C#, define a simple class as follows:

public class Shape {

//---properties---

public double length { get; set; }

public double width { get; set; }

//---method---

public double Perimeter() {

return 2 * (this.length + this.width);

}

}

Here, the Shapeclass contains two properties and a single method. By itself, this class does not specify a particular shape, but it does assume that a basic shape contains length and width. It also assumes that the perimeter of a shape is simply double the sum of its length and width.

Using this base class, you can define other shapes such as square, rectangle, and circle. Let's start with the rectangle shape. Using Shapeas the base class, you can define a Rectangleclass (a derived class because it derives from the Shapeclass) by inheriting from the Shapeclass, like this:

public class Rectangle : Shape {}

In C#, you use the colon ( :) operator to indicate that a class inherits from another class (known as the base class). This example reads: "The Rectangleclass inherits from the Shapeclass." This means that whatever members the Shapeclass has are inherited by the Rectangleclass. (In this example, the Rectangleclass has no implementation; that will be added in the next few sections.)

C# supports only single-class inheritance, which means that a class can inherit directly from only one base class. If you do not specify the base class, the C# compiler assumes that it is inheriting from the System.Objectclass. Because the Shapeclass did not specify who it is inheriting from, it is equivalent to:

public class Shape : Object {

//---properties---

public double length { get; set; }

public double width { get; set; }

//---method---

public double Perimeter() {

return 2 * (this.length + this.width);

}

}

To use the Rectangleclass, you instantiate it as you would other classes:

Rectangle r = new Rectangle();

Because the Rectangleclass inherits all the members of the Shapeclass, you can access its members as if they are defined within the Rectangleclass itself:

r.length = 4;

r.width = 5;

Console.WriteLine(r.Perimeter()); //---18---

Abstract Class

The Shapeclass does not specify a particular shape, and thus it really does not make sense for you to instantiate it directly, like this:

Shape someShape = new Shape();

Instead, all other shapes should inherit from this base class. To ensure that you cannot instantiate the Shapeclass directly, you can make it an abstract class by using the abstractkeyword:

public abstract class Shape {

//---properties---

public double length { get; set; }

public double width { get; set; }

//---method---

public double Perimeter() {

return 2 * (this.length + this.width);

}

}

Once a class is defined as abstract, you can no longer instantiate it directly; the following is now not permitted:

//---cannot instantiate directly---

Shape someShape = new Shape();

The abstractkeyword indicates that the class is defined solely for the purpose of inheritance; other classes need to inherit from it in order to have objects of this base type.

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «C# 2008 Programmer's Reference»

Представляем Вашему вниманию похожие книги на «C# 2008 Programmer's Reference» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «C# 2008 Programmer's Reference»

Обсуждение, отзывы о книге «C# 2008 Programmer's Reference» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x