Java Basics-Object oriented (4)

Source: Internet
Author: User

The first chapter constructs the method

The method to execute when the object is created. Since it is the method to be executed when the object is created, it is possible to assign a property value to the object at the time it is executed, as long as the new object is aware of what it is doing to construct the method.

Format of the construction method:

Modifier Constructor method name ( parameter list ){

}

The embodiment of the construction method:

The construction method does not return a value type. You do not need to write a return value. Because it is for building objects, the object is created, and the method executes the end.

The construction method name must be consistent with the type.

The constructor method does not have a specific return value.

The code embodiment of the construction method:

class Person {    //  Person's member property age and name    privateint age ;     Private String name;     // constructor method for person, with parameter list    Person (int  A, String nm) {        //  accepts the value passed in when the object was created, assigns the value to the member property, age        = A;         = nm;    }}

Details of the construction method:

1, a class can have more than one construction method, more than one construction method exists in the form of overloading

2, the construction method can be modified by private, function: Other programs cannot create objects of this class.

 class   person { private  int   age;     private   String name;  //  Private parameterless construction method where the outside world cannot pass new person (); Statement to create this class object  private   person () {}  
   
    // 
     Multiple construction methods exist as overloaded  person (
    int  
     a) {age  =
     A; } person (String nm,  
    int  
     a) {name =
     nm;    Age  =
     A; }}
   

The construction method executes when the object is created and executes only once. The general method is called by the object after it is created, and can be called multiple times.

Chapter II This keyword 2.1 This call construction method

To construct a method call format:

This ( parameter list );

Call to construct method

classPerson {//member properties for person    Private intAge ; PrivateString name; //method of constructing without parametersPerson () {}//constructor method for name initializationPerson (String nm) {name=nm; }    //constructor method for name and age initializationPerson (String nm,inta) {//because the constructor that initializes the name already exists name = NM;//call other constructor methods, you need to call through the This keyword         This(NM); //to initialize the age ofAge =A; }}
2.2 member variable and local variable have the same name problem

When the local variable and the member variable have the same name in the method, you can precede the member variable name with this. To distinguish member variables from local variables.

classPerson {Private intAge ; PrivateString name; //constructor method for name and age initializationPerson (String name,intAge ) {        //when you need to access a member variable, just precede the member variable with this.         This. Name =name;  This. Age =Age ; }     Public voidspeak () {System.out.println ("Name=" + This. Name + ", age=" + This. Age); }}classPersondemo { Public Static voidMain (string[] args) {person P=NewPerson ("Zhang San", 23);    P.speak (); }}
Chapter Three super keywords call to construct method in 1.1 child parent class

When you create a subclass object, the constructor of the parent class executes first, because the first row of all constructed methods in the subclass has the default implicit super () statement.

Call a constructor method in this class

This ( argument list );

Calling an empty parameter construction method in the parent class

Super ();

Calling a parameterized construction method in a parent class

Super ( argument list );

Why do subclass object creation have access to the constructor method in the parent class? Because subclasses inherit the contents of a parent class, when you create an object, you must first see how the parent class initializes its content.

 Public classTest { Public Static voidMain (string[] args) {NewZi (); }    }classfu{intnum; Fu () {System.out.println ("Fu Construction Method" +num); Num= 4; }}classZiextendsfu{Zi () {//super (); Call the parent class Null argument construction methodSystem.out.println ("Zi construction method" +num); }}

The result shows that when the subclass constructs the method execution, the parent class constructor method is called, which indicates that there is a super () in the subclass construction method.

So why does a constructor in a subclass have an implicit super ()?

Cause: Subclasses inherit content from the parent class, so when the subclass initializes, it must first go to the parent class to perform the initialization action of the parent class. In this way, you can use the contents of the parent class.

When there are no empty arguments in the parent class to construct the method, the subclass's constructor must have a super statement displayed, specifying that the parent class to access has a parameter constructor method.

If the subclass's constructor method first writes this call to the other constructor methods of this class, then does Super call the statement of the parent class still there?

This is not the case because this () or super () can only be defined in the first line of the constructor , because the initialization action is performed first.

Is there an implicit super in the parent-class constructor method?

There are. Remember: The first line is super () as long as the constructor method is the default;

Attention:

Constructor method in a class by default the first row has an implicit super () statement that accesses an empty argument in the parent class constructor method. So the constructor of the parent class can either initialize its own object or initialize its own subclass object.

If the default implicit super () statement does not have a corresponding construction method in the parent class, you must explicitly define the constructor to invoke in the constructor method by this or super.

Java Basics-Object oriented (4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.