简体   繁体   English

在Java中重载私有方法

[英]Overloading a private method in Java

I want to get this thing straight: does overloading apply to methods in sub/super classes, or only to methods of one class can be overloaded? 我想直截了当地说:重载是应用于子/超类中的方法,还是只能重载一个类的方法?

public class Super{

     private void method(){
     }
}

class Sub extends Super{

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

Are both methods of Sub legally overloaded? 这两种方法是否合法超载? Is the method of Super overloaded as well? 超重的方法也是吗?

I don't know why you made all methods private. 我不知道为什么你把所有的方法都私有化了。 If you didn't, your question would actually make perfect sense. 如果你没有,你的问题实际上是完全合理的。 Consider this code: 考虑以下代码:

class Super{
  void method() {}
}

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

Now, even though it just declares one, the class Sub actually has two methods named method , therefore that method is overloaded for Sub . 现在,即使它只声明了一个,类Sub实际上有两个名为method ,因此该方法为Sub 重载 The class Super is unaffected because it still has just one method . Super类没有受到影响,因为它仍然只有一种method

As an aside, the most notorious example where the above gets in the way of program correctness involves the standard method equals . 顺便说一句,上面阻碍程序正确性的最臭名昭着的例子涉及标准方法equals Beginners are tempted to implement it just for the specific type: 初学者很想为特定类型实现它:

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

but this doesn't override Object.equals , so now the class has two equals methods. 但这不会覆盖Object.equals ,所以现在该类有两个equals方法。 The worst thing comes when some code accidentally uses the specific overload, whereas other code uses the general one: 最糟糕的事情发生在某些代码意外使用特定的重载时,而其他代码则使用通用代码:

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

You can't override a private method, because outside of Super , you can't even call the method. 您无法覆盖私有方法,因为在Super之外,您甚至无法调用该方法。 Even in subclasses. 即使在子类中。 You can define another method with the same name, but then the superclass still has its method, and the subclass has its own separate method. 您可以使用相同的名称定义另一个方法,但是超类仍然具有其方法,并且子类具有其自己的单独方法。

You need to understand basic overriding Rules in java : 您需要了解java中的基本重写规则:

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

A overriden method cannot : 覆盖方法不能

 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

As your question revolves around private method, i'll try to explain it. 由于您的问题围绕私有方法,我将尝试解释它。

  1. private method with in a class 类中的私有方法

     You are allowed to use same private method name with different signature with in a class. 

Please find the below example 请查看以下示例

private void method() {
    System.out.println("method");
}

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

2.private method in sup/sub class 2. sup / sub类中的私有方法

    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. 

Let's see what method overloading is by JLS : 让我们看看JLS的重载方法是什么:

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded. 如果一个类的两个方法(无论是在同一个类中声明,还是由一个类继承,或者一个是声明的,一个是继承的)都具有相同的名称,但签名不是覆盖等效的,那么方法名称就是超载。

What do we mean by override-equivalent ? 覆盖等价是什么意思?

Let's see what JLS says : 让我们看看JLS说的是什么:

Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1. 如果m1是m2的子签名或m2是m1的子签名,则两个方法签名m1和m2是覆盖等价的。

The signature of a method m1 is a subsignature of the signature of a method m2 if either: 方法m1的签名是方法m2的签名的子签名,如果:

m2 has the same signature as m1, or the signature of m1 is the same as the erasure (§4.6) of the signature of m2. m2与m1具有相同的签名,或者m1的签名与m2签名的擦除(§4.6)相同。

Now, lets see the example above. 现在,让我们看看上面的例子。

2 private methods in Sub are overloaded. Sub中的2个私有方法被重载。

The method() of Super is not inherited by Sub as it is private. Super的method()不是Sub继承的,因为它是私有的。 Hence, there is no overloading between these method() of Super and method(int x) of Sub. 因此,在Super的方法()和Sub的方法(int x)之间没有过载。

Let's see an simple example of overloading in a class in inheritance chain. 让我们看一个在继承链中的类中重载的简单示例。 In Eagle class, fly() is overloaded. 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;
    }
}

public class Super{ 公共课超级{

 private void method(){
 }

} class Sub extends Super{ } class Sub extends Super {

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

} }

Are both methods of Sub legally overloaded? 这两种方法是否合法超载? The Answer is Yes. 答案是肯定的。

Is the method of Super overloaded as well? 超重的方法也是吗? The Answer is No. Since Method is private in Super class it is not visible in subclass. 答案是否定的。由于Method在Super类中是私有的,因此在子类中不可见。

does overloading apply to methods in sub/super classes, or only to methods of one class can be overloaded? 重载是否适用于子/超类中的方法,或者只能重载一个类的方法? The Answer is Yes. 答案是肯定的。 All the public and protected methods of the super class are derived in child class. 超类的所有公共和受保护方法都是在子类中派生的。 You can overload derived methods. 您可以重载派生的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM