HOME

TheInfoList



OR:

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
, an indexer allows instances of a particular class or struct to be indexed just like arrays. It is a form of operator overloading.


Implementations


C++

In C++ one can emulate indexing by overloading the operator. The expression translates to a call to the user-defined function as . Here is an example, struct vector ; #include int main()


C#

Indexers are implemented through the get and set accessors for the . They are similar to properties, but differ by not being static, and the fact that indexers' accessors take parameters. The get and set accessors are called as methods using the parameter list of the indexer declaration, but the set accessor still has the implicit parameter.


Example 1

public class Vector


Example 2

Here is a C# example of the usage of an indexer in a class: class Family Usage example: void Main() In this example, the indexer is used to get the value at the nth position, and then to get the position in the list referenced by its value. The output of the code is: John is the member number 0 of the doeFamily Jane is the member number 1 of the doeFamily


PHP

In PHP indexing can be implemented via the predefined interface, class Vector implements ArrayAccess $vector = new Vector(3); for ($i = 0; $i < $vector->size; $i++) $vector i= $i + 1; for ($i = 0; $i < $vector->size; $i++) print "\n";


Python

In Python one implements indexing by overloading the and methods, import array class Vector(object): def __init__(self, n: int): self.size = n self.data = array.array("d", .0* n) def __getitem__(self, i: int): return self.data def __setitem__(self, i: int, value): self.data = value vector = Vector(3) for i in range(vector.size): vector = i + 1 for i in range(vector.size): print(vector


Rust

Rust provides the trait. use std::ops::Index; enum Nucleotide struct NucleotideCount impl Index for NucleotideCount let nucleotide_count = NucleotideCount ; assert_eq!(nucleotide_count ucleotide::A 14); assert_eq!(nucleotide_count ucleotide::C 9); assert_eq!(nucleotide_count ucleotide::G 10); assert_eq!(nucleotide_count ucleotide::T 12);


Smalltalk

In Smalltalk one can emulate indexing by (e.g.) defining the and instance methods. For example, in GNU Smalltalk, Object subclass: vector data, vector class extend v"> v:=super new. v init: n. ^vvector extend init: n data:= Array new: n ">init: n [ data:= Array new: n vector extend data:= Array new: n ">data:= Array new: n ">init: n [ data:= Array new: n vector extend [ size [ ^(data size) ">size [ ^(data size) ">data:= Array new: n ">data:= Array new: n ">init: n [ data:= Array new: n vector extend [ size [ ^(data size) vector extend [ get: i [ ^(data at: i) ] ] vector extend [ set: i value: x [ data at: i put: x ] ] v:=vector new: 3 1 to: (v size) do: [:i, v set: i value: (i+1) ] 1 to: (v size) do: [:i, (v get: i) printNl ]


See also

* Mutator method


References

Articles with example C++ code Articles with example C Sharp code Articles with example Python (programming language) code Articles with example PHP code Articles with example Rust code Programming language topics Object-oriented programming Operators (programming) {{compu-prog-stub