方法重载与构造方法重载

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()的构造声明

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值