首页 > 解决方案 > 为什么 Java 设计者没有为具有相同参数但顺序不同的重载方法创建错误?

问题描述

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here

        Tree t = new Tree();
        t.cutTree("saw", 10);
        t.cutTree(10, "knife");

    }

    static class Tree{

        public static void cutTree(String instrument, int cutLength){
            System.out.println("first method");
        }

        public static void cutTree(int cutLength, String instrument){
            System.out.println("second method");
        }

    }
}

在这两个重载方法中,参数相同,但顺序不同。

为什么在 Java 中允许这样做?Java 创建者背后的原因是什么?

标签: java

解决方案


推荐阅读