I am totally new to Dart
and class
OOP, please help me understand the issue I have mentioned below in my comments in my code.
main() {
var shape = new Slot<Circle>();
shape.insert(new Circle());
}
class Circle {
something() {
print('Lorem Ipsum');
}
}
class Square {}
class Slot<T> {
insert(T shape) {
print(shape); // Instance of 'Circle'
print(shape.something()); // The method 'something' isn't defined for the class 'dart.core::Object'.
}
}
My question is, how can I call method that's in the Instance of 'Circle' and why am I getting the error?
You need to notify the compiler that your generic implements a specific interface:
abstract class DoSomething {
void something();
}
class Shape<T extends DoSomething> {
T value;
foo() {
// works fine
value.something();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.