One of the most compelling features about Java is code reuse.But to be revolutionary,you've got to be able to do a lot more than copy code and change it.
Composition syntax
重写toString()方法的例子:
//: reusing/SprinklerSyste.java
//Composition for code reuse.
class Watersource{
private String s;
WaterSource(){
System.out.println("WaterSource()");
s = "Constructed";
}
public String toString(){return s;}
}
public class SprinklerSystem{
private String valve1,valve2,valve3,valve4;
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString(){
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "/n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source;
}
public static void main(String[] args){
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
}
输出结果:
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
Anytime you want to allow this behavior with a class you create,you need only write a toString() method.
It makes sense that the compiler doesn't just create a default object for every reference,because that would incur unnessary overhead in many cases.If you want the references initialized,you can do it:
1.At the point the objects are defined.This means that they'll always be initialized before the constructor is called.
2.In the constructor for that class.
3.Right before you actually need to use the object.This is often called lazy initialization.It can reduce overhead in situations where object creation is expensive and the object doesn't need to be created every time.
4.Using instance initialization.
示例:
//:reusing/Bath.java
//Constructor initialization with composition.
import static net.mindview.util.Print.*;
class Soap{
private String s;
Soap(){
print("Soap()");
s = "Constructed";
}
public String toString(){return s;}
}
public class Bath{
private String // Initializing at point of definition:
s1 = "Happy",
s2 = "Happy",
s3,s4;
private Soap castille;
private int i;
private float toy;
public Bath(){
print("Inside Bath()");
s3 = "Joy";
toy = 3.14f;
castille = new Soap();
}
// Instance initialization:
{ i = 47; }
public String toString(){
if(s4 == null) //Delayed initialization:
s4 = "Joy";
return
"s1 = " + s1 + "/n" +
"s2 = " + s2 + "/n" +
"s3 = " + s4 + "/n" +
"s4 = " + s4 + "/n" +
"i = " + i + "/n" +
"toy = " + toy + "/n" +
"catille = " + castille;
}
public static void main(String[] args){
Bath b = new Bath();
print(b);
}
}
输出结果:
Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed
Inheritance syntax
Inheritance is an integral part of Java(and all OOP languages).It turns out that you're always doing inheritance when you create a class,because unless you explicityly inherit from some other class,you implicitly inherit from Java's standard root class Object.
示例:
//:reusing/Detergent.java
//Inheritance syntax & properties.
import static net.mindview.util.Print.*;
class Cleanser{
private String s = "Cleanser";
public void append(String a ){s += a;}
public void dilute(){append(" dilute()");}
public void apply(){append(" apply()");}
public void scrub(){append(" scrub()");}
public String toString(){return s;}
public static void main(String[] args){
Cleanser x = new Cleanser();
x.dilute();x.apply();x.scrub();
print(x);
}
}
public class Detergent extends Cleanser{
//Change a method:
public void scrub(){
append(" Detergent.scrub()");
super.scrub();//Call base-class version
}
//Add methods to the interface:
public void foam(){append(" foam()");}
//Test the new class:
public static void main(String[] args){
Detergent x = new Detergent();
x.dilute();
x.apply();
x.scrub();
x.foam();
print(x);
print("Testing base class:");
Cleanser.main(args);
}
}
输出结果:
Cleanser dilute() apply() Detergent.scrub() scrub() foam()
Testing base class:
Cleanser dilute() apply() scrub()
This demonstrates a number of features.First,in the Cleanser append() method,Strings are concatenated to s using the += operator,which is one of the operators(along with '+')that the Java designers"overloaded"to work with Strings.
Second,both Cleanser and Detergent contain a main() method.You can create a main() for each one of your classes;this technique of putting a main() in each class allows easy testing for each class.And you don't need to remove the main() when you're finished;you can leave it in for later testing.
...as a general rule make all fields private and all methods public.
(待续)