java之 惰性初始化
2024-10-09 00:04:17
class Soap {
private String s;
Soap(){
System.out.println("Soap()");
s="Constructed"; }
public String toString() {
return s; }
}
class Bath {
private String
s1 ="happy",
s2 = "happy",
s3,s4;
private Soap castille;
private int i;
private float toy;
public Bath() {
System.out.println("Inside Bath()");
s3 = "Joy";
toy = 3.14f;
castille = new Soap(); }
{
i=47;
}
public String toString(){
if(s4==null)
s4 = "Joy";
return
"s1 = " + s1 +"\n"+
"s2 = " + s2 +"\n"+
"s3 = " + s3 +"\n"+
"s4 = " + s4 +"\n"+
"i = " + i +"\n"+
"toy = " + toy +"\n"+
"castille =" + castille;
} public static void main(String[] args) {
Bath b = new Bath();
System.out.println(b);
}
}
输出
Inside Bath()
Soap()
s1 = happy
s2 = happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille =Constructed
来自thinking in java
最新文章
- Erlang垃圾回收机制的二三事
- iOS 钥匙串 指纹识别 get和Post请求的区别
- UnionPay,ChinaPay 最新 银联支付接口C#\Asp.net\MVC 版本
- 【循序渐进学Python】11.常用标准库
- Linux平台Makefile文件的编写基础入门(课堂作业)
- PHP中的文件下载
- python练习程序(c100经典例21)
- HIVE Transform using 用法
- 4k 对齐,你准备好了吗?
- DBCONN
- 基于OSGi的企业级快速开发平台(开源)
- 《Java并发编程实战》第十四章 构建自己定义的同步工具 读书笔记
- SQL__用命令删除定期的备份数据库文件
- PHP CodeBase: 生成N个不重复的随机数
- python基础整理----基本概念和知识
- sql server 高可用故障转移(2)
- newJob_newFell
- [luogu3455][POI2007]ZAP-Queries【莫比乌斯反演】
- 使用MyEclipse建立working set
- T550 HiDPI Ubuntu 16.04安装流水帐