我有不同类型的类,每个类都有自己的变量和函数。我在创建一个类,在内部创建一个遗传过程,该过程将对象类作为参数,并能够调用该类的变量和函数。我在创建遗传功能时遇到问题。
这是我的数据课程
class TransactionContent {
int id, groupId;
String category, frequency, payoutDate, amount, status;
TransactionContent({
this.id,
this.groupId,
this.category,
this.frequency,
this.payoutDate,
this.amount,
this.status
});
factory TransactionContent.fromMap(Map<String, dynamic> transactionContent) => new TransactionContent(
id: transactionContent["id"],
groupId: transactionContent["groupId"],
category: transactionContent["category"],
frequency: transactionContent["frequency"],
payoutDate: transactionContent["payoutDate"],
amount: transactionContent["amount"],
status: transactionContent["status"],
);
Map<String, dynamic> toMap() => {
"id": id,
"groupId": groupId,
"category": category,
"frequency": frequency,
"payoutDate": payoutDate,
"amount": amount,
"status": status,
};
}
class Expense implements ListItem {
int id, groupId;
String expenseName, frequency, paidDate, amount, status;
TransactionContent({
this.id,
this.groupId,
this.expenseName,
this.frequency,
this.paidDate,
this.amount,
this.status
});
factory Expense.fromMap(Map<String, dynamic> expense) => new Expense (
id: expense["id"],
groupId: expense["groupId"],
expenseName: expense["expenseName"],
frequency: expense["frequency"],
paidDate: expense["paidDate"],
amount: expense["amount"],
status: expense["status"],
);
Map<String, dynamic> toMap() => {
"id": id,
"groupId": groupId,
"expenseName": expenseName,
"frequency": frequency,
"paidDate": paidDate,
"amount": amount,
"status": status,
};
}
这是我试图在另一个类中创建的遗传函数。 基本上,getTransactions函数将接受任何类型的类对象,例如, TransactionContent,Expense类等,然后您可以访问作为参数传递的该类的变量和函数。
getTransactions(class T) async {
final db = await database;
var res = await db.query('Transaction');
List<T> transactions = res.isNotEmpty ? res.map((trans) => T.fromMap(trans)).toList() : [];
return transactions;
}
如何实现一个接受任何类对象并能够访问类对象变量和函数的函数?例如,如果我传递Expense的类对象,那么我应该能够像这样访问变量:
classobject.expenseName or function like classobject.ToMap().
我不想为每种类型的类创建多个函数。我正在寻找可以通过不同类型的类重用的东西。 预先谢谢你