Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Thinking in C# phần 4 pptx
Nội dung xem thử
Mô tả chi tiết
Chapter 7: Reusing Classes 255
But be careful with your assumptions. In general, it’s difficult to anticipate how a
class can be reused, especially a general-purpose class. Unless you declare a
method as virtual, you prevent the possibility of reusing your class through
inheritance in some other programmer’s project simply because you couldn’t
imagine it being used that way.
Initialization and
class loading
In more traditional languages, programs are loaded all at once as part of the
startup process. This is followed by initialization, and then the program begins.
The process of initialization in these languages must be carefully controlled so
that the order of initialization of statics doesn’t cause trouble. C++, for example,
has problems if one static expects another static to be valid before the second
one has been initialized.
C# doesn’t have this problem because it takes a different approach to loading.
Because everything in C# is an object, many activities become easier, and this is
one of them. As you will learn more fully in the next chapter, the compiled code
for a set of related classes exists in their own separate file, called an assembly.
That file isn’t loaded until the code is needed. In general, you can say that “Class
code is loaded at the point of first use.” This is often not until the first object of
that class is constructed, but loading also occurs when a static field or static
method is accessed.
The point of first use is also where the static initialization takes place. All the
static objects and the static code block will be initialized in textual order (that
is, the order that you write them down in the class definition) at the point of
loading. The statics, of course, are initialized only once.
Initialization with inheritance
It’s helpful to look at the whole initialization process, including inheritance, to get
a full picture of what happens. Consider the following code:
//:c07:Beetle.cs
// The full process of initialization.
using System;
class Insect {
int i = 9;
internal int j;
256 Thinking in C# www.MindView.net
internal Insect() {
Prt("i = " + i + ", j = " + j);
j = 39;
}
static int x1 =
Prt("static Insect.x1 initialized");
internal static int Prt(string s) {
Console.WriteLine(s);
return 47;
}
}
class Beetle : Insect {
int k = Prt("Beetle.k initialized");
Beetle() {
Prt("k = " + k);
Prt("j = " + j);
}
static int x2 =
Prt("static Beetle.x2 initialized");
public static void Main() {
Prt("Beetle constructor");
Beetle b = new Beetle();
}
} ///:~
The output for this program is:
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
Beetle.k initialized
i = 9, j = 0
k = 47
j = 39
The first thing that happens when you run Beetle is that you try to access
Beetle.Main( ) (a static method), so the loader goes out and finds the compiled
code for the Beetle class (this happens to be in an assembly called Beetle.exe).
In the process of loading it, the loader notices that it has a base class (that’s what
the colon after class Beetle says), which it then loads. This will happen whether
Chapter 7: Reusing Classes 257
or not you’re going to make an object of that base class. (Try commenting out the
object creation to prove it to yourself.)
If the base class has a base class, that second base class would then be loaded,
and so on. Next, the static initialization in the root base class (in this case,
Insect) is performed, and then the next derived class, and so on. This is
important because the derived-class static initialization might depend on the base
class member being initialized properly.
At this point, the necessary classes have all been loaded so the object can be
created. First, all the primitives in this object are set to their default values and
the object references are set to null—this happens in one fell swoop by setting
the memory in the object to binary zero. Then, the base-class fields are initialized
in textual order, followed by the fields of the object. After the fields are initialized,
the base-class constructor will be called. In this case the call is automatic, but you
can also specify the base-class constructor call (by placing a color after the
Beetle( ) constructor and then saying base( )). The base class construction goes
through the same process in the same order as the derived-class constructor.
Finally, the rest of the body of the constructor is executed.
Summary
Both inheritance and composition allow you to create a new type from existing
types. Typically, however, you use composition to reuse existing types as part of
the underlying implementation of the new type, and inheritance when you want
to reuse the interface. Since the derived class has the base-class interface, it can
be upcast to the base, which is critical for polymorphism, as you’ll see in the next
chapter.
Despite the strong emphasis on inheritance in object-oriented programming,
when you start a design you should generally prefer composition during the first
cut and use inheritance only when it is clearly necessary. Composition tends to be
more flexible. In addition, by using the added artifice of inheritance with your
member type, you can change the exact type, and thus the behavior, of those
member objects at run-time. Therefore, you can change the behavior of the
composed object at run-time.
Although code reuse through composition and inheritance is helpful for rapid
project development, you’ll generally want to redesign your class hierarchy before
allowing other programmers to become dependent on it. Your goal is a hierarchy
in which each class has a specific use and is neither too big (encompassing so
much functionality that it’s unwieldy to reuse) nor annoyingly small (you can’t
use it by itself or without adding functionality).
258 Thinking in C# www.ThinkingIn.NET
Exercises
1. Create two classes, A and B, with default constructors (empty argument
lists) that announce themselves. Inherit a new class called C from A, and
create a member of class B inside C. Do not create a constructor for C.
Create an object of class C and observe the results.
2. Modify Exercise 1 so that A and B have constructors with arguments
instead of default constructors. Write a constructor for C and perform all
initialization within C’s constructor.
3. Create a simple class. Inside a second class, define a field for an object of
the first class. Use lazy initialization to instantiate this object.
4. Inherit a new class from class Detergent. Override Scrub( ) and add a
new method called Sterilize( ).
5. Take the file Cartoon.cs and comment out the constructor for the
Cartoon class. Explain what happens.
6. Take the file Chess.cs and comment out the constructor for the Chess
class. Explain what happens.
7. Prove that default constructors are created for you by the compiler.
8. Prove that the base-class constructors are (a) always called, and (b) called
before derived-class constructors.
9. Create a base class with only a nondefault constructor, and a derived
class with both a default and nondefault constructor. In the derived-class
constructors, call the base-class constructor.
10. Create a class called Root that contains an instance of each of classes
(that you also create) named Component1, Component2, and
Component3. Derive a class Stem from Root that also contains an
instance of each “component.” All classes should have default
constructors that print a message about that class.
11. Modify Exercise 10 so that each class only has nondefault constructors.
12. Add a proper hierarchy of Dispose( ) methods to all the classes in
Exercise 11.
Chapter 7: Reusing Classes 259
13. Create a class with a method that is overloaded three times. Inherit a new
class, add a new overloading of the method, and show that all four
methods are available in the derived class.
14. In Car.cs add a Service( ) method to Engine and call this method in
Main( ).
15. Create a class inside a namespace. Your class should contain a
protected method and a protected internal method. Compile this
class into a library assembly. Write a new class that tries to call these
methods; compile this class into an executable assembly (you’ll need to
reference the library assembly while compiling, of course). Explain the
results. Now inherit from your first class and call the protected and
protected internal methods from this derived class. Compile this
derived class into its own assembly and explain the resulting behavior.
16. Create a class called Amphibian. From this, inherit a class called Frog.
Put appropriate methods in the base class. In Main( ), create a Frog
and upcast it to Amphibian, and demonstrate that all the methods still
work.
17. Modify Exercise 16 so that Frog overrides the method definitions from
the base class (provides new definitions using the same method
signatures). Note what happens in Main( ).
18. Create a class with a method that is not defined as virtual. Inherit from
that class and attempt to override that method.
19. Create a sealed class and attempt to inherit from it.
20. Prove that class loading takes place only once. Prove that loading may be
caused by either the creation of the first instance of that class, or the
access of a static member.
21. In Beetle.cs, inherit a specific type of beetle from class Beetle,
following the same format as the existing classes. Trace and explain the
output.
22. Find a way where inheritance can be used fruitfully in the party domain.
Implement at least one program that solves a problem by upcasting.
23. Draw a UML class diagram of the party domain, showing inheritance and
composition. Place classes that interact often near each other and classes
in different namespaces far apart or even on separate pieces of paper.
260 Thinking in C# www.MindView.net
Consider the task of ensuring that all guests are given a ride home by
someone sober or given a place to sleep over. Add classes, namespaces,
methods, and data as appropriate.
24. Consider how you would approach the tasks that you have solved in the
party domain in the programming language other than C#, with which
you are most familiar. Fill in this Venn diagram comparing aspects of the
C# approach with how you would do it otherwise:
Unique to C# Unique to other
Similar
♦ Are there aspects unique to one approach that you see as having a major
productivity impact?
♦ What are some important aspects that both approaches share?
261
8: Interfaces and
Implementation
Polymorphism is the next essential feature of an objectoriented programming language after data abstraction. It
allows programs to be developed in the form of
interacting agreements or “contracts” that specify the
behavior, but not the implementation, of classes.
Polymorphism provides a dimension of separation of interface from
implementation, to decouple what from how. Polymorphism allows improved
code organization and readability as well as the creation of extensible programs
that can be “grown” not only during the original creation of the project but also
when new features are desired.
Encapsulation creates new data types by combining characteristics and
behaviors. Implementation hiding separates the interface from the
implementation by making the details private. This sort of mechanical
organization makes ready sense to someone with a procedural programming
background. But polymorphism deals with decoupling in terms of types. In the
last chapter, you saw how inheritance allows the treatment of an object as its own
type or its base type. This ability is critical because it allows many types (derived
from the same base type) to be treated as if they were one type, and a single piece
of code to work on all those different types equally. The polymorphic method call
allows one type to express its distinction from another, similar type, as long as
they’re both derived from the same base type. This distinction is expressed
through differences in behavior of the methods that you can call through the base
class.
In this chapter, you’ll learn about polymorphism (also called dynamic binding or
late binding or run-time binding) starting from the basics, with simple examples
that strip away everything but the polymorphic behavior of the program.
262 Thinking in C# www.ThinkingIn.NET
Upcasting revisited
In Chapter 7 you saw how an object can be used as its own type or as an object of
its base type. Taking an object reference and treating it as a reference to its base
type is called upcasting, because of the way inheritance trees are drawn with the
base class at the top.
You also saw a problem arise, which is embodied in the following:
//:c08:Music.cs
// Inheritance & upcasting.
using System;
public class Note {
private int value;
private Note(int val) { value = val;}
public static Note
MIDDLE_C = new Note(0),
C_SHARP = new Note(1),
B_FLAT = new Note(2);
} // Etc.
public class Instrument {
public virtual void Play(Note n) {
Console.WriteLine("Instrument.Play()");
}
}
// Wind objects are instruments
// because they have the same interface:
public class Wind : Instrument {
// Redefine interface method:
public override void Play(Note n) {
Console.WriteLine("Wind.Play()");
}
}
public class Music {
public static void Tune(Instrument i) {
// ...
i.Play(Note.MIDDLE_C);
}