Overloading of methods

Source: Internet
Author: User

public class Chongzai {
private int m,n;

//overloading of constructor methods, using the This keyword to invoke overloaded other constructor methods
Public Chongzai () {
}

Public Chongzai (int m) {
THIS.M = m;
}

Public Chongzai (int m, int n) {
This (m);
This.n=n;
}


public void SayHello () {
System.out.println ("Hello");
}

//This method differs from the number of first method parameters. Parameter numbers are the same, but different locations are overloaded
public void SayHello (String name) {
System.out.println ("Hello," +name);
}

//This method differs from the parameter type of the second method
public void SayHello (char name) {
System.out.println ("Hello," +name);
}

//NOTE: You cannot use parameter names to differentiate methods,
//NOTE: You cannot use the return value of a method to differentiate overloaded methods, and the return value types of overloaded methods can be different
public int max (int x,int y) {
Return x>y?x:y;
}

public int max (int x,short y) {
System.out.println ("OK");
Return x>y?x:y;
}

public double max (int x,byte y) {
return 0;
}

Public double Max (byte Y,int x) {
return 0;
}

public static void Main (String args[]) {
Chongzai test=new Chongzai ();
Test.sayhello ();
Test.sayhello ("JFL");
Test.sayhello (' A ');
System.out.println (Test.max);
Test.max (1,(short) 2);
}

}

Overloading of methods

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.