在Java中重载私有方法

时间:2014-09-18 18:55:58

标签: java overloading

我想直截了当地说:重载是否适用于子类/超类中的方法,或者只能重载一个类的方法?

public class Super{

     private void method(){
     }
}

class Sub extends Super{

     private void method(){ 
     }
     private void method(int x){    
     }  
}

这两种方法是否合法超载?超重的方法也是吗?

6 个答案:

答案 0 :(得分:4)

我不知道为什么你把所有方法都私有化了。如果你没有,你的问题实际上是完全合理的。请考虑以下代码:

class Super{
  void method() {}
}

class Sub extends Super {
  void method(int x) {}  
}

现在,尽管它只声明了一个,但是类Sub实际上有两个方法,名为method,因此该方法重载 Sub。课程Super不受影响,因为它仍然只有一个method

顺便说一句,上面妨碍程序正确性的最臭名昭着的例子涉及标准方法equals。初学者很想为特定类型实现它:

public class Thing {
  public boolean equals(Thing that) { ...compare by object contents... }
}

但这不会覆盖Object.equals,因此现在该类有两个equals方法。最糟糕的是,当某些代码意外使用特定的重载时,而其他代码则使用通用的代码:

Thing t1 =  new Thing(), t2 = new Thing();
System.out.println(t1.equals(t2)); // true, great
Object o1 = t1, o2 = t2;
System.out.println(o1.equals(o2)); // now suddenly false
System.out.println(t1.equals(o2)); // false again
System.out.println(o1.equals(t2)); // still false

答案 1 :(得分:3)

您无法覆盖私有方法,因为在Super之外,您甚至无法调用该方法。即使在子类中。您可以使用相同的名称定义另一个方法,但是超类仍然有它的方法,并且子类有自己独立的方法。

答案 2 :(得分:1)

您需要了解java中的基本覆盖规则:

 0).private, static and final method can  not be overridden 

重写方法不能

 1)   reduces access of overriden method i.e.if overridden method declared in parent class is defined with access modifier public than overriding method can not be  package private or protected 
 2).  throw broder checked Exception For example if overridden method throws FileNotFoundException then overriding method can not throw java.lang.IOException

答案 3 :(得分:1)

由于你的问题围绕私人方法,我会尝试解释它。

  1. 类中的私有方法

    You are allowed to use same private method name with different signature with in a class.
    
  2. 请查看以下示例

    private void method() {
        System.out.println("method");
    }
    
    private void method(int x) {
        System.out.println("method with param x");
    }
    

    2. sup / sub class中的私有方法

        There is no question of using private method outside the class as it is not visible.  Means you can't overload a super class private method in sub class. 
    

答案 4 :(得分:0)

让我们看看JLS重载的方法是什么:

  

如果一个类的两个方法(无论是在同一个类中声明,还是   既由一个类继承,又一个是声明的,一个是继承的)   相同的名称,但不是覆盖等效的签名,然后是   方法名称被称为重载。

覆盖等价是什么意思?

让我们看看JLS说的是什么:

  

如果是m1,则两个方法签名m1和m2是覆盖等效的   是m2或m2的子签名是m1的子签名。

     

方法m1的签名是a的签名的子签名   方法m2如果:

     

m2与m1具有相同的签名,或者m1的签名与   删除签名的删除(§4.6)。

现在,让我们看看上面的例子。

Sub中的2个私有方法被重载。

Super的method()不会被Sub继承,因为它是私有的。因此,在Super的方法()和Sub的方法(int x)之间没有过载。

让我们看一个在继承链中的类中重载的简单示例。在Eagle类中,fly()被重载。

public class Bird {
    public void fly() {
        System.out.println("Bird is flying");
    }
    public void eat(int food) {
        System.out.println("Bird is eating "+food+" units of food");
    }
}
public class Eagle extends Bird {
    public int fly(int height) {
        System.out.println("Bird is flying at "+height+" meters");
        return height;
    }
}

答案 5 :(得分:0)

公共类超级{

 private void method(){
 }

} Sub类扩展了Super {

 private void method(){ 
 }
 private void method(int x){    
 }  

}

Sub的两种方法是否合法重载? 答案是肯定的。

超级重载的方法也是吗? 答案是否定的。由于方法在超级类中是私有的,因此在子类中不可见。

重载适用于子类/超级类中的方法,还是仅适用于一个类的方法可以重载? 答案是肯定的。超类的所有公共方法和受保护方法都在子类中派生。您可以重载派生的方法。