In Apple's programming language Swift, you can use subscript as you do in arrays for your own class. For example a class in Swift could look like this:
class MyNumber {
let value: Int
init(value: Int) {
self.value = value
}
subscript(i: Int) -> Int {
return value * i
}
}
This class only has a getter subscript but you could do a setter subscript, too. Anyway when doing this:
let number = MyNumber(value: 15)
println(number[3])
it produces the output 45.
Is it possible to write such classes using subscript in Java as well? Of course, I could simply use a method with a parameter but I wanted to know if this is possible. Thanks for any answer :)