简体   繁体   English

是否可以在超类类型的数组中包含子类数组

[英]is it possible to have an array of subclasses in an array of type superclass

suppose one has a super-class of shapes two sub-classes of square and triangle.假设一个人有一个超类的形状,两个子类是正方形和三角形。 could one then make an array of type shapes containing square and triangle objects?然后可以制作一个包含正方形和三角形对象的类型形状数组吗? if so what would be the best practice for doing so?如果是这样,这样做的最佳做法是什么?

example below of what i want to know is possible:下面我想知道的例子是可能的:

class shapes{
    protected int linear_scale_factor = 4;
}

class square extends shapes {
    int area;
    int scaled_area = area * linear_scale_factor**2;
}

class triangle extends shapes {
   int area;
   int scaled_area = area * linear_scale_factor**2;

}

class main{
   shapes[] shapes_arr = new shapes[8];
   shapes_arr[0] = new square(34);
}

edit编辑

class shapes{
    protected int linear_scale_factor = 4;
    protected int scaled_area ;
}

class square extends shapes {
    int area;
    int scaled_area = area * linear_scale_factor*linear_scale_factor;
    
    public square(int i){
        area = i;
    }
}

class triangle extends shapes {
   int area;
   int scaled_area = area * linear_scale_factor*linear_scale_factor;

   public triangle(int i){
       area = i;
   }

}

class main{
    public static void main(String[] args) {
        shapes[] shapes_arr = new shapes[8];
        shapes_arr[0] = new square(34);
        shapes_arr[1] = new triangle(12);
        shapes_arr[2] = new square(453);


        for(shapes shape : shapes_arr){
            System.out.println(shape.scaled_area);
        }
    }

}

something like this doesn't work, why?像这样的东西不起作用,为什么?

You have two classes that are subclasses of the Shape class.您有两个类是形状 class 的子类。 The two classes override the field scaled_area .这两个类覆盖了scaled_area字段。 This means the any reference to scaled_area via shapes_arr is calling the superclass field, which has not been assigned any value.这意味着通过shape_arr对 scaled_area 的任何引用正在调用尚未分配任何值的超类字段。 Furthermore, you are computing the value for scale_area during declaration when the values stored in the field area are defaults (0 for int).此外,当存储在字段区域中的值是默认值(0 表示 int)时,您在声明期间计算scale_area的值。 Here is a possible rearrangement that can work:这是一个可行的重新排列:

class shapes{
    protected int linear_scale_factor = 4;
    protected int scaled_area ;
}

class square extends shapes {
    int area;
    public square(int i){
        area = i;
        scaled_area = area * linear_scale_factor*linear_scale_factor;
    }
}

class triangle extends shapes {
   int area;
   

   public triangle(int i){
       area = i;
       scaled_area = area * linear_scale_factor*linear_scale_factor;
   }

}

class main{
    public static void main(String[] args) {
        shapes[] shapes_arr = new shapes[8];
        shapes_arr[0] = new square(34);
        shapes_arr[1] = new triangle(12);
        shapes_arr[2] = new square(453);


        for(shapes shape : shapes_arr){
            System.out.println(shape.scaled_area);
        }
    }

}

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

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