Java Foundation Constructors and overloaded __ block chains

Source: Internet
Author: User

Last blog we talked about the basic concepts of classes and objects, and then we'll talk about constructors and overloads. Presumably everyone has a certain understanding of the constructor, we will discuss the following. One, what is the constructor and the same class name no return value to initialize the instance constructor is always called with the new operator. A constructor can have 0, one, or more parameters.

public class Human {
    private String name;
    This parameterless constructor public
    Human () {

    }
    //This is a parameter constructor public
    Human (String name) {
        this.name = name;
    }
}

In a Java class, if the declaration constructor is not displayed, the JVM gives the class a default parameterless constructor. When a class declares a constructor, the JVM does not assign a default constructor to the class. Of course, a class can have multiple constructors, which are called overloads, as we'll talk about here. Let me give an example to illustrate the function of constructors.

public class Human {
    private String name;
    private int age;
    After we define a parameter constructor, the default parameterless constructor is substituted, so it is generally added to the class with an parameterless constructor public
    Human () {

    }
    //Overload a constructor, which is described below for overloading
    Public Human (String Name,int age) {
        this.name = name;
        This.age = age;
    }
    public static void Main (string[] args) {
        //below we use the New keyword to create an object
        Human human01 = new Human ();
        Human human02 = new Human ("Sun Ding");
        System.out.println ("Human01------->name:" +human01.name+ "Age  :" +human01.age);
        System.out.println ("Human02------->name:" +human02.name+ "Age  :" +human02.age);
    }

The results of the operation are as follows:

Human01------->name:null  age:0
human02------->name: Sun Ding  age:18

The above code is a good illustration of how we typically initialize an object with a parameter constructor, rather than creating an object, which is done with new. Second, the method of overloading

Features of overloading: multiple methods in the same class have the same name, different argument lists, which is called method overload. When overloaded methods are invoked, the Java compiler determines which version of the overloaded method is actually invoked, based on the type and number of arguments. The method overload does not consider the return type of the method. Method overloading is a way for Java to implement an object-oriented polymorphism mechanism.

public class Testoverload {public
    void run () {
        System.out.println ("Test.run (): Method with no parameters");
    public void run (int a) {
        System.out.println ("Test.run (1): Method of a parameter");
    }
    public void run (int a,string b) {
        System.out.println ("Test.run (1,b): Method for two arguments");
    public void Run (String a,int b) {
        System.out.println ("Test.run (a,1) parameter type Order is also possible");
    }
    /* This overload is incorrect because the overload and return type are not related to public
    int run (String a,int b) {
        System.out.println ("not related to the return value type");
    }
    */public
    static void Main (string[] args) {
        testoverload test = new Testoverload ();
        Test.run ();
        Test.run (1);
        Test.run (1, "B");
        Test.run ("A", 1);
    }
}

Run Result:

Test.run (): Method with no parameters
Test.run (1): Method of a parameter
Test.run (1,b): Method of two parameters
Test.run (a,1) parameter type Order of different is also possible
Third, summary

The knowledge of constructors and overloads is very small, and very easy to understand, read the above content, we should have a full understanding of the next I am ready to explain this and super keyword, I hope we pay more attention.

Related Article

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.