一、初始代码
根据 java 或者 OC 的代码习惯,定义代码如下
void main() {
ShoppingCart sc = ShoppingCart('购物车');
sc.items = [Item('苹果',10.0), Item('鸭梨',20.0)];
print(sc.getInfo());
}
//商品Item
class Item {
double? price;
String? name;
Item(name,price) {
this.name = name;
this.price = price;
}
}
//定义购物车类
class ShoppingCart {
String? name;
List<Item>? items;
ShoppingCart(this.name)
price() {
double sum = 0.0;
for(Item i in items??[]) {
sum += i.price??0.toDouble();
}
return sum;
}
getInfo() {
return '===' + name.toString() + '===' + price().toString();
}
}
二、初始化函数优化
Dart语言特性,优化初始化函数
void main() {
ShoppingCart sc = ShoppingCart('购物车');
sc.items = [Item('苹果',10.0), Item('鸭梨',20.0)];
print(sc.getInfo());
}
//商品Item
class Item {
double? price;
String? name;
Item(this.name,this.price);
}
//定义购物车类
class ShoppingCart {
String? name;
List<Item>? items;
ShoppingCart(this.name);
price() {
double sum = 0.0;
for(Item i in items??[]) {
sum += i.price??0.toDouble();
}
return sum;
}
getInfo() {
return '===' + name.toString() + '===' + price().toString();
}
}
三、提取公共类
由于两个类都有price和name 了两个属性,因此提出公共类,并让两个类都继承公共类
void main() {
ShoppingCart sc = ShoppingCart('购物车');
sc.items = [Item('苹果',10.0), Item('鸭梨',20.0)];
print(sc.getInfo());
}
class Meta {
double price;
String name;
Meta(this.name, this.price);
}
//商品Item
class Item extends Meta{
Item(name, price) : super(name, price);
}
//定义购物车类
class ShoppingCart extends Meta{
List<Item>? items;
ShoppingCart(name,) : super(name, 0);
double get price {
double sum = 0.0;
for(Item i in items??[]) {
sum += i.price;
}
return sum;
}
getInfo() {
return '===' + name.toString() + '===' + price.toString();
}
}
四、优化逻辑函数
重载了+运算符,合并商品为套餐商品,然后把迭代求和改写为归纳合并
void main() {
ShoppingCart sc = ShoppingCart('购物车');
sc.items = [Item('苹果',10.0), Item('鸭梨',20.0)];
print(sc.getInfo());
}
class Meta {
double price;
String name;
Meta(this.name, this.price);
}
//商品Item
class Item extends Meta{
Item(name, price) : super(name, price);
//重载了+运算符,合并商品为套餐商品
Item operator+(Item item) => Item(name + item.name, price + item.price);
}
//定义购物车类
class ShoppingCart extends Meta{
List<Item> items;
ShoppingCart(name) : items = [], super(name, 0);
//把迭代求和改写为归纳合并
double get price => items.reduce((value, element) => value + element).price;
getInfo() {
return '===' + name.toString() + '===' + price.toString();
}
}
五、调用优化
声明抽象类,提供打印的函数
abstract class PrintHelper {
printInfo() => print(getInfo());
getInfo();
}
让ShoppingCart mixin PrintHelper
//定义购物车类
class ShoppingCart extends Meta with PrintHelper{
List<Item> items;
ShoppingCart(name) : items = [], super(name, 0);
//把迭代求和改写为归纳合并
double get price => items.reduce((value, element) => value + element).price;
getInfo() {
return 'name' + name.toString() + '价格' + price.toString();
}
}
优化调用方式,使用…来减少中间变量的声明定义
ShoppingCart('购物车')
..items = [Item('苹果',10.0), Item('鸭梨',20.0)]
..printInfo();
最终代码
void main() {
ShoppingCart('购物车')
..items = [Item('苹果',10.0), Item('鸭梨',20.0)]
..printInfo();
}
abstract class PrintHelper {
printInfo() => print(getInfo());
getInfo();
}
class Meta {
double price;
String name;
Meta(this.name, this.price);
}
//商品Item
class Item extends Meta{
Item(name, price) : super(name, price);
//重载了+运算符,合并商品为套餐商品
Item operator+(Item item) => Item(name + item.name, price + item.price);
}
//定义购物车类
class ShoppingCart extends Meta with PrintHelper{
List<Item> items;
ShoppingCart(name) : items = [], super(name, 0);
//把迭代求和改写为归纳合并
double get price => items.reduce((value, element) => value + element).price;
getInfo() {
return 'name' + name.toString() + '价格' + price.toString();
}
}
|