java--Object-oriented Advanced (construction method, This,super)

Source: Internet
Author: User

Construction Method

In development, it is often necessary to define the object's property value while creating the object, such as the employee's entry company should be clear about his name, age and other attributes.

The construction method is the method used to construct the creation, that is, the method to be executed 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:

modifier constructor Method name ( parameter list ) {... }

    • 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.

Class Person {//person's member property age and Nameprivate int age;private String name;//Person constructor method, with parameter list person (int A, String nm) {// Accept the value passed in when the object was created, assign the value to the member property age = A;name = NM;}}

Note:

    • If we do not write the constructor method, the compiler will automatically add the default constructor method (public class name () {}) to the class file. If we show that the constructor is specified when describing the class, the compiler will no longer add a default constructor to the class file when compiling the Java source file.
    • The construction method executes when the object is created and executes only once.

    • There can be more than one construction method in a class, and multiple construction methods exist in the form of overloading.
    • The constructor method can be modified by private : Other programs cannot create objects of that class.
class Person {private int age;private String name;//A private parameterless constructor, that is, the outside world cannot create this class object by using the new person (); the private person () {}//Multiple constructor methods are exists as an overloaded person (int a) {age = A;} Person (String nm, int a) {name = Nm;age = A;}}
This keyword

Calls between multiple construction methods can be done through the This keyword.

Class Person {//person's member property private int age;private string name;//No parameter constructor method person () {}//constructor method for name initialization person (String nm) {Nam e = NM;} Constructor for name and age initialization person (String nm, int a) {//Because there is already a constructor method that initializes the name, name = NM; Therefore, only calls can//calls other constructor methods, this keyword is required to call this (NM) ;//Initialize Age = A;}}

When the local variable and the member variable have the same name in the method, precede the member variable name with this . to distinguish between member variables and local variables

class Person {private int age;private String name;//constructor for name and age initialization person (String name, int ages) {///when access to a member variable is required, simply precede the member variable with the On this. can this.name = Name;this.age = age;} public void Speak () {System.out.println ("name=" + THIS.name + ", age=" + This.age);}} Class Persondemo {public static void main (string[] args) {person p = new person ("Zhang San", "");p. Speak ();}}
Super keyword

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 constructor of in this class: this ( Argument list );

calls the Null argument construction method in parent class : super ();

to call a parameterized constructor method in parent class : super ( argument list

When you create a subclass object, you access the constructor method in the parent class, because the subclass inherits the contents of the parent class, so when you create the object, you must first see how the parent class initializes its contents.

public class Test {public static void main (string[] args) {new Zi ();}} Class Fu{int num; Fu () {System.out.println ("Fu construction Method" +num); num = 4;}} Class Zi extends Fu{zi () {         ///super (); Call the parent class Null argument construction method System.out.println ("Zi constructor Method" +num);}}

Execution Result:

  Fu Construction method 0//Because the parent class constructor method is executed first

  Zi Construction method 4

question and answer zone

1, if the subclass of the construction method of the first line to write this call this class other construction methods, then Super call the parent class statement still have?

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.

2. Is there an implicit super in the parent-class construction method?

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

3. Who is the parent of the parent class? Who is the constructor of Super call?

The Java system is designed to define a parent class object for all objects.

java--Object-oriented Advanced (construction method, This,super)

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.