03_ java object constructor (construtor)

1. Introduction constructor

The method of construction is also called a constructor (constructor), for performing an initialization operation to the object, the object member variable that is assigned an initial value.

Statement format: [Modifier] class name (parameter list) {}

Precautions:

  1. Constructor class name and the name must be the same, using a large hump naming conventions.

  2. Constructor without defining the type of the return value, the method does not allow body return specific data.

  3. Constructor must be called with the new keyword, it is a special method.

[**] ** novice minefield object is created entirely by the constructor methods to achieve it?

不是。构造方法是创建java对象的重要途径,通过new关键字调用构造器时,构造器也确实返回了该类的对象,但这个对象并不是完全由构造器负责创建的。创建一个对象分为如下四步:
 1、分配对象空间,并将对象成员变量默认初始化。
 2、执行成员变量的显示初始化。
 3、执行构造方法。
 4、返回对象的地址给相关的变量。

2. constructor with no arguments

Not a class constructor, it will provide a default constructor with no arguments.

[Example] constructor with no arguments

class Person {
	String name;
	/*
	默认存在的无参构造方法
	public Person() {}
	*/
}
public class ConstructorDemo {
	public static void main(String[] args) {
		// 采用无参构造方法,来实例化一个对象
		Person p = new Person();
		p.name = "xiaohua";
		System.out.println(“name:” + p.name);
	}
}		

We can also be demonstrated by way of decompiling constructor with no arguments do exist.

Decompile steps:

  1. javac to compile the source code into byte file, Person.java à Person.class

  2. javap decompile operations, javap Person

3. There arg constructor

Which by definition is the constructor with no arguments is added in the form of a certain parameter by parameter to a member variable.

[Example] Reference is added to the Teacher class constructor.

// 老师类
class Teacher {
	// 成员变量
	String name; // 姓名
	int age; // 年龄
	String major; // 专业
	// 有参构造方法
	public Teacher(String name, int age, String major) {
		// 局部变量和成员变量同名,使用this来区分成员变量
		this.name = name;
		this.age = age;
		this.major = major;
	}
	// 成员方法
	void work() {
		System.out.println(name + "在上课");
	}
	void show() {
		System.out.println("name:" + name + " age:" + age + " major:" + major);
	}
}
// 测试类
public class TeacherTest {
	public static void main(String[] args) {
		// 创建一个老师对象
		// Teacher t = new Teacher(); 编译错误,思考为什么???
		Teacher teacher = new Teacher("谭老师", 31, "java讲师");
		// 调用成员方法
		teacher.work();
		teacher.show();
	}
}

Precautions:

  1. If you define a constructor method specified, then the default constructor provided no.

  2. When the member variables of the same name and local variables used to distinguish members of this variable.

  3. Recommendation: Do not omit constructor with no arguments, should have its own non-construction method parameters for each class.

4. constructor overloads

In java, a constructor may be overloaded , overload constructor is a special case of a typical method overloading. When you create an object, JVM will automatically match the constructor form in line with the definition of the class according to the current form of the method call, the constructor executed after a successful match.

[Example] constructor overloads

// 老师类
class Teacher {
	// 成员变量
	String name; // 姓名
	int age; // 年龄
	String major; // 专业
	// 无参构造方法
	public Teacher() {}
	// 有参构造方法
	public Teacher(String name) {
		this.name = name;
	}
	// 有参构造方法
	public Teacher(String name, int age) {
		this.name = name;
		this.age = age;
	}
	// 有参构造方法
	public Teacher(String name, int age, String major) {
		// 局部变量和成员变量同名,使用this来区分成员变量
		this.name = name;
		this.age = age;
		this.major = major;
	}
	// 成员方法
	void work() {
		System.out.println(name + "在上课");
	}
	void show() {
		System.out.println("name:" + name + " age:" + age + " major:" + major);
	}
}
// 测试类
public class TeacherTest {
	public static void main(String[] args) {
		// 创建四个老师对象
		Teacher t1 = new Teacher();
		Teacher t2 = new Teacher("谭老师");
		Teacher t3 = new Teacher("谭老师", 31);
		Teacher t4 = new Teacher("谭老师", 31, "java讲师");		
	}
}

5. The method of construction and method discriminating members

  • A difference: the difference between the definition format

    Constructor method name to the class name, and do not define the type of the return value.

    Methods members only method compliant identifier, you must define the return type.

  • Difference between the two: the difference between calling period

    Constructor called when an object is instantiated.

    Members of the method is called after the object has been created.

  • Three differences: the difference is called

    Constructor is called by the new keyword.

    The method is called by members of the object.

  • Distinguish four: the difference between the number of calls

    Constructor can be called only once, when the call to create the object.
    Members of the method can be called any number of times!

[Learning Exercise]

1, the coordinates define a class (Point), for indicating a position of the two-dimensional coordinate space. By the method of the coordinates of the class to achieve calculates the distance between two coordinate positions (requirements: constructor).

Class member variables coordinates: X-axis point (x), Y-axis point (y)

Coordinate class constructor: Point (double x, double y)

Method coordinate class members: calculating the distance between two coordinate positions (getDistance)

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 35 original articles · won praise 0 · Views 331

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104581575