overload的一点思考

仅参数类型不同的重载方法,使用过程的一个困惑:

有没有必要使用instanceof方法?

package overload.special;

public class OverLoadTest {
	
	public void test(Object obj){
		System.out.println("Object:"+obj);
	}
	
	public void test(Integer integer){
		System.out.println("Integer:"+integer);
	}
	
	public void test(int int1){
		System.out.println("int:"+int1);
	}
	
	public void test(String str){
		System.out.println("String:"+str);
	}
	
	public static void main(String[] args) {
		OverLoadTest ot=new OverLoadTest();
		ot.test(1);
		
		Integer object_int=new Integer(1);
		ot.test(object_int);
		
		Object obj=object_int;
		ot.test(obj);
		
		ot.test("1");
	}
}

 

优先级:
基本类型>对象
对象中 子类>父类,
其中Object类的优先级最低

 

http://shmilyaw-hotmail-com.iteye.com/blog/1447631

package overload.special;

public class OverLoadTest {
	
	public void test(Object obj){
		System.out.println("Object:"+obj);
	}
	
	public void test(Integer integer){
		System.out.println("Integer:"+integer);
	}
	
	public void test(int int1){
		System.out.println("int:"+int1);
	}
	
	public void test(String str){
		System.out.println("String:"+str);
	}
	
	public static void main(String[] args) {
		OverLoadTest ot=new OverLoadTest();
		ot.test(1);
		
		Integer object_int=new Integer(1);
		ot.test(object_int);
		
		Object obj=object_int;
		if (obj instanceof Integer) {
			ot.test(object_int);
		}else {
			ot.test(obj);
		}
		
		ot.test("1");
	}
}

 

 

posted @ 2014-11-05 17:02  沧海一滴  阅读(249)  评论(0编辑  收藏  举报