java对象构造

对于java对象的构造:总的来说有6种方法,第一种是有参数的构造函数,第二种是无参数的构造函数,第三种是重载构造函数,第四种是调用其他构造器的构造函数,第五种是使用初始化块,第六种是使用显式域初始化

下面就一一介绍这几种方法并给出示例,最后还要说明一下一些需要注意的方面:

第一种:最普通的一种,有参数的构造函数

示例代码:

package InitTest;

import static java.lang.System.*;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee Amy=new Employee("Amy",3000.0);
		out.println("员工"+Amy.getName()+"的薪水为每月"+Amy.getSalary()+"元");
	}
}

class Employee{
	private String name;
	private double salary;
	
	public Employee(String aName,double aSalary) {
		name=aName;
		salary=aSalary;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public void setSalary(double aSalary) {
		salary=aSalary;
	}
}

运行结果:


第二种:无参数的构造函数

示例代码:

package InitTest;

import static java.lang.System.*;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee aEmployee=new Employee();
		out.println("员工"+aEmployee.getName()+"的薪水为每月"+aEmployee.getSalary()+"元");
	}
}

class Employee{
	private String name;
	private double salary;
	
	public Employee() {
		name="aEmployeeName";
		salary=0.0;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public void setSalary(double aSalary) {
		salary=aSalary;
	}
}

运行结果:


第三种:重载构造函数

示例代码:

package InitTest;

import static java.lang.System.*;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee aEmployee=new Employee();
		out.println("员工"+aEmployee.getName()+"的薪水为每月"+aEmployee.getSalary()+"元");
		Employee Amy=new Employee("Amy",3000.0);
		out.println("员工"+Amy.getName()+"的薪水为每月"+Amy.getSalary()+"元");
	}
}

class Employee{
	private String name;
	private double salary;
	
	public Employee() {
		name="aEmployeeName";
		salary=0.0;
	}
	
	public Employee(String aName,double aSalary) {
		name=aName;
		salary=aSalary;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public void setSalary(double aSalary) {
		salary=aSalary;
	}
}

运行结果:


第四种:调用其他构造器的构造函数

package InitTest;

import static java.lang.System.*;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee aEmployee=new Employee();
		out.println("员工"+aEmployee.getName()+"的薪水为每月"+aEmployee.getSalary()+"元");
		Employee Amy=new Employee("Amy",3000.0);
		out.println("员工"+Amy.getName()+"的薪水为每月"+Amy.getSalary()+"元");
		Employee John=new Employee("John",4000.0,"A组组长");
		out.println("员工"+John.getName()+"的薪水为每月"+John.getSalary()+"元,备注:"+John.getDescription());
	}
}

class Employee{
	private String name;
	private double salary;
	private String description;
	
	public Employee() {
		name="aEmployeeName";
		salary=0.0;
	}
	
	public Employee(String aName,double aSalary) {
		name=aName;
		salary=aSalary;
	}
	
	public Employee(String aName,double aSalary,String aDescription) {
		this(aName,aSalary);
		description=aDescription;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public void setSalary(double aSalary) {
		salary=aSalary;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String aDescription) {
		name=aDescription;
	}
}

运行结果:


第五种:使用初始化块

package InitTest;

import static java.lang.System.*;
import java.util.Random;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee aEmployee=new Employee();
		out.println("员工"+aEmployee.getName()+"的薪水为每月"+aEmployee.getSalary()+"元");
		out.println("目前id:"+Employee.getRandomPointer());
		Employee Amy=new Employee("Amy",3000.0);
		out.println("员工"+Amy.getName()+"的薪水为每月"+Amy.getSalary()+"元");
		out.println("目前id:"+Employee.getRandomPointer());
		Employee John=new Employee("John",4000.0,"A组组长");
		out.println("员工"+John.getName()+"的薪水为每月"+John.getSalary()+"元,备注:"+John.getDescription());
		out.println("目前id:"+Employee.getRandomPointer());
	}
}

class Employee{
	
	private static int randomPointer;
	{
		Random generator=new Random();
		randomPointer=	generator.nextInt();
	}
	
	private String name;
	private double salary;
	private String description;
	
	public Employee() {
		name="aEmployeeName";
		salary=0.0;
	}
	
	public Employee(String aName,double aSalary) {
		name=aName;
		salary=aSalary;
	}
	
	public Employee(String aName,double aSalary,String aDescription) {
		this(aName,aSalary);
		description=aDescription;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public double getSalary() {
		return salary;
	}
	
	public void setSalary(double aSalary) {
		salary=aSalary;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String aDescription) {
		name=aDescription;
	}
	
	public static int getRandomPointer() {
		return randomPointer;
	}
}

运行结果:


第六种:

示例代码:

package InitTest;

import static java.lang.System.*;
import java.util.Random;

public class EmployeeTest {

	public static void main(String[] args) {
		Employee aEmployee=new Employee();
		out.println("员工"+aEmployee.getName()+",描述:"+aEmployee.getDescription());
		
	}
}

class Employee{
	
	private String name;
	private String description="aDescription";
	
	public Employee() {
		name="aEmployeeName";
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String aName) {
		name=aName;
	}
	
	public String getDescription() {
		return description;
	}
}

运行结果:



以下是几点注意事项:

(1)如果在构造器中没有显式的给域赋予初值,那么就会自动地赋为默认值:数值为0,布尔值为false,对象引用为null

(2) 如果初始化块使用的是static,则只执行一次

(3)下面是java中调用构造器的具体处理步骤:

1.所有数据被初始化为默认值(0,false,null)

2.按顺序执行域初始化语句和初始化块

3.执行构造器,如果构造器里面使用了其他构造器,则首先执行里面的构造器。

(4)另外还有一个finalize方法用于销毁对象,但是由于java的垃圾回收机制相当完善,因此这个方法并不常用




展开阅读全文

没有更多推荐了,返回首页