• Tidak ada hasil yang ditemukan

INHERITANCE

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 55-60)

4 VARIABLES AND DATA TYPES

Problem 3: TheBook

4.6 INHERITANCE

Different kinds of objects often have several characteristics in common with each other. If you think of banknotes they all have a value, but they differ with respect to how they look.

They have different colors, and they show different pictures. In object-oriented programming it is possible that classes can inherit each other. That means that you can collect common properties in a base class, while the differences can be placed in derived classes. Below is a class that represent a note with the value 100 (a Danish banknote):

using System;

namespace BankProgram {

class BankNote100 : BankNote {

public BankNote100() : base(100) {

}

public void Print() {

Console.WriteLine(”Den gamle Lillebæltsbro og Hindsgavl-dolken”);

} } }

The class is called BankNote100, and after the class name you define with a colon that the class inherits the class BankNote. This means that the class inherits all the properties and methods that a BankNote have and is possible expanded with new properties and methods.

In this case BankNote100 extends BankNote with a method Print(), to simulate how the note looks (a text written in Danish). The class BankNote has a constructor which initialize the instance variable value. A BankNote100 must also initialize this variable (with the value 100). It happens in the class’s constructor with a call of base(100), which means that the base class’s constructor is executed.

It is clear that in quite the same way you can write a class BankNote200 representing a banknote with the value 200.

With these classes available you can write the following Main() method:

static void Main(string[] args) {

BankNote100 note1 = new BankNote100();

BankNote200 note2 = new BankNote200();

note1.Print();

note2.Print();

Console.WriteLine(note1.Value + note2.Value);

}

Here, you create two objects of the types respectively BankNote100 and BankNote200. Next the objects Print() method are called. Note the syntax, and how to perform a method on an object using the dot operator. The last statement prints the sum of the two objects values.

Here you should note the use of the property Value. It is defined in the class BankNote, but is inherited to the classes BankNote100 and BankNote200.

Often wee illustrate inheritance as follows:

In this context the base class BankNote is also called a super-class while the derived classes BankNote100 and BankNote200 are called sub-classes. Sometimes wee also say that BankNote is a generalization of BankNote100 and BankNote200 while these classes are called specializations of BankNote.

Interfaces

An object’s properties and methods are known to the outside world through the public methods that the class defines and thus made available as services. Methods form the object’s interface to the outside world. In order to define an object’s characteristics, one can use an interface which defines the methods of an object of a certain class. For example you can define a banknote as follows:

namespace BankProgram {

interface Note {

public int Value { get; } public void Print();

} }

It looks like the definition of a class, but you should notice that it simply is only a definition and that the interface does not contain code that performs something. Note the syntax for a property and how to define that the property Value only should have a get method. Also note that the interface has no using statements. This will often be the case, but in this case there are no references to .NET types which are not known and are therefore unnecessary.

Given an interface you can define that a class must implement this interface. Thus you specify that the class has the methods that the interface defines. For example the class BankNote can implement the interface Note as follows:

namespace BankProgram {

abstract class BankNote : Note {

private int value;

public BankNote(int value) {

this.value = value;

}

public int Value {

get {

return value;

} }

public abstract void Print();

} }

Note first how to define that the class implements an interface, which is the same syntax as for inheritance. This means that the class should have a property Value and a method Print() - it must implement the methods that the interface defines. It is not the case, since it do not implements the method Print(). It can not do that because it does not know how this method should be written. It is the derived classes BankNote100 and BankNote200, that has that knowledge. The problem is solved by defining the class as abstract and the method Print() must in the class BankNote be defined as abstract. The method then has to be implemented in the derived classes, what it indeed is. That a class is abstract has further that consequence that you can not instantiate objects whose type is the abstract class, in this case BankNote. It is also reasonable, else you could instantiate notes with an arbitrary

The class BankNote100 inherits BankNote which is an abstract class, and BankNote100 must then implements the abstract method Print() what it already do, but it is necessary with the word override to indicate that it is a method overridden from the base class:

public override void Print() {

Console.WriteLine(”Den gamle Lillebæltsbro og Hindsgavl-dolken”);

}

The same goes for the class BankNote200. Below is a new version of the main() method:

static void Main(string[] args) {

Note note1 = new BankNote100();

Note note2 = new BankNote200();

note1.Print();

note2.Print();

Console.WriteLine(note1.Value + note2.Value);

}

There will again be created two objects, respectively a BankNote100 and a BankNote200 object, but their type is Note. A BankNote100 is especially a BankNote, which is again is a Note. This means that the two objects in the rest of the program alone is known from the defining interface and then in the rest of the program are objects that provides two services Value and Print().

From the above, it is not obvious what the advantage of interfaces are and it will first be clear later, but the explanation must be sought in that, note1 and note2 are in the program only known from the defining interface, which means that the two objects are treated equally independent their specific types.

The project BankProgram implements the above classes and interface.

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 55-60)