Java: Extend class as array

2019-06-28 02:56发布

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条回答
ら.Afraid
2楼-- · 2019-06-28 03:30

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)

查看更多
登录 后发表回答