简体   繁体   English

如何使用父类中的字段实例化对象?

[英]How do I instantiate an object with fields from the parent class?

I am trying to get an understanding of object oriented programming in Java and I have this problem. 我试图了解Java中的面向对象编程,但遇到了这个问题。

Say for example, I have aa parent class like this: 例如,我有一个这样的父类:

public class Shape {
    private int location;
    private Color color;
    // methods such as getLocation() and getColor()

    public Shape(int initialLocation, Color initialColor) {
        location = initialLocation;
        color = initialColor;
    }


}

How do I make my child class so that I can construct, say, a rectangle with an initial location and an initial color in a main method? 如何制作子类,以便可以在main方法中构造具有初始位置和初始颜色的矩形? Do I create a constructor in the Rectangle class? 是否在Rectangle类中创建构造函数? I can't because location and color are private fields. 我不能,因为位置和颜色是私有字段。 Do I create accessor methods for location and color and just set the location and color after instantiation? 我是否创建用于位置和颜色的访问器方法,并且仅在实例化后设置位置和颜色? I guess, but is there a way to do this without accessors? 我想,但是有没有访问器的方法吗?

public class Rectangle extends Shape {
    public Rectangle(int initialLocation, Color initialColor) {
        super(initialLocation, initialColor);
    }

}

I just can't wrap my head around this fundamental concept. 我只是无法将这个基本概念束之高阁。 Any help? 有什么帮助吗?

Reuse constructors 重用构造函数

public class Shape {
    private int location;
    private Color color;

    public Shape(int location, Color color) {
        this.location = location;
        this.color = color;
    }
    // methods such as getLocation() and getColor()
}

and

public class Rectangle extends Shape {
    public Rectangle(int location, Color color /*, more */) { 
        super(location, color);
        // more
    }
}

This official tutorial explains its use. 官方教程介绍了其用法。

如果要扩展变量,可以将其修饰符更改为protected ,以便可以继承,否则private就像它们对于子类不存在一样。

Although you can define the instance variables as protected , however, this goes against the Object Oriented principle of encapsulation. 尽管您可以将实例变量定义为protected ,但这违反了面向对象的封装原理。 I would use the getters and setters for each instance variable of the class Shape. 我将对Shape类的每个实例变量使用getter和setter方法。 Also, if you create a Constructor within Shape you could call the super constructor in Rectangle to set the location and colour in Rectangle. 另外,如果在Shape中创建构造函数,则可以在Rectangle中调用超级构造函数以在Rectangle中设置位置和颜色。

public class Rectangle extends Shape {
    public Rectangle(int location, Color color) { 
        super(location, color);
    }
}

as long as you have the following constructor in Shape: 只要您在Shape中具有以下构造函数:

public class Shape {
    // location and color define.

    public Shape(int location, Color color) {
        this.location = location;
        this.color = color;
    }
    // getters and setters which are public for location and color
}

Private members in a base class that are only accessed by subclasses are pointless! 基类中只能由子类访问的私有成员是毫无意义的! If you want to read them, you'll need at least a public or protected getter. 如果要阅读它们,则至少需要一个公共或受保护的吸气剂。 If you want to write them, you'll need at least a public or protected setter and/or a constructor that initializes them. 如果要编写它们,则至少需要一个公共的或受保护的setter和/或用于初始化它们的构造函数。

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

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