原则 1、属性
序号
方案
使用平台
推荐星数
其他
1
继承
iOS、Android、Flutter
2
组合
iOS、Android、Flutter
3
协议
iOS
4
抽象类
Flutter
1.1、继承 1.2、组合 1.3、协议(iOS) 在iOS开发中,除了使用Swift语言创建基类和组合属性的方式之外,还可以考虑使用协议(Protocol)来实现类似的效果。协议可以定义一组属性和方法的要求,然后其他类可以遵循该协议并提供相应的实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 protocol GoodsPriceProtocol { var marketPrice: Int ? { get set } var salePrice: Int ? { get set } } protocol GoodsBrandProtocol { var brandName: String ? { get set } var brandId: String ? { get set } } class BaseGoodsBean : GoodsPriceProtocol , GoodsBrandProtocol { var marketPrice: Int ? var salePrice: Int ? var brandName: String ? var brandId: String ? }
1.4、抽象类 在Flutter中,没有与iOS中的协议(Protocol)直接对应的语言特性。然而,您可以使用抽象类(Abstract Class)来实现类似的效果。抽象类可以定义一组抽象方法和属性,然后其他类可以继承该抽象类并提供相应的实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 abstract class GoodsPriceModel { int? marketPrice; int? salePrice; } abstract class GoodsBrandModel { String? brandName; String? brandId; } class BaseGoodsBean implements GoodsPriceModel , GoodsBrandModel { @override int? marketPrice; @override int? salePrice; @override String? brandName; @override String? brandId; }
End