简体   繁体   English

如何从具体子类中的重写方法访问通过抽象类中的构造函数实例化的对象的属性?

[英]How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class?

I'm new to OOP and am working my way through an exercise in which I'm provided an abstract class and asked to implement a concrete subclass, while also modifying the abstract class by adding hidden state. 我是OOP的新手,我正在通过一个练习来工作,在该练习中,我提供了一个抽象类并要求实现一个具体的子类,同时还通过添加隐藏状态来修改抽象类。

public abstract class someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        this.a = a;
        this.b = b;
    }

    /* Abstract Getter 1 */
    public abstract String geta();

    /* Abstract Getter 2 */
    public abstract String getb();

}

public class concreteClass extends someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        super(a,b);
    }

    /* Implementation of Getter 1 */
    @Override
    public String geta(){
        // ???
    }

    /* Implementation of Getter 2 */
    @Override
    public String getb(){
        // ???
    }
}

The above is a simplification of the two classes. 上面是这两个类的简化。 What I am unsure of is how can I define the two getter methods in my concrete child class, if the methods are abstract in the superclass? 我不确定的是,如果超子类中的方法是抽象的,该如何在具体的子类中定义两个getter方法? More specifically, how can I return the attributes of the object, if I've used the superclass constructor to instantiate the object. 更具体地说,如果我已经使用超类构造函数实例化了对象,那么如何返回对象的属性。

I cannot simply say something like return super.a or return this.a - when I try to do so, the compiler complains that it cannot find a . 我不能简单地说是这样return super.areturn this.a -当我尝试这样做,编译器会抱怨它不能找到a If I were to have a non-abstract method in the abstract class, I could reference that method in my concrete (child) class, and access the attributes I'm trying to access. 如果要在抽象类中使用非抽象方法,则可以在具体的(子)类中引用该方法,并访问我尝试访问的属性。 Surely I'm overlooking something very banal. 当然,我忽略了一些平庸的东西。 Thank you all in advance for any shared wisdom. 预先感谢大家的共同智慧。

I have made some corrections to answer your question. 我已作了一些更正,以回答您的问题。

  • Names of classes start with capital letters (its a convention) 类的名称以大写字母开头(约定)

  • If the abstract class references a and b with this , it also has to declare those variables as members of the class. 如果抽象类使用this引用ab ,则它还必须将这些变量声明为类的成员。

  • The constructor is defined with the same name of the class. 构造函数以与类相同的名称定义。

When you extend a class, the extended class inherits the member variables of the super class as its own. 扩展类时,扩展类将继承超类的成员变量。 So to reference a and b from the inherited class you can just write a, b, or this.a, this.b ( this referencess the instance of the inherited class from withing itself). 因此,要从继承的类中引用ab ,您可以只编写a,b或this.a,this.b( this是从继承自身引用继承类的实例)。

If the inherited class will be able to just read the value or change it will depend if it was declared as public, private, or (the default) protected in the super class (the abstract class in this case) 如果继承的类将能够读取值或进行更改,则取决于继承的类是在超类(在本例中为抽象类)中声明为公共,私有还是(默认)保护的。

public abstract class SomeAbstractClass{
    protected String a;
    protected String b;

    /* Constructor */
    public SomeAbstractClass(String a, String b){
        this.a = a;
        this.b = b;
    }

    /* Abstract Getter 1 */
    public abstract String geta();

    /* Abstract Getter 2 */
    public abstract String getb();

}

public class concreteClass extends someAbstractClass{

    /* Constructor */
    public New(String a, String b){
        super(a,b);
    }

    /* Implementation of Getter 1 */
    @Override
    public String getA(){
        return this.a;
    }

    /* Implementation of Getter 2 */
    @Override
    public String getB(){
        return this.b;
    }
}

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

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