Syntax for composing Java classes

Source: Internet
Author: User
Tags soap tostring

In the previous learning situation, in fact, several "compositing" operations have been done. For the synthesis, we simply place the object handle in the new class. For example, suppose you need to hold several string objects in one object, two basic data types, and one object that belongs to another class. For objects of a primitive type, you simply place the handle in the new class, and for the base data type, you need to define them in your own class. As shown below (if you are having trouble executing this program, see Chapter 3rd 3.1.2 Subsection "assignment"):

 

//: Sprinklersystem.java//Composition for code reuse package C06;
  Class Watersource {private String s;
    Watersource () {System.out.println ("Watersource ()");
  s = new String ("constructed");

Public String toString () {return s;}}
  public class Sprinklersystem {private String valve1, Valve2, Valve3, Valve4;
  Watersource source;
  int i;
  float F;
    void print () {System.out.println ("valve1 =" + valve1);
    System.out.println ("valve2 =" + valve2);
    System.out.println ("valve3 =" + Valve3);
    System.out.println ("valve4 =" + valve4);
    System.out.println ("i =" + i);
    System.out.println ("f =" + f);
  System.out.println ("Source =" + source);
    public static void Main (string[] args) {Sprinklersystem x = new Sprinklersystem ();
  X.print (); }
} ///:~


A method defined within the Watersource is more specific: toString (). It will soon be known that every object of a primitive type has a ToString () method. This method is invoked if the compiler would have wanted a string, but had obtained an object of that sort. So in the following expression:
System.out.println ("Source =" + source);
The compiler will find that we are trying to add a string object ("Source =") to a watersource. This is unacceptable to it because we can only "add" a string to another string, so it says: "I'm going to call ToString () and convert the source to a string!" After this is done, it compiles two strings and passes the result string to a System.out.println (). Each time you create a class that allows this behavior, you only need to write a ToString () method.
If you do not delve into it, you may rashly assume that the compiler will automatically construct the object for each handle in the preceding code (due to Java's security and cautious image). For example, you might think that it would call the default builder for Watersource to initialize source. The output of a print statement is actually:

valve1 = null
valve2 = null
valve3 = null
valve4 = null
i = 0
f = 0.0
Source = null

The basic data that


uses as a field within a class is initialized to 0, as noted in chapter 2nd. However, the object handle is initialized to null. And if you try to call a method for either of them, an "offending" occurs. The results are actually pretty good (and useful), and we can still print them out without discarding one violation. The
compiler does not simply create a default object for each handle, as that can incur unnecessary overhead in many cases. If you want the handle to be initialized, you can do this in the following places:
(1) When the object is defined. This means that they are definitely initialized before the builder calls. The
(2) is in the builder of that class. The
(3) is immediately before the request actually uses that object. Doing so reduces unnecessary overhead-if the object does not need to be created.

Shows you all of these three methods:

 

: Bath.java//constructor initialization with composition class Soap {private String s;
    Soap () {System.out.println ("soap ()");
  s = new String ("constructed");

Public String toString () {return s;}} public class Bath {private String//initializing at point of definition:s1 = new String ("Happy"), S2 =
  "Happy", S3, S4;
  Soap Castille;
  int i;
  float toy;
    Bath () {System.out.println ("Inside Bath ()");
    S3 = new String ("Joy");
    i = 47;
    Toy = 3.14f;
  Castille = new Soap ();
    } void Print () {//delayed initialization:if (S4 = = null) S4 = new String ("Joy");
    System.out.println ("S1 =" + S1);
    System.out.println ("s2 =" + s2);
    System.out.println ("s3 =" + S3);
    System.out.println ("s4 =" + S4);
    System.out.println ("i =" + i);
    System.out.println ("toy =" + toy);
  System.out.println ("Castille =" + Castille);
    public static void Main (string[] args) {Bath b = new Bath (); B.Print (); }
} ///:~

Note that in the Bath builder, a statement is executed before all initialization begins. If you do not initialize at definition, there is still no guarantee that any initialization can be performed before a message is sent to an object handle-unless an unavoidable run-time violation occurs.
The following is the output of the program:

Inside Bath ()
Soap ()
s1 = Happy
s2 = Happy s3
= Joy
S4 = Joy
i =
toy = 3.14 Castille = Constructed

When you call print (), it fills the S4 so that all the fields are initialized correctly before they are used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.