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

CHAPTER 7 CLASSES AND INHERITANCE

Hiding Members of a Base Class

Although a derived class cannot delete any of the members it has inherited, it can hide them.

To hide an inherited data member, declare a new member of the same type and with the same name.

You can hide, or mask, an inherited function member by declaring in the derived class a new function member with the same signature. Remember that the signature consists of the name and parameter list but does not include the return type.

To let the compiler know that you are purposely hiding an inherited member, use the new modifier. Without it, the program will compile successfully, but the compiler will warn you that you are hiding an inherited member.

You can also hide static members.

The following code declares a base class and a derived class, each with a string member called Field1. The keyword new is used to explicitly tell the compiler to mask the base class member. Figure 7-4 illustrates an instance of each class.

class SomeClass

// Base class

{

 

 

 

 

 

 

 

 

 

 

public string Field1;

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

class OtherClass : SomeClass

// Derived class

{

 

 

 

 

 

 

 

 

 

 

new public string Field1;

// Mask base member with same name

 

 

 

 

 

 

 

Keyword

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-4. Hiding a member of a base class

165

CHAPTER 7 CLASSES AND INHERITANCE

In the following code, OtherClass derives from SomeClass but hides both its inherited members. Note the use of the new modifier. The code is illustrated in Figure 7-5.

class SomeClass

 

 

// Base class

{

 

 

 

public string Field1 = "SomeClass Field1";

 

public void

Method1(string value)

 

{ Console.WriteLine("SomeClass.Method1: {0}", value); }

}

 

 

 

class OtherClass : SomeClass

// Derived class

{ Keyword

 

 

 

 

 

 

new public string Field1 = "OtherClass Field1";

// Mask the base member.

new public void

Method1(string value)

// Mask the base member.

{ Console.WriteLine("OtherClass.Method1: {0}", value); }

}Keyword

class Program

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

static void Main()

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

OtherClass oc = new OtherClass();

// Use the masking member.

oc.Method1(oc.Field1);

// Use the masking member.

}

 

 

 

 

 

 

 

 

 

This code produces the following output:

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OtherClass.Method1: OtherClass Field1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 7-5. Hiding a field and a method of the base class

166

CHAPTER 7 CLASSES AND INHERITANCE

Base Access

If your derived class absolutely must access a hidden inherited member, you can access it by using a base access expression. This expression consists of the keyword base, followed immediately by a period and the name of the member, as shown here:

Console.WriteLine("{0}", base.Field1);

Base access

For example, in the following code, derived class OtherClass hides Field1 in its base class but accesses it by using a base access expression.

class SomeClass {

 

// Base class

public string Field1 = "Field1 -- In the base class";

 

}

 

 

 

 

 

 

 

 

class OtherClass : SomeClass {

 

// Derived class

new public string Field1 = "Field1 -- In the derived

class";

 

 

 

 

 

 

Hides the field in the base class

 

 

public void PrintField1()

 

 

{

Console.WriteLine(Field1);

// Access

the derived class.

 

 

 

 

Console.WriteLine(base.Field1);

// Access

the base class.

}

 

 

 

 

 

 

}

 

 

 

Base access

 

 

class Program { static void Main()

{

OtherClass oc = new OtherClass(); oc.PrintField1();

}

}

This code produces the following output:

Field1 -- In the derived class

Field1 -- In the base class

If you use this feature frequently, you might want to revaluate the design of your classes. Generally there are more elegant designs, but the feature is there if there’s a situation where nothing else will do.

167

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