可以在超类数组中拥有子类数组吗?

huangapple go评论136阅读模式
英文:

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

问题

假设有一个形状的超类,以及正方形和三角形的两个子类。
那么,是否可以创建一个类型为形状的数组,其中包含正方形和三角形对象?
如果可以的话,最佳做法是什么?

下面是一个示例,展示了我想了解的可能性:

class shapes{
    protected int linear_scale_factor = 4;
}

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

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

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

编辑

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);
        }
    }
}

类似这样的代码为什么不起作用呢?

英文:

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?

答案1

得分: 0

你有两个类,它们是Shape类的子类。这两个类都覆盖了字段scaled_area。这意味着通过shapes_arr对scaled_area的任何引用都会调用超类字段,而该字段尚未被赋任何值。此外,在声明时计算scale_area的值时,字段area中存储的值是默认值(对于int为0)。以下是一个可能的重新排列,可以正常工作:

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);
        }
    }
}
英文:

You have two classes that are subclasses of the Shape class. The two classes override the field 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. Furthermore, you are computing the value for scale_area during declaration when the values stored in the field area are defaults (0 for int). 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);
        }
    }

}

huangapple
  • 本文由 发表于 2020年6月29日 09:01:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/62629821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定