Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Define a Generic Class

This video was last updated on Jun 28 2022

Level up your generic programming skills by learning how to apply generic concepts to classes.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

If you watched the previous course, you were introduced to generics. Generics allow us to write code that works on many different types. If you haven’t watched that episode, then stop watching now and watch that episode. It’ll get you up to speed.

class Adder<T extends num> {

}
T total;
Adder(this.total);
void add(T item) {
  total += total;
}
total = (total + item) as T;
var intAdder = Adder<int>(0);
intAdder.add(10);
intAdder.add(40);
print(intAdder.total);
var doubleAdder = Adder<double>(4.5);
doubleAdder.add(6.1);
doubleAdder.add(10.10);
print(doubleAdder.total);
var stringAdder = Adder<String>(0);

Reviews

Comments