TestOverLoad.java
class="java" name="code">//方法的重载 //方法名相同,返回类型相同,方法参数不同 public class TestOverload { void max(int a , int b) { System.out.println( a > b ? a : b ); } void max(short a , short b) { System.out.println("short"); System.out.println( a > b ? a : b ); } void max(float a, float b) { System.out.println( a > b ? a : b ); } public static void main(String[] args) { Test t = new Test(); t.max(3, 4); short a = 3; short b = 4; t.max(a, b); } }
TestOverLoad.java
//构造方法的重载 public class TestOverLoad2 { public static void main(String[] args) { Person p = new Person(); Person p1 = new Person(400); Person p2 = new Person(2, 500); p.info(); p.info("ok"); } } class Person { //构造方法的重载 Person() { id = 0; age = 20; } Person(int _id) { id = _id; age = 23; } Person(int _id, int _age) { id = _id; age = _age; } //成员变量定义 private int id; private int age = 20; //方法定义 public int getAge() {return age;} public void setAge(int i) {age = i;} public int getId() {return id;} //方法的重载 void info() { System.out.println("my id is : " + id); } void info(String t) { System.out.println(t + " id " + id); } }
?