Java: Extend class as array

核能气质少年 提交于 2019-12-10 17:30:32

问题


Quick novice question: I want to extend a class as an array, like so

public class Map extends Item[][]
{

}

Is this possible, am I going about it the wrong way? Thanks!


回答1:


Arrays are weird beasts. The have some properties, like length but they are not a class, so you can't extend them like you are attempting.

I think you are better off using composition, i.e. create a class that contains an Item[][] and then extend that class (if you need to, having one class might be enough)



来源:https://stackoverflow.com/questions/10914928/java-extend-class-as-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!