第五章 初始化和清理
第五章 初始化和清理
初始化变量和清理数据
5.1 用构造器确保初始化
假设为每个类都定义一个initialize方法,该方法提醒你在使用对象的时候,先调用initialize方法。这要求用户必须自己调用该方法。
在Java中,通过构造器,类的设计者可以确保每个对象都会得到初始化。创建对象的时候,类有构造器,Java就会在用户操作对象之前就调用相应的构造器,保证了初始化的进行。
构造器的名称和类相同。
public class Demo{
public static void main(String args[]){
for(int i = 0; i < 10; i++){
Rock rock = new Rock();
}
}
}
class Rock{
Rock(){
System.out.println("Rock");
}
}
创建对象 new Rock(),将会为对象分配存储空间,并调用相应的构造器。这确保了在你能操作对象的时候,他已经初始化了。
构造器的名称和类名相同,方法首字母小写的规则并不适用于构造器。
默认构造器:不接受任何参数的构造器,无参构造器
public class Demo{
public static void main(String args[]){
for(int i = 0; i < 8; i++){
new Rock(i);
}
}
}
class Rock{
Rock(int i){
System.out.println("Rock" + i);
}
}
构造器有了参数之后,就可以在初始化对象的时候提供实际参数。Tree有一个构造器,它接受一个整型变量来表示树的高度
Tree t = new Tree(12);
如果Tree(int)是Tree类中的唯一构造器,那么编译器将不会允许你以任何方式创建Tree对象
构造器有助于减少错误,并且代码更容易阅读。
在Java中初始化和创建捆绑在一起,两者不能分离。
构造器是一种特殊的方法,没有返回值
练习1 :创建一个类,它包含一个未初始化的String引用,验证该引用被Java初始化成null
public class Demo{
static String s;
public static void main(String args[]){
System.out.println(Demo.s);
Demo demo = new Demo();
System.out.println(demo.s);
}
}
创建一个类,它包含一个在定义时候就被初始化了String域,以及另一个通过构造器初始化的String域
public class Demo{
static String s;
public static void main(String args[]){
System.out.println(s);
Demo demo = new Demo("sdfasf");
}
public Demo(String s){
System.out.println(s);
}
}
//构造器里面的数据必须手动进行初始化
5.2 方法重载
当创建一个对象的时候,也就给对象分配到的存储空间娶了一个名字。方法实际上是给某个动作取得名字。
通过名字可以引用所有的名字和方法
方法重载:相同的名字可以表示不同的含义。
在Java中,构造器是强制重载方法名的另一个原因。构造器的名字由类决定,就只能有一个构造器名。但是想用多种方法来构造对象。这就需要多个构造器。但是它们的名字相同,必须用到方法重载。方法重载除了可以用在构造器上,用在其他方法也是可以的。
public class Demo{
public static void main(String args[]){
for(int i = 0; i < 5; i++){
Tree t = new Tree(i);
t.info();
t.info("嘻sdf");
}
new Tree();
}
}
class Tree{
int height;
Tree(){
System.out.println("种一个种子");
height = 0;
}
Tree(int i){
height = i;
System.out.println("种了一个树"+height);
}
void info(){
System.out.println("树高"+height);}
void info(String s ){
System.out.println(s+"树高"+height);}
}
5.2.1 区分重载方法
有几个名字相同的方法,Java如何知道你指的是哪一个?每个重载的方法都必须有一个独一无二的参数类型列表。甚至参数顺序的不同也能区分两个方法,但是一般别这么做。
public class Demo{
public static void main(String args[]){
f(1,"sdfsf");
f("sdfsfdfd",3);
}
static void f(int i, String s){
System.out.println(i +""+ s);
}
static void f(String s, int i){
System.out.println(s +""+ i);
}
}
设计基本类型的重载
基本类型从一个较小的类型自动提升为一个较大的类型,
public class Demo{
public static void main(String args[]){
}
void f1(char x) {
System.out.println("f1(char)");}
void f1(byte x) {
System.out.println("f1(byte)");}
void f1(short x) {
System.out.println("f1(short)");}
void f1(int x) {
System.out.println("f1(int)");}
void f1(long x) {
System.out.println("f1(long)");}
void f1(float x) {
System.out.println("f1(float)")}
void f1(double x) {
System.out.println("f1(double)");}
void f2(byte x){
System.out.println("f2(byte)");}
void f2(short x){
System.out.println("f2(short)");}
void f2(int x){
System.out.println("f2(int)");}
void f2(long x){
System.out.println("f2(long)");}
void f2(float x){
System.out.println("f2(float)");}
void f2(double x){
System.out.println("f2(double)");}
void f3(short x){
System.out.println("f3(short)");}
void f3(int x){
System.out.println("f3(int)");}
void f3(long x){
System.out.println("f3(long)");}
void f3(float x){
System.out.println("f3(float)");}
void f3(double x){
System.out.println("f3(double)");}
void f4(int x){
System.out.println("f4(int)")}
void f4(long x){
System.out.println("f4(long)")}
void f4(float x){
System.out.println("f4(float)");}
void f4(double x){
System.out.println("f4(double");}
void f5(long x){
System.out.println("f5(long)");}
void f5(float x){
System.out.println("f5(float)");}
void f5(double x){
System.out.println("f5(double)")}
void f6(float x){
System.out.println("f6(float)");}
void f6(double x){
System.out.println("f6(double)");}
void f7(double x){
System.out.println("f7(double)");}
void test1(){
System.out.println("5");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5); // int,int,int,int,long,float,double
}
void test2(){
char x = 'x';
System.out.println("char");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);// char,byte,short,int,long,float,double
// char,int,int ,int ,long ,float,double
//如果没有char类型,会直接提升为int类型
}
void test3(){
byte x = 0;
System.out.println("byte");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);//byte,byte,short,int,long,float,double
}
void test4(){
short x = 0;
System.out.println("short");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);//short,short,short,int,long,float,double
}
void test5(){
int x = 0;
System.out.println("int");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);//int,int,int,int,long,float,double
}
void test6(){
long x = 0;
System.out.println("long");
f1(x);f2(x);f3(x);f4