Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 7 CLASSES AND INHERITANCE

Abstract Classes

Abstract classes are designed to be inherited from. An abstract class can be used only as the base class of another class.

You cannot create instances of an abstract class.

An abstract class is declared using the abstract modifier.

Keyword

abstract class MyClass

{

...

}

An abstract class can contain abstract members or regular, nonabstract members. The members of an abstract class can be any combination of abstract members and normal members with implementations.

An abstract class can itself be derived from another abstract class. For example, the following code shows one abstract class derived from another.

abstract class AbClass

// Abstract class

{

 

...

 

}

 

abstract class MyAbClass : AbClass

// Abstract class derived from

{

// an abstract class

...

 

}

 

Any class derived from an abstract class must implement all the abstract members of the class by using the override keyword, unless the derived class is itself abstract.

192

CHAPTER 7 CLASSES AND INHERITANCE

Example of an Abstract Class and an Abstract Method

The following code shows an abstract class called AbClass with two methods.

The first method is a normal method with an implementation that prints out the name of the class. The second method is an abstract method that must be implemented in a derived class. Class DerivedClass inherits from AbClass and implements and overrides the abstract method. Main creates an object of DerivedClass and calls its two methods.

Keyword

 

 

 

 

abstract class AbClass

 

// Abstract class

{

 

 

 

 

 

public void IdentifyBase()

 

// Normal method

{ Console.WriteLine("I am AbClass"); }

 

 

 

Keyword

 

 

 

 

 

 

abstract public void IdentifyDerived();

 

// Abstract method

}

 

 

 

 

 

class DerivedClass : AbClass

 

// Derived class

{ Keyword

 

 

 

 

 

 

override public void IdentifyDerived()

 

// Implementation of

{ Console.WriteLine("I am DerivedClass"); }

// abstract method

}

 

 

 

 

 

class Program

 

 

{

 

 

 

 

 

static void Main()

 

 

{

 

 

 

 

 

// AbClass a = new AbClass();

// Error.

Cannot instantiate

 

// a.IdentifyDerived();

// an abstract class.

 

DerivedClass b = new DerivedClass(); // Instantiate the derived class.

 

b.IdentifyBase();

// Call the inherited method.

 

b.IdentifyDerived();

// Call the "abstract" method.

}

}

This code produces the following output:

I am AbClass

I am DerivedClass

193

CHAPTER 7 CLASSES AND INHERITANCE

Another Example of an Abstract Class

The following code shows the declaration of an abstract class that contains data members as well as function members. Data members cannot be declared as abstract.

abstract class MyBase

// Combination of abstract

and non-abstract members

{

 

 

 

 

public

int SideLength

= 10;

//

Data member

const

int TriangleSideCount = 3;

//

Data member

abstract public void PrintStuff( string s );

//

Abstract method

abstract public int

MyInt { get; set; }

//

Abstract property

public

int PerimeterLength( )

//

Regular, non-abstract method

{ return TriangleSideCount * SideLength; }

 

 

}

 

 

 

 

class MyClass : MyBase

 

 

 

{

 

 

 

 

public

override void PrintStuff( string s )

//

Override abstract method

{ Console.WriteLine( s ); }

 

 

private int _myInt;

 

 

 

public

override int MyInt

//

Override abstract property

{

 

 

 

 

get

{ return _myInt; }

 

 

set

{ _myInt = value; }

 

 

}

 

 

 

 

}

 

 

 

 

class Program

{

static void Main( string[] args )

{

MyClass mc = new MyClass( ); mc.PrintStuff( "This is a string." ); mc.MyInt = 28;

Console.WriteLine( mc.MyInt );

Console.WriteLine( "Perimeter Length: {0}", mc.PerimeterLength( ) );

}

}

This code produces the following output:

This is a string. 28

Perimeter Length: 30

194

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]