I want to translate the java code below into Dart.
//Java Code
int i = (value == true) ? 0 : 1;
MyClass[] newInstance = new MyClass[2];
{
newInstance[0] = new Class2();
newInstance[1] = new Class3();
}
The first line works great!
//Java Code
int i = (value == true) ? 0 : 1;
But I'm not sure how to create an array 'newInstance' of 2 elements of the class, 'MyClass'.
I have tried looking at this answer and this website and came up with this:
//Dart Code
class MyClass {
List<MyClass> myClassListObject;
MyClass(int num) {
this.myClassListObject = new List<MyClass>(num);
}
}
But I'm not sure how to progress from here.
Any help would be great!
Something like this. Notice that you don't need to allocate the array but can just create the array with two element. Also, the two elements in the List need to implements/extends the MyClass for type safety.
class MyClass {}
class Class2 implements MyClass {}
class Class3 implements MyClass {}
void main() {
bool value = true;
int i = (value == true) ? 0 : 1;
List<MyClass> newInstance = [Class2(), Class3()];
}
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.