zoukankan      html  css  js  c++  java
  • 基本类型的重载规则

    package overloading.main;
    //基本类型的重载
    public class Main {
    
        void f1(char x){
            System.out.print("f1(char x) ");
        }
        void f1(byte x){
            System.out.print("f1(byte x) ");
        }
        void f1(short x){
            System.out.print("f1(short x) ");
        }
        void f1(int x){
            System.out.print("f1(int x) ");
        }
        void f1(long x){
            System.out.print("f1(long x) ");
        }
        void f1(float x){
            System.out.print("f1(float x) ");
        }
        void f1(double x){
            System.out.print("f1(double x) ");
        }
    
    
        void f2(byte x){
            System.out.print("f1(byte x) ");
        }
        void f2(short x){
            System.out.print("f1(short x) ");
        }
        void f2(int x){
            System.out.print("f1(int x) ");
        }
        void f2(long x){
            System.out.print("f1(long x) ");
        }
        void f2(float x){
            System.out.print("f1(float x) ");
        }
        void f2(double x){
            System.out.print("f1(double x) ");
        }
    
    
        void f3(short x){
            System.out.print("f1(short x) ");
        }
        void f3(int x){
            System.out.print("f1(int x) ");
        }
        void f3(long x){
            System.out.print("f1(long x) ");
        }
        void f3(float x){
            System.out.print("f1(float x) ");
        }
        void f3(double x){
            System.out.print("f1(double x) ");
        }
    
        void f4(int x){
            System.out.print("f1(int x) ");
        }
        void f4(long x){
            System.out.print("f1(long x) ");
        }
        void f4(float x){
            System.out.print("f1(float x) ");
        }
        void f4(double x){
            System.out.print("f1(double x) ");
        }
    
        void f5(long x){
            System.out.print("f1(long x) ");
        }
        void f5(float x){
            System.out.print("f1(float x) ");
        }
        void f5(double x){
            System.out.print("f1(double x) ");
        }
    
        void f6(float x){
            System.out.print("f1(float x) ");
        }
        void f6(double x){
            System.out.print("f1(double x) ");
        }
    
        void f7(double x){
            System.out.print("f1(double x) ");
        }
    
        void testChar(){
            System.out.println("testChar()...");
            char c='a';
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testByte(){
            System.out.println("testByte()...");
            byte c=0;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testShort(){
            System.out.println("testShort()...");
            short c=0;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testInt(){
            System.out.println("testInt()...");
            int c=0;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testLong(){
            System.out.println("testLong()...");
            long c=0L;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testFloat(){
            System.out.println("testFloat()...");
            float c=0;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        void testDouble(){
            System.out.println("testDouble()...");
            double c=0;
            f1(c);f2(c);f3(c);f4(c);f5(c);f6(c);f7(c);
            System.out.println();
        }
        public static void main(String[] args) {
            Main m=new Main();
            m.testChar();
            m.testByte();
            m.testShort();
            m.testInt();
            m.testLong();
            m.testFloat();
            m.testDouble();
            /* 输出结果:
             * testChar()...
             * f1(char x) f1(int x) f1(int x) f1(int x) f1(long x) f1(float x) f1(double x)
             * testByte()...
             * f1(byte x) f1(byte x) f1(short x) f1(int x) f1(long x) f1(float x) f1(double x)
             * testShort()...
             * f1(short x) f1(short x) f1(short x) f1(int x) f1(long x) f1(float x) f1(double x) 
             * testInt()...
             * f1(int x) f1(int x) f1(int x) f1(int x) f1(long x) f1(float x) f1(double x)
             * testLong()...
             * f1(long x) f1(long x) f1(long x) f1(long x) f1(long x) f1(float x) f1(double x)
             * testFloat()...
             * f1(float x) f1(float x) f1(float x) f1(float x) f1(float x) f1(float x) f1(double x) 
             * testDouble()...
             * f1(double x) f1(double x) f1(double x) f1(double x) f1(double x) f1(double x) f1(double x) 
             */
        }
    
    }
    

    注:基本类型重载可以看成是占用内存大的类型是占用内存小的类型的父类,在重载的时候默认自动向上造型至离自己最近的父类(下面代码验证向上造型调用离自己最近的父类型参数方法),

    package overloading.main;
    
    public class SubAndParent {
        /*
         * 该类有两个重载方法overloading,分别含参Integer和Object,
         * 其中Object是Integer的父类,当输入参数为Integer时会调用参
         * 数类型为Integer的方法,当输入参数类型为Object时,会调用参数
         * 类型为Object的方法,从而验证编译器会默认调用离自己最近的父类或者
         * 同类型参数的方法。
         */
        public void overloading(Integer intg) {
            System.out.println("overloading(Integer intg)");
        }
    
        public void overloading(Object obj) {
            System.out.println("overloading(Object obj)");
        }
    
        public static void main(String[] args) {
            SubAndParent sp = new SubAndParent();
            sp.overloading(1);
            sp.overloading(new Integer(1));
            sp.overloading((Object) (new Integer(1)));
            /*
             * Output:
             * overloading(Integer intg)
             * overloading(Integer intg)
             * overloading(Object obj)
             */
        }
    }
    

    其中,char有点区别,如果不存在char类型重载方法,则char自动转换为int再遵守基本类型的向上造型原则(见testChar()用例)。

  • 相关阅读:
    Pwn-level3
    【mysql】 mysql忘记密码
    【Linux】linux磁盘管理
    【Linux】Linux系统LVM管理及Raid
    【Git】git撤销与回滚
    【linux】Linux系统SELinux简介
    【Linux】Linux中的网络命令
    【Linux】linux中文本操作利器grep,awk,sed
    【Linux】linux正则表达式及通配符
    【Mysql】mysql数据备份
  • 原文地址:https://www.cnblogs.com/wjc920/p/9256177.html
Copyright © 2011-2022 走看看