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");
    	}
    }
    

     

  • 相关阅读:
    MySQL性能调优my.cnf详解
    PHP调试工具Xdebug安装配置教程
    【转载】Zend Studio 10正式版注册破解
    SQL 大全(3)
    SQL 大全(1)
    JQuery方法
    修改 C:Users[account name] 目录名称
    wordmate 词典安装
    Python3 之 import 和 当前目录
    初遇 dotcloud
  • 原文地址:https://www.cnblogs.com/softidea/p/4076807.html
Copyright © 2011-2022 走看看