Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Composition syntax

java



+ Font mai mare | - Font mai mic



Composition syntax

Until now, composition has been used quite frequently. You simply place object handles inside new classes. For example, suppose you'd like an object that holds several String objects, a couple of primitives and an object of another class. For the non-primitive objects, just put handles inside your new class, and for the primitives just define them inside your class: (See page 97 if you have trouble executing this program.)



//: SprinklerSystem.java

// Composition for code reuse

package c06;

class WaterSource

public String toString()

public class SprinklerSystem

public static void main(String[] args)

One of the methods defined in WaterSource is special: toString( ). You will learn later that every non-primitive object has a toString( ) method, and it's called in special situations when the compiler wants a String but it's got one of these objects. So in the expression:

System.out.println('source = ' + source);

the compiler sees you trying to add a String object ("source = ") to a WaterSource. This doesn't make sense to it, because you can only "add" a String to another String, so it says "I'll turn source into a String by calling toString( )!" After doing this it can combine the two Strings and pass the resulting String to System.out.println( ). Any time you want to allow this behavior with a class you create you need only write a toString( ) method.

At first glance, you might assume - Java being as safe and careful as it is - that the compiler would automatically construct objects for each of the handles in the above code, for example calling the default constructor for WaterSource to initialize source. The output of the print statement is in fact:

valve1 = null

valve2 = null

valve3 = null

valve4 = null

i = 0

f = 0.0

source = null

Primitives that are fields in a class are automatically initialized to zero, as noted in Chapter 2. But the object handles are initialized to null, and if you try to call methods for any of them you'll get an exception. It's actually pretty good (and useful) that you can still print them out without throwing an exception.

It makes sense that the compiler doesn't just create a default object for every handle because that would incur unnecessary overhead in many cases. If you want the handles initialized, you can do it:

At the point the objects are defined. This means that they'll always be initialized before the constructor is called.

In the constructor for that class

Right before you actually need to use the object. This can reduce overhead, if there are situations where the object doesn't need to be created.

All three approaches are shown here:

//: Bath.java

// Constructor initialization with composition

class Soap

public String toString()

public class Bath

void print()

public static void main(String[] args)

Note that in the Bath constructor a statement is executed before any of the initializations take place. When you don't initialize at the point of definition, there's still no guarantee that you'll perform any initialization before you send a message to an object handle - except for the inevitable run-time exception.

Here's the output for the program:

Inside Bath()

Soap()

s1 = Happy

s2 = Happy

s3 = Joy

s4 = Joy

i = 47

toy = 3.14

castille = Constructed

When print( ) is called it fills in s4 so that all the fields are properly initialized by the time they are used.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 586
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved