zoukankan      html  css  js  c++  java
  • 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");
    	}
    }
    

     

  • 相关阅读:
    1112评论
    1029 C语言文法
    0909编译原理理解和解释
    复利计算4.0-单元测试
    命令解析程序的编写
    《构建之法》1、2、3章思考与感想
    复利计算4.0
    实验三的分析与总结
    复利计算(更新)
    单、复利计算程序
  • 原文地址:https://www.cnblogs.com/softidea/p/4076807.html
Copyright © 2011-2022 走看看