Construction method

defined

In Java, any variables must be set before the initial use, without the explicit assignment, the compiler will be hidden for assignment. Java provides a special method for the class member variables of the initial value.

specificity

constructor is a special member of the method, its specificity is reflected in the following aspects:.

1 constructor function: (1) constructed instances of a class (2) constructed for instance (object) of a class initialization.

2. The method of construction must be the name of his class name exactly defined, there is no return type, not even void.

3. The main object call initialization is completed, the construction method is used when creating a new operator of the object.

4. Class constructor must, if a write, the system automatically adds constructor with no arguments. It must not be instantiated, the constructor does not interface.

5. Can not be static, final, synchronized, abstract and native modification.

6. Constructor object is automatically executed at initialization, generally can not be explicitly called directly. When a plurality of the same class constructor exists, java compiler will automatically follow the rearmost brackets initialization parameters The number and type of parameters automatically correspond. Complete the constructor call. .

7 configured into two methods: with a constructor with no arguments arg constructor

constructor method may be overloaded. No argument constructor called the default constructor, as in the general method, constructor can be any activity, but he often designed to perform various initialization activities, such property is initialized object.

8 configuration code block:.

(1) effect: to initialize the object, the object a build is executed, and in preference to constructor executes

( 2) the difference between structure code blocks and constructors:

configuration code blocks are uniformly initialized to a characteristic common to all the different objects, constructor to the corresponding object is initialized

Construction method

9. subclass inherits the parent class,

*** instantiation process subclass

*** construction method can not be inherited by subclasses

*** promoter when the class creates an object, the object will go to the parent class is created.

is the default constructor with no arguments to call the parent class.

*** subclass constructor method, the first row is the default super ()

*** Why subclasses have a first row default super ()

because he inherited a member of the parent class of use, before using these members must be initialized,

and they are members of the parent class, it must be initialized by the parent class.

So, will first create an object of a parent class.

** no time when the parent class constructor with no arguments

have to use this or other super constructor call.

10. Custom class, if not write constructor, java added by default a constructor with no arguments. If you write a constructor parameter, you must write the constructor with no arguments.

To use a constructor with no arguments, it must be given manually constructor with no arguments.

Comments: In general, we have custom class constructor with no arguments are given manually.

specific use: calculate the length and width of a rectangular area of ​​two 20,10 and 6,3.

 class RectConstructor {double length; double width; double area () {return length * width;} RectConstructor (double width, double length) {// constructor arguments this.length = length; this .width = width;}} public class RectDemo {public static void main (String args []) {RectConstructor rect1 = new RectConstructor (10,20); RectConstructor rect2 = new RectConstructor (3,6); double ar; ar = rect1 .area (); System.out.println ( "a first area of ​​a rectangle is a" + ar); ar = rect2.area (); System.out.println ( "second rectangular area is" + ar) ;}} 

console output is:

is the area of ​​a rectangle 200

is the second area of ​​the rectangle 18

soft channel quotations

constructor:

constructor method is called when a class object is constructed mainly used to instantiate the object.

Related Articles
TOP