半夜思考, Java 重载的实现

      因为最近在学 scala,看到了参数的默认值这个特性,但是Java好像没有这个特性, Java8 也没有, 所以特意去查了一下,就牵扯到了 C++了,【只怪 C++没怎么学,,】。

      下面将一下为什么 Java的重载机制的实现,以及为什么 Java8没有出来参数默认值这特性【我猜的】

      刚开始还在想, 重载是怎么实现的, 看了 String 的源码之后, 原来自己也写过, 就是使用 this调用其他的函数, 写的多个构造函数不也是这么写的吗, 我只想嘲笑我自己了,

    @Deprecated
    public String(byte ascii[], int hibyte) {
        this(ascii, hibyte, 0, ascii.length);
    }

   //省略很多行

    @Deprecated
    public String(byte ascii[], int hibyte, int offset, int count) {
        checkBounds(ascii, offset, count);
        char value[] = new char[count];

        if (hibyte == 0) {
            for (int i = count; i-- > 0;) {
                value[i] = (char)(ascii[i + offset] & 0xff);
            }
        } else {
            hibyte <<= 8;
            for (int i = count; i-- > 0;) {
                value[i] = (char)(hibyte | (ascii[i + offset] & 0xff));
            }
        }
        this.value = value;
    }

String 类的这个构造函数我还没用过, 反正重载就是这样使用 this实现的.

为什么 Java8 没有参数默认值这个特性, 假想一下, 如果有了参数默认值这个特性, 我们怎么实现重载 .

 

不要怀疑, 这篇博客的确是来闹眼子的 .

posted @ 2017-07-06 23:30  码上猿梦  阅读(222)  评论(0编辑  收藏  举报