2015年3月22日 20:30:59
参见马士兵老师 第三章12讲方法重载与内存分析
Test.java
public class Test {
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);
}
}
<pre name="code" class="java">TestOverLoad。java
public class TestOverLoad {
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);
}
}
对比思考点:
1.
void max(int a , int b) {
System.out.println( a > b ? a : b );
}
//在public中构建函数并有void返回值(无返回)修饰<pre name="code" class="java">Test t = new Test();做引用Test中的方法
和
Person() {
id = 0;
age = 20;
}
/*在class Person非公共类中构造方法,并在 public class中做<pre name="code" class="java"> Person p = new Person()的构造声明