易百教程

75、可以通过使方法成为静态来重载方法吗?

不能。不能通过仅对它们应用 static 关键字来重载方法(参数和类型的数量相同)。 考虑以下示例:

public class Animal  
{  
    void consume(int a)  
    {  
        System.out.println(a+" consumed!!");  
    }  
    static void consume(int a)  
    {  
        System.out.println("consumed static "+a);  
    }  
    public static void main (String args[])  
    {  
        Animal a = new Animal();  
        a.consume(10);  
        Animal.consume(20);  
    }  
}

运行结果:

Animal.java:7: error: method consume(int) is already defined in class Animal
    static void consume(int a)
                ^
Animal.java:15: error: non-static method consume(int) cannot be referenced from a static context
        Animal.consume(20);
              ^
2 errors