Skip to content

How do I define a generic type? #7

@ThomsonTang

Description

@ThomsonTang

Like a regular type, but with a type parameter declaration attached.

A generic type is a reference type that has one or more type parameters. In the definition of the generic type, the type parameter section follows the type name. It is a comma separated list of identifiers and is delimited by angle brackets.

Example:

class Pair<X, Y> {
    private X first;
    private Y second;

    public Pair(X x, Y y) {
        this.first = x;
        this.second = y;
    }

    public X getFirst() {
        return first;
    }

    public Y getSecond() {
        return second;
    }

    public void setFirst(X x) {
        this.first = x;
    }

    public void setSecond(Y y) {
        this.second = y;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions