浏览量 3865
2014/01/31 02:07
java 组合语法
package javahaonan;
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.print(sprinklers);
}
}
/*
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
*/
toString()每一个非基本类型的对象都有一个toSting的方法,当编译器需要一个String时而你却只有一个对象时,该方法就会被调用。"source = " + source 是将一个source对象 "source=“同WaterSource相加。由于只能将一个 String同另一个 String相加,因此会调用WaterSource的toString(),把source转换成为一个sting。而后将两个string连接到一起并传给System.out.print()。基本类型会被初始化为零,对象的饮用会被初始化为null。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javahaonan;
import static javahaonan.Print.*;
class Soap{
private String s ;
Soap(){
print("Soap()");
s= "Constructed";
}
public String toString(){return s;}
}
public class Bath {
private String s1 ="Happy" , s2 ="sd" ,s3 ,s4 ;
private Soap castille;
private int i ;
private float toy ;
public Bath(){
print("Inside Bath()");
s3= "Joy";
toy=3.14f;
castille= new Soap();
}
{i=47;}
public String toString(){
if(s4 ==null)
s4= "Joy";
return
"s1 ="+s1 + " " +
"s2 ="+ s2 + " " +
"s3 ="+ s3 + " " +
"s4 ="+ s4 + " " +
"i= "+ i + " " +
"toy="+ toy + " " +
"castille="+ castille ;
}
public static void main(String[] args) {
Bath b = new Bath();
print (b);
}
}
/*
Inside Bath()
Soap()
s1 =Happy s2 =sd s3 =Joy s4 =Joy i= 47 toy=3.14 castille =Constructed
*/
四种初始化引用位置的示例,1、private String s1="Happy"在定义对象的地方初始化。2、在类构造其中初始化 s= "Constructed" ;。3、在使用对象之前初始化,惰性初始化Delayed initialization if (s4 == null) s4= "Joy" ;4、使用实例初始化{ i=47;}。
print(b); 将return值传递给 print方法。
"castille=" + castille ;当我需要将一个String对象却只有一个对象时,会调用Soap 中的 toString()方法。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
class A{}
public class VarArgs {
static void printArray(Object[] args){
for(Object obj : args)
System. out.print(obj+" " );
System. out.println();
}
public static void main(String[] args) {
printArray(new Object[]{
new Integer(47),new Float(32.12),332.2,new Double(3.321)
});
printArray(new Object[]{
"one","wang" ,"zi"
});
printArray(new Object[]{
new A(),new A(),new A()
});
}
}/*
47 32.12 332.2 3.321
one wang zi
javat.A@653cca0e javat.A@79f5910e javat.A@69066caf
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import static javat.Print.*;
class Soap {
private String s ;
Soap(){
print("Soap()");
s= "Constructed";
}
public String toString() {return s;}
}
public class Bath {
private String 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();
}
{i=4;}
public String toString(){
if(s4 ==null)
s4= "Joy";
return
"s1="+ s1 +"\n" +
"s2="+ s2 + "\n" +
"s3="+ s3 + "\n"
+ "i="+i +"\n" +
"toy="+toy +"\n" +
"castille="+ castille ;
}
public static void main(String[] args) {
print("wangzi");
Bath b= new Bath();
print(b);
}
}
/*
wangzi
Inside Bath()
Soap()
s1=Happy
s2=Happy
s3=Joy
i=4
toy=3.14
castille=Constructed
*/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package javat;
import static javat.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{
public void scrub(){
append( "Detergent.scrub()");
super.scrub();
}
public void foam() {append("foam()");}
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);
}
}
/*
Cleanserdilute()apply()Detergent.scrub()scrub()foam()
Testing base class:
Cleanserdilute()apply()scrub()
*/
上一篇 搜索 下一篇