简体   繁体   English

设置扩展抽象类的子类的实例变量

[英]Setting an instance variable of a child class that extends an abstract class

I have a class called Shape that is abstract, and a class named Circle that extends Shape 我有一个名为Shape的类,它是抽象的,还有一个名为Circle的类,它扩展了Shape

Shape shapeCircle = new Circle();

I can set and get colors of shapeCircle fine because the color getters and setters are in Shape , but the dimensions of Circle is only for the Circle class (radius). 我可以设置和获取shapeCircle的颜色很好,因为颜色获取器和设置器在Shape中,但是Circle的尺寸仅适用于Circle类(半径)。

If Circle class has an instance variable private int radius and a method called getRadius() , how can I get/set the radius of shapeCircle ? 如果Circle类具有一个实例变量private int radius和一个名为getRadius()的方法,如何获取/设置shapeCircle的半径? I tried shapeCircle.getRadius(); 我尝试了shapeCircle.getRadius(); , but no luck. ,但没有运气。

Only methods of Shape are accessible with object shapeCircle. 只有对象ShapeCircle可以访问Shape的方法。 shapeCircle is type Shape, methods in Circle are not visible. shapeCircle是Shape类型,Circle中的方法不可见。

By using this, 通过使用这个

 Circle shapeCircle = new Circle();

you can call both method of Circle and Shape. 您可以同时调用Circle和Shape方法。 The above case is take advantage of only Inheritance . 以上情况仅利用Inheritance

Edit 编辑

But if you add public abstract int getDimension(); 但是,如果您添加public abstract int getDimension(); in Shape class and Circle class implements getDimension() method Shape类和Circle类中实现getDimension()方法

@Override
    public int getDimension() {
        return radius;
    }

All classes that extends Shape needs to implements getDimension() method. 所有扩展Shape类都需要实现getDimension()方法。 But each subclass has its own dimension. 但是每个子类都有其自己的维度。

You can use 您可以使用

    Shape shapeCircle = new Circle();
    shapeCircle.getDimension();

This takes advantage of both polymorphism and abstract class . 这同时利用了多态和抽象类

Shape shapeCircle = new Circle();

Here the reference is of Shape and shape class doesn't defines radius variable.So you cannot use shapeCircle.getRadius(); 这里的引用是Shape,shape类没有定义半径变量。所以不能使用shapeCircle.getRadius();

To invoke get/set radious method type cast the shapecircle variable like this 要调用get / set radious方法,请像下面这样铸造shapecircle变量

Circle shapeCircle = (Circle)shapeCircle;

Now you can invoke the get/set radious methods. 现在,您可以调用get / set radious方法。 Please note that typecasting code should be in some different method to benefit using polymorphism. 请注意,类型转换代码应采用某种不同的方法来受益于多态。

EDIT: 编辑:

You should design your abstract classes or interfaces to be having all important/common operations/methods which will help you in writing polymorphic code. 您应该将抽象类或接口设计为具有所有重要/通用的操作/方法,以帮助您编写多态代码。 For example: Radius field is specific to a Circle class.But consider you eventually want to calculate the area. 例如:Radius字段特定于Circle类,但是考虑到您最终想要计算面积。 So you should define CalculateArea() method in your abstract class and let every shape implement this method.This way, you achieve polymorphism. 因此,您应该在抽象类中定义CalculateArea()方法,并让每个形状都实现此方法。这样您就可以实现多态。

Shape ShapeCicle = new Circle(Radius);
Float area = ShapeCircle.area();

Point is don't confine yourself with the radius example.Try to define classes in a way so that you can benefit from polymorphism. 要点不限于半径示例,请尝试以某种方式定义类,以便可以从多态中受益。

You have to use instanceof keyword to check for the original type here . 您必须使用instanceof关键字在此处检查原始类型。 If the type is Circle , then you can type cast and call the getRadius method. 如果类型为Circle ,则可以键入cast并调用getRadius方法。 Something like 就像是

if (shapeCircle instanceof Circle) {
 Circle circle = (Circle)shapeCircle;
 int radius = circle.getRadius();
}

That should do. 那应该做。

“形状不是圆形” =圆形方法不适用于“形状”。

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

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