简体   繁体   English

继承和私有实例变量

[英]Inheritance and private instance variables

I'm having trouble figuring out how to access a private instance variable of the super class. 我无法弄清楚如何访问超类的私有实例变量。 I'm writing an equals method in the Dog class that compares to see if the name and breeds are the same, but name is a private instance variable inside Pet (that Dog inherits). 我正在Dog类中编写一个equals方法来比较,看看名称和品种是否相同,但name是Pet内部的私有实例变量(Dog继承)。

Here's my code: 这是我的代码:

public class Pet {

    private String name;

    public Pet(){
        name = "";
    }
    public Pet(String name){
        this.name = name;
    }

    public boolean equals(Pet other){
        return this.name.equals(other.name);
    }
}

and my Dog class: 和我的狗课:

public class Dog extends Pet {
    private String breed;
    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }

    public Dog(){
        breed = "";
    }

    @Override
    public boolean equals(Object obj){

        if(obj == null){
            return false;
        }

        if(obj.getClass() != this.getClass()){
            return false;
        }else{
            Pet p = (Pet)obj;
            Pet q = (Pet)this;
            Dog temp = (Dog)obj;
            boolean name = q.equals(p);
            boolean bred = breed.equals(temp.breed);
            return name && bred;
        }
    }
}

In my main class: 在我的主要课程中:

Dog d1 = new Dog("Harry", "Puggle");
Dog d2 = new Dog("Harry", "Pug");
System.out.println(d1.equals(d2));

For some reason it keeps using my Pet class's equal method. 由于某种原因,它继续使用我的Pet类的相同方法。

Thanks 谢谢

@Pshemo has identified the immediate cause of your problem. @Pshemo已找出问题的直接原因。 Your Pet.equals(Object) does not override `Dog.equals(String) because the signatures don't match. 您的Pet.equals(Object)不会覆盖`Dog.equals(String)因为签名不匹配。 And your d1.equals(d2) call is binding to the most closely matching method signature, which is the one with a Pet formal parameter rather than an Object formal parameter. 并且您的d1.equals(d2)调用绑定到最接近匹配的方法签名,该签名是具有Pet形式参数而不是Object形式参数的签名。

But once you have corrected that, there is another problem in the Dog.equals(String) method: 但是一旦你纠正了这个问题, Dog.equals(String)方法Dog.equals(String)出现另一个问题:

        Pet p = (Pet)obj;
        Pet q = (Pet)this;
        boolean name = q.equals(p);

When you fix the signature of Pet.equals, that is going to result in a recursive call to Dog.equals ... and a StackOverflowError . 修复Pet.equals的签名时,会导致对Dog.equals ...和StackOverflowError的递归调用。 (Dog.equals will call Dog.equals, which will call Dog.equals, which ...). (Dog.equals将调用Dog.equals,它将调用Dog.equals,其中......)。 Basically, q.equals is the same method as the one that is currently executing. 基本上, q.equals与当前正在执行的方法相同。 The type casts aren't doing anything ... 类型演员没有做任何事情......

Change it to this: 把它改成这个:

        Pet p = (Pet)obj;
        boolean name = super.equals(p);

The super keyword used this way invokes the overridden version of the equals method. 使用这种方式的super关键字调用equals方法的重写版本。


I'm having trouble figuring out how to access a private instance variable of the super class. 我无法弄清楚如何访问超类的私有实例变量。

This is a different issue to what is causing your problem. 对于导致问题的原因,这是一个不同的问题。 But the answer is that if you want a child classes method to be able to access a private variable in a parent class, then you need to either add getter and/or setter methods to the parent class, or change the access of the variable to (typically) protected . 但答案是,如果您希望子类方法能够访问父类中的private变量,那么您需要将getter和/或setter方法添加到父类,或者将变量的访问权限更改为(通常) protected

If you look carefully your Dog class have two equals methods: 如果你仔细看看你的Dog类有两个equals方法:

  • equals(Pet other) form super class equals(Pet other)形成超级
  • equals(Object obj) from Dog class . 来自Dog类的equals(Object obj)

Since you are using d1.equals(d2) where d1 and d2 are Dog instances Java will use equals(Pet other) method from super class in which you only check name equality. 由于您使用的是d1.equals(d2) ,其中d1和d2是Dog实例,Java将使用来自超类的equals(Pet other)方法,其中您只检查name相等性。

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

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