The method of construction and the conventional method

Construction method:

Construction method, also known as the constructor . Can be constructed by instances of the class constructor, in fact, is the object.

Format: Modifier + class name (parameter list)

1. The name of the constructor class name must be the same, no return type.

2. The method of construction can not use the return statement to return a value.

3. When creating an object need new new , Example: Note note = new Note () ;

4. The method of construction is not specified, the system will automatically add the constructor with no arguments.

The construction method is also overloaded.

6. The presence of the specified constructor, regardless of whether the constructor or no-argument is a reference, the system will not automatically add a constructor with no arguments.

No-argument constructor
class Person {
public Person () {
System.out.println ( "no-argument constructor is called ...");
}
}
class Ex 11 {
public static void main (String [] args) {
Person P = new new Person ();
}
}
with a reference constructor (the purpose is to give the object instance variable assignment)
class Person {
int Age;
public Person (int a) {
Age = a;
}
public void Speak () {
the System .out.println ( "the I AM" + Age + "Old years");
}
}
class Ex 11 {
public static void main (String [] args) {
Person Person new new P = ();
p.speak ();
}
}

7. The method may be configured to initialize the class attributes.

如:public people(String name){

this.name=name;

}

or

public people(){

name="zhangsan";

age=11;

} Initialization is completed people of age or class attribute name

8. parent class constructor can not be inherited by subclasses, can only be called by subclasses

    The reason: Other functions can be invoked through the class object, but the constructor function is used to create an object, the object before it.

The inheritance of the object is able to call the parent class, but the object does not exist you what the parent class constructor also call? and so

The constructor can not be inherited.

 

Common methods:

1. have a return type.

2. The method name can not be the same as the class name.

3. The method name is lower case.

Common methods used to describe the functional behavior of the object, the object constructor creates and initializes a value of the object.

 

 

Guess you like

Origin www.cnblogs.com/xing-29391/p/12069407.html