您的位置:首页 > 编程语言 > Java开发

【ThinkingInJava】8、对象的初始化为null,基本类型默认初始化为0

2015-04-14 10:36 375 查看
/**
* 书本:《Thinking In Java》
* 功能:对象的初始化为null,基本类型默认初始化为0
* 		1、在定义对象的地方。
* 		2、在类的构造器中
* 		3、在使用这些对象之前,这个叫惰性初始化
* 		4、使用实例初始化
* 文件:Bath.java
* 时间:2014年10月12日15:21:17
* 作者:cutter_point
*/
package Lesson7ReusingClasses;

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 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=47; }		//这个初始化,略屌
	public String toString()
	{
		if(s4 == null)
			s4="cutter_point";
		return 	"s1="+s1+"\n"+
				"s2="+s2+"\n"+
				"s3="+s3+"\n"+
				"s4="+s4+"\n"+
				"i="+i+"\n"+
				"toy="+toy+"\n"+
				"castille="+castille+"\n";
	}
	
	public static void main(String[] args)
	{
		Bath b=new Bath();
		print(b);
	}
}


输出:

Inside Bath() obj1

Soap() obj1

s1=Happy

s2=Happy!

s3=Joy

s4=cutter_point

i=47

toy=3.14

castille=Constructed

obj1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐