鸿蒙 ArkTS 实战:沙拉分量计算的状态建模、算法与交互实现
鸿蒙 ArkTS 实战:沙拉分量计算的状态建模、算法与交互实现
前言
沙拉分量计算用于按人数计算蔬菜与蛋白食材总克数。
项目通过 ArkTS、ArkUI 和 @State 管理 people、greens、protein、result。
核心是 人数公共因子、双乘法和整数范围,默认数据为2 人、每人蔬菜 120g、蛋白 80g。
页面展示蔬菜总量和蛋白总量。
图 1:DevEco Studio 中的 ArkTS 开发环境。
页面结果应能从当前状态完整复算。
边界条件必须落到真实代码路径。
两个结果共享 people。
本文只讨论源码已实现能力。
一、业务对象
1.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑业务对象。
两个结果共享 people。
1.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
1.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
1.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 业务对象维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
二、状态字段
2.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑状态字段。
两类单份重量独立。
2.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
2.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
2.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
三、核心规则
3.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑核心规则。
一次计算同时更新 result。
3.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
3.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
3.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
四、输入控件
4.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑输入控件。
两个结果共享 people。
4.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
4.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
4.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 输入控件维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
五、事件回调
5.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑事件回调。
两类单份重量独立。
5.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
5.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
5.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
六、边界保护
6.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑边界保护。
一次计算同时更新 result。
6.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
6.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
6.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 沙拉分量计算保存真实状态。
- calc参与核心逻辑。
- 蔬菜总量和蛋白总量形成反馈。
- 人数公共因子、双乘法和整数范围保护输出。
七、状态反馈
7.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑状态反馈。
两个结果共享 people。
7.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
7.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
7.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
八、默认运行
8.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑默认运行。
两类单份重量独立。
8.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
8.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
8.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 默认运行维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
九、连续操作
9.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑连续操作。
一次计算同时更新 result。
9.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
9.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
9.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
十、数组或数值更新
10.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑数组或数值更新。
两个结果共享 people。
10.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
10.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
10.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
十一、声明式刷新
11.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑声明式刷新。
两类单份重量独立。
11.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
11.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
11.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 沙拉分量计算保存真实状态。
- calc参与核心逻辑。
- 蔬菜总量和蛋白总量形成反馈。
- 人数公共因子、双乘法和整数范围保护输出。
十二、布局结构
12.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑布局结构。
一次计算同时更新 result。
12.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
12.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
12.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 布局结构维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
十三、颜色语义
13.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑颜色语义。
两个结果共享 people。
13.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
13.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
13.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
十四、异常输入
14.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑异常输入。
两类单份重量独立。
14.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
14.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
14.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
十五、验证场景
15.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑验证场景。
一次计算同时更新 result。
15.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
15.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
15.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 读取输入。
- 执行业务规则。
- 处理边界。
- 刷新界面。
十六、性能表现
16.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑性能表现。
两个结果共享 people。
16.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
16.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
16.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 性能表现维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
十七、维护方式
17.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑维护方式。
两类单份重量独立。
17.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
17.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
17.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
十八、代码职责
18.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑代码职责。
一次计算同时更新 result。
18.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
18.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
18.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
- 沙拉分量计算保存真实状态。
- calc参与核心逻辑。
- 蔬菜总量和蛋白总量形成反馈。
- 人数公共因子、双乘法和整数范围保护输出。
十九、运行结论
19.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑运行结论。
两个结果共享 people。
19.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
19.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
19.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
二十、项目启示
20.1 数据结构
沙拉分量计算使用 people、greens、protein、result 支撑项目启示。
两类单份重量独立。
20.2 业务规则
人数分别乘每人蔬菜和每人蛋白克数,生成双结果
calc 及相关表达式承担核心行为。
20.3 交互刷新
控件回调更新状态并显示蔬菜总量和蛋白总量。
ArkUI 根据依赖关系刷新。
20.4 边界结论
人数公共因子、双乘法和整数范围
结果因此保持稳定。
| 项目启示维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
二十一、真实源码分段
21.1 源码片段 1
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.2 源码片段 2
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.3 源码片段 3
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.4 源码片段 4
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.5 源码片段 5
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.6 源码片段 6
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.7 源码片段 7
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
21.8 源码片段 8
以下代码来自当前项目。
@Entry @Component struct Index{@State people:number=2;@State greens:number=120;@State protein:number=80;@State result:string='设置人数后计算';calc():void{this.result='蔬菜 '+this.people*this.greens+' 克,蛋白食材 '+this.people*this.protein+' 克'}build(){Column(){Text('沙拉分量计算').fontSize(28).fontWeight(FontWeight.Bold).width('100%');Text('按人数计算蔬菜和蛋白食材用量').fontColor('#667085').width('100%').margin({top:6,bottom:24});Text('人数:'+this.people).fontSize(19).width('100%');Slider({value:this.people,min:1,max:12,step:1}).onChange((v:number)=>this.people=Math.round(v)).width('100%');Text('每人蔬菜:'+this.greens+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.greens,min:50,max:300,step:10}).onChange((v:number)=>this.greens=Math.round(v)).width('100%');Text('每人蛋白:'+this.protein+' 克').fontSize(19).width('100%').margin({top:20});Slider({value:this.protein,min:30,max:200,step:10}).onChange((v:number)=>this.protein=Math.round(v)).width('100%');Button('计算食材量').width('100%').height(50).margin({top:24}).onClick(()=>this.calc());Text(this.result).fontSize(20).fontWeight(FontWeight.Bold).textAlign(TextAlign.Center).width('100%').padding(22).margin({top:16}).backgroundColor('#F0FAF4').borderRadius(10)}.width('100%').height('100%').padding(24).backgroundColor('#FFFFFF')}}
该片段参与按人数计算蔬菜与蛋白食材总克数。
二十二、运行案例
22.1 默认数据
使用2 人、每人蔬菜 120g、蛋白 80g。
观察 people、greens、protein、result 与结果。
确认蔬菜总量和蛋白总量可复算。
22.2 最小边界
人数公共因子、双乘法和整数范围
观察 people、greens、protein、result 与结果。
确认蔬菜总量和蛋白总量可复算。
22.3 临界输入
围绕人数公共因子、双乘法和整数范围验证阈值。
观察 people、greens、protein、result 与结果。
确认蔬菜总量和蛋白总量可复算。
22.4 连续操作
连续执行 calc。
观察 people、greens、protein、result 与结果。
确认蔬菜总量和蛋白总量可复算。
| 验证维度 | 当前实现 | 作用 |
|---|---|---|
| 状态 | people、greens、protein、result | 保存数据 |
| 规则 | 人数分别乘每人蔬菜和每人蛋白克数,生成双结果 | 生成反馈 |
| 边界 | 人数公共因子、双乘法和整数范围 | 保护结果 |
| 结果 | 蔬菜总量和蛋白总量 | 呈现结论 |
二十三、总结
沙拉分量计算使用鸿蒙 ArkTS 完成了“按人数计算蔬菜与蛋白食材总克数”闭环。
核心规则是:人数分别乘每人蔬菜和每人蛋白克数,生成双结果。
状态字段为 people、greens、protein、result。
状态建模、业务函数 与 声明式界面 共同保证结果稳定。
相关资源:
更多推荐

所有评论(0)