Java基础学习笔记(十)—— 对象

Java基础学习笔记(十)—— 对象

I'm the type of person,if you ask me a question, and I don't konw the answer,I'm gonna tell you that I don't konw.But I bet you what: I know how to find the answer, and I'll find the answer.

| @Author:TTODS


对象的创建

一个对象的创建包括两个部分,即声明和实例化。

对象的声明
type name;

type是引用类型,即类,接口,数组。
例如我们在声明一个String类型的对象时,String myName.
注意,对于只声明而没有实例化的对象,我们只是对其分配了一个引用,暂时还没有为对象分配内存空间。

对象的实例化

对象的实例化包括两个部分:①为对象分配内存空间,②对象的初始化。
一般我们使用new关键字为对象分配内存空间,然后调用构造对象的方法初始化对象

String myName;
myName = new String("TTODS.");
package first;

public class Test{
	public static void main(String[] args) {
		String[] strArray = new String[10];
		for(String str:strArray) {
			System.out.println(str);
		} 
	}
}

空对象

对于没有实例化,仅声明的对象,它的值为null;
例如:一个未初始化的String数组,其中的元素的值默认为null

package first;

public class Test{
	public static void main(String[] args) {
		String[] strArray = new String[10]; //声明一个String 数组,但没有对数组中的元素进行初始化
		for(String str:strArray) {
			System.out.println(str);
		} 
	}
}

输出

null
null
null
null
null
null
null
null
null
null

又例如下例中,我们定义了一个Animal类,Animal有一个成员变量name,且其默认的构造方法中没有对name进行初始化。然后我们在Test.java文件中创建一个Animal对象obj,由于obj.name始终没有初始化,所以obj.name的值为null.有时我们需要使用obj==null来判断一个对象obj是否已初始化。

//first包内的Animal.java文件
package first;

public class Animal{
	String name;
	public Animal() {};
	
}
//first包内的Test.java文件
package first;

public class Test{
	public static void main(String[] args) {
		Animal obj = new Animal();
		System.out.println(obj.name);
		if(obj.name==null)
			System.out.println("The name of obj is null");
	}
	
}

输出

null
The name of obj is null

构造方法

构造方法是对象实例化时用来初始化一个对象的属性值的方法,,java中构造方法有三大特点:

  1. 构造方法名必须与类名相同。
  2. 构造方法没有任何返回值,包括void。
  3. 构造方法只能与new运算符结合使用。
默认构造方法

默认构造方法的方法体中内无任何语句,也就是不能初始化成员变量,那么这些成员变量就会使用与它类型对应的默认值。默认构造方法

package first;
public class Animal{
	String name;
	float weight;
	public Animal() {};//默认的无参构造方法
	
	}

值得注意的是,当在类中没有构造方法时,java虚拟机会自动提供一个无参的默认构造函数。如下例中,我们在Animal类中没有写构造方法。但我们仍可以运行Test.java的代码,此时就是使用了无参的默认构造方法。

//first包内的Animal.java文件
package first;

public class Animal{
	String name;
	float weight;
	}
//first包内的Test.java文件
package first;

public class Test{
	public static void main(String[] args) {
		Animal obj = new Animal();
	}
}

但是,当类中存在其他的构造方法时,不会自动生成无参的构造方法,若要使用必须写出。如下例中,我们在Animal中写了一个构造方法public Animal(String n,float w),但是没有写无参的构造方法,然后Test.java文件中使用了new Animal()来构造一个Animal对象,会报错构造函数 Animal()未定义

package first;

public class Animal{
	String name;
	float weight;
	public Animal(String n,float w) {}
	}
package first;

public class Test{
	public static void main(String[] args) {
		Animal obj = new Animal();
	}
}
构造方法的重载

重载方法的名字都是一样的,在下面的例子中,我们为Animal类写了4个构造方法。

package first;

public class Animal{
	String name;
	float weight;
	//默认的无参构造方法
	public Animal() {
		this.name = "暂未命名的神秘生物" ;
		this.weight = 0.0f; //用0.0f 代表重量未知
	}
	//知道名字和重量
	public Animal(String n,float w) {
		this.name = n;
		this.weight = w;
	}
	//只知道重量,不知道名字
	public Animal(float w) {
		this.weight = w;
	}
	//只知道名字,不知道重量
	public Animal(String n) {
		this.name = n;
	}
	}
package first;

public class Test{
	public static void main(String[] args) {
		//神秘动物1:一切未知
		Animal mysteriousAnimal1 = new Animal();
		System.out.printf("mysteriousAnimal1: name:%S , weight:%.2fkg\n",mysteriousAnimal1.name,mysteriousAnimal1.weight);
		//神秘动物2:只知道重量,不知道名字
		Animal mysteriousAnimal2 = new Animal(20.0f);
		System.out.printf("mysteriousAnimal2: name:%S , weight:%.2fkg\n",mysteriousAnimal2.name,mysteriousAnimal2.weight);
		//一只 名叫 "小T" ,重 4.0kg的动物。
		Animal myDog = new Animal("小T",4.0f);
		System.out.printf("myDog: name:%S , weight:%.2fkg\n",myDog.name,myDog.weight);
		//一只只知道 名字叫 "小花",却不知道重量的动物
		Animal yourDog = new Animal("小花");
		System.out.printf("yourDog: name:%S , weight:%.2fkg\n",yourDog.name,yourDog.weight);
		
	}
}

输出:

mysteriousAnimal1: name:暂未命名的神秘生物 , weight:0.00kg
mysteriousAnimal2: name:暂未命名的神秘生物 , weight:20.00kg
myDog: name:小T , weight:4.00kg
yourDog: name:小花 , weight:0.00kg

this关键字

我们在上面的例子中有用到this关键字,java中this关键字和c++中的一样,我们可以只用this来调用对象的成员变量和方法,例如

this.name;
this.toString();

再谈构造方法(this和super语句在构造方法中的使用)

我们在一个类中使用多个构造方法时,可以在一个构造方法内使用this调用该类其他的构造方法。但是如果该构造方法中还有其他的语句,this语句必须是该构造方法中的第一句代码。

package first;

public class Animal{
	String name;
	float weight;
	//知道名字和重量
	public Animal(String n,float w) {
		this.name = n;
		this.weight = w;
	}
	//只知道重量,不知道名字
	public Animal(float w) {
		this("暂未命名的神秘生物",w);
	}
	//只知道名字,不知道重量
	public Animal(String n) {
		this(n,0.0f);
	}
		//默认的无参构造方法
	public Animal() {
		this("暂未命名的神秘生物",0.0f);
	}
	}

类似于this语句在构造方法中的使用方式,我们也可以使用super关键字调用父类中的构造方法,当然super语句也只能是该构造方法的第一句,这里就不多演示了。
还有一点,类的构造方法同样可以使用public,protected,private来修饰,访问级别与普通方法一样。

对象销毁

java语言对象是由垃圾回收器(Garbage Collection)自动收集并释放的,程序员不必关心释放的细节。当一个对象的引用不存在时,该对象就会被释放


- THE END -
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章