I remember in C#, you can do something (can't remember exactly) that will make your own class act just like it's an Array - you could forexample make a Book class and then a static class called Books and then just say Books to access the fourth Book in Books.
Considering you can't have a static class I would guess no, but I'm not sure the advantage of that over just having an array (maybe a static array) of type Book. Maybe you could elaberate more on what you are trying to do.
So, if I understand your question correctly, you want to be able to create a class (say BookArray), that you can use like this:
To answer your question, no you cannot do this. However, there are other ways to accomplish the same thing. If your question is more than acedemic interest, perhaps you can post an explanation of what you want to do and we can help you find a Java-like way to do it.
well I'm trying to make a class to keep track of a non-predetermined number of radio stations and each radio station is represented by a Station object that contains Name, URL, and Keyword.
I want to be able to easily parse an XML document containing a set of Station elements into a bunch of Station objects inside a StationArray object. Then I want to be able to pick a Station at random and then call some of its methods. But I also want the StationArray object to have some special methods of its own that I can call to perform some custom actions on the entire collection of Station objects.
Joined: Dec 29, 2004
If you want to call a method on all of the Station elements you will have to iterate over them. You would probably want to store them in something like an ArrayList. You could create a class that would take an Array or Collection of Stations and perform some method on them all.
Joined: Jan 31, 2005
oh, no I'm sorry I didn't make it quiet clear enough - I don't mean perform a method on each one of the Stations in StationArray. I mean I want the StationArray class to have some methods for itself, for example a Save() method that saves the StationArray to disk as an XML document.
Just make a class that implements List and has the extra functionality you want
Joined: Dec 06, 2001
I read somewhere that in OOP, you should prefer encapsulation over inheritence. Although, either way would work, I would suggest that you create a class with an ArrayList (or other Collection if that would be more appropriate). You can provide a getStation() method that returns an individual Station and someone else can then call any methods they want on that Station.
I would agree that your best bet is to write a class called "StationList" which has a List encapsulated inside of it. Then write a few accessor methods like setStation, getStation, size, addStation and maybe removeStation. These could all operate on a class called "Station" which has whatever properties you want a station to have....
I tend to do this with most custom list applications, and I like it because you are writing the list accessor methods yourself, so you can control what goes in and what goes out. A BIG plug here is that you do the casting to Object inside these accessor methods, so all the "client" programmer sees is Station's going in and out.
You can also do things like check for null, so that null references cannot be added to the list.