一个类中有多个同名的参数不一样的方法。
作用:可以根据不同的条件调用不同的方法。
注意:java不会因为方法的返回类型或者权限的不同而判断为不同的两个方法。
public class Student {public Student() {System.out.println("Student构造方法1");}public Student(int x, float y) {System.out.println("Student构造方法2");}public Student(float x, int y) {System.out.println("Student构造方法3");}// 普通方法public String study() {System.out.println("学习");return "study方法1";}public String study(String subject) {System.out.println("学习" + subject);return "study方法2";}public static void main(String[] args) {Student s1 = new Student();s1.study();s1.study("数学");} }
输出结果:
Student构造方法1
学习
学习数学