前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 复用类

java 复用类

作者头像
葫芦
发布2019-04-17 14:25:54
4070
发布2019-04-17 14:25:54
举报
文章被收录于专栏:葫芦葫芦
代码语言:javascript
复制
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()
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014/01/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档