由优快云争议的问题想到的!--->(boolean值带来的效率问题)

本文通过实验对比了Java中boolean变量与true/false直接比较的效率差异,并得出结论:两种方式的效率基本一致,差异源于CPU处理时的占用时间不同。

这是一个优快云上大家有所争议的问题,在这里给出个人的见解和测试。如果觉得有什么不对的地方或者你有什么更好的想法欢迎拍砖!废话不多说看一下几种情况!

1: IDE的识别问题 :虽然作为程序员的我们基本上都有一套自己的比较正确的写法,但是作为新人也许会问你这些问题,如果知道了这个的话就不必那么的感到惊讶!

       

(为了大家能看到报错的信息这里是截图;还望理解)

2 : 测试两种写法的效率问题

package 优快云;

/**
 * boolean 效率问题!!!
 * 
 * @author qiuqiu 注: 优快云所看到的一个问题虽然我们也许都不这么写但是如果如果被问到你知道的话也不会那么觉得不可思议
 * 
 */
public class BooleanTest {
	public static void main(String[] args) {
		testEff_priVer(false);
		testEff_endVer(false);
	}
	public static void testEff_priVer(boolean bConn) {
		long pri_time = System.currentTimeMillis();
		System.out.println("pri_time = " + pri_time);
		if (bConn == false) {
			int count = 0;
			for (int i = 0; i < 100000000; i++) {
				count++;
			}
		}
		System.out
				.println(("last_time = " + (System.currentTimeMillis() - pri_time)));
	}

	public static void testEff_endVer(boolean bConn) {
		long pri_time = System.currentTimeMillis();
		System.out.println("pri_time = " + pri_time);
		if (false == bConn) {

			int count = 0;
			for (int i = 0; i < 100000000; i++) {
				count++;
			}
		}
		System.out
				.println(("last_time = " + (System.currentTimeMillis() - pri_time)));
	}
}


结果1: (后面效率更高)

pri_time = 1313719099953
ver is pri : last_time = 78
pri_time = 1313719100031
ver is end :last_time = 62

结果2:(效率相同)

pri_time = 1313719144937
ver is pri : last_time = 78
pri_time = 1313719145015
ver is end :last_time = 78

结果3 :(前面效率更高)

pri_time = 1313719201718
ver is pri : last_time = 63
pri_time = 1313719201781
ver is end :last_time = 78

如果你多测试几次可能就会出现结果3了,所以在此给出一个结论: 其实这两种的效率是一样的。(只是在处理的时候CPU占用的时间导致了不同结果)

3:其实还有就是测试true 与 false 与变量比较的情况!以及 if(bconn = =true) ,与if(bconn)

    结果如2所得出的结论,效率其实是一样的,只是作为程序员的我们有了if(bconn) 就没有任何的必要去写if(bconn = =true) 这也许就是程序员的不成文规范罢!

以上结论纯属个人测试所得。如果有什么异议欢迎拍砖!

       


 

     

<think>根据用户的问题,用户正在使用uni-appX开发,并询问两个具体问题: 1. uni-appX模板结构中scroll-view的使用方式 2. TestTypeComponentPublicInstance类型引用写法 结合引用内容,我们逐步解答: ### 一、uni-appX 中 scroll-view 组件的使用方式 在uni-appX中,`scroll-view`是一个可滚动视图容器,用于实现局部滚动区域。与Web开发中的div滚动不同,它提供了更接近原生滚动的体验。 #### 基本结构 ```vue <scroll-view scroll-y="true" style="height: 300rpx;" @scroll="handleScroll" > <view v-for="item in 50" :key="item">第{{item}}行</view> </scroll-view> ``` #### 关键属性说明 | 属性 | 类型 | 说明 | |--------------|-----------|----------------------------------------------------------------------| | `scroll-y` | Boolean | 允许纵向滚动(需设置固定高度)[^1] | | `scroll-x` | Boolean | 允许横向滚动(需设置固定宽度) | | `scroll-top` | Number | 设置竖向滚动条位置 | | `@scroll` | Event | 滚动时触发的事件,可通过`event.detail.scrollTop`获取滚动位置[^2] | #### 注意事项 1. **必须设置尺寸**:纵向滚动需设置`height`,横向滚动需设置`width`(推荐使用rpx单位) 2. **性能优化**:长列表建议配合`<recycle-list>`使用(App端专用)[^3] 3. **平台差异**:H5端部分样式(如滚动条样式)可能与原生有差异 ### 二、TestTypeComponentPublicInstance 类型引用写法 在uni-appX中使用TypeScript时,如果需要引用组件的实例类型,可通过以下方式: #### 1. 定义组件类型(.uts文件) ```typescript // 定义组件公共实例类型 export interface TestTypeComponentPublicInstance { method1: () => void; property1: string; } ``` #### 2. 在页面中引用类型 ```vue <script lang="uts"> // 导入类型定义 import { TestTypeComponentPublicInstance } from '@/components/test-component.uts' export default { components: { // 注册组件(假设组件名为test-type) 'test-type': () => import('@/components/test-type.uvue') }, methods: { handleRef(ref: TestTypeComponentPublicInstance | null) { if (ref) { ref.method1() // 调用组件方法 console.log(ref.property1) // 访问组件属性 } } } } </script> <template> <test-type ref="handleRef"></test-type> </template> ``` #### 关键说明 | 要点 | 说明 | |--------------------------|----------------------------------------------------------------------| | **组件实例类型** | 通过`interface`定义组件对外暴露的方法和属性 | | **模板引用** | 使用`ref`属性绑定到函数(非字符串)以获得强类型实例[^4] | | **空处理** | 使用`| null`处理组件未挂载情况 | | **组件导入** | 动态导入组件确保TreeShaking(`() => import(...)`) | ### 三、完整示例代码 ```vue <template> <!-- 滚动区域示例 --> <scroll-view scroll-y :style="{ height: windowHeight + 'px' }" @scroll="onScroll" > <view class="item" v-for="i in 100" :key="i">列表项 {{i}}</view> </scroll-view> <!-- 自定义组件类型引用 --> <test-type ref="handleTestRef"></test-type> </template> <script lang="uts"> import { TestTypeComponentPublicInstance } from '@/components/test-type.uts' import { getWindowInfo } from 'uni-app' export default { components: { 'test-type': () => import('@/components/test-type.uvue') }, data() { return { windowHeight: 0 } }, onLoad() { // 获取屏幕高度(减去安全区域) const info = getWindowInfo() this.windowHeight = info.windowHeight - 50 // 减去顶部导航栏高度 }, methods: { onScroll(e: UniApp.ScrollViewEvent) { console.log('滚动位置:', e.detail.scrollTop) }, handleTestRef(ref: TestTypeComponentPublicInstance | null) { if (ref) { ref.showToast('组件引用成功') } } } } </script> <style> .item { height: 100rpx; line-height: 100rpx; border-bottom: 1rpx solid #eee; padding-left: 30rpx; } </style> ``` ### §§ 相关问题 §§ 1. 如何在uni-appX中实现下拉刷新功能? 2. uni-appX的scroll-view与普通view滚动有什么区别? 3. 如何定义全局组件类型以便多页面复用? 4. uni-appX中如何监听原生滚动事件? > 提示:uni-appX的文档仍在持续完善中,遇到问题时建议查阅最新官方示例[^1][^3]。 [^1]: uni-appX官方文档 - 组件 scroll-view [^2]: uni-appX事件系统说明 [^3]: 性能优化建议 - 长列表渲染 [^4]: 组件模板引用规范</think>### uni-appX 中 `scroll-view` 使用指南 #### 一、`scroll-view` 基础用法 在 uni-appX 中,`scroll-view` 是用于创建可滚动视图的核心组件,支持横向和纵向滚动: ```vue <template> <!-- 纵向滚动(必须设置固定高度) --> <scroll-view scroll-y style="height: 500rpx"> <view v-for="item in 100" :key="item" class="item">列表项 {{ item }}</view> </scroll-view> <!-- 横向滚动(必须设置固定宽度) --> <scroll-view scroll-x style="width: 100%; white-space: nowrap"> <view v-for="item in 10" :key="item" class="h-item">标签 {{ item }}</view> </scroll-view> </template> <style> .item { height: 100rpx; line-height: 100rpx; border-bottom: 1rpx solid #eee; } .h-item { display: inline-block; width: 200rpx; margin-right: 20rpx; background: #f0f0f0; } </style> ``` #### 二、关键属性说明 | 属性 | 类型 | 说明 | |---------------------|-----------|----------------------------------------------------------------------| | `scroll-y` | Boolean | 启用纵向滚动(需设置固定高度)[^1] | | `scroll-x` | Boolean | 启用横向滚动(需设置固定宽度) | | `scroll-top` | Number | 设置竖向滚动条位置(单位 rpx) | | `scroll-left` | Number | 设置横向滚动条位置 | | `scroll-with-animation` | Boolean | 滚动时使用平滑动画(默认 false) | | `enable-back-to-top`| Boolean | iOS 点击顶部状态栏返回顶部(默认 false) | #### 三、事件监听 ```vue <scroll-view scroll-y style="height: 80vh" @scroll="handleScroll" @scrolltolower="loadMore" > <!-- 内容 --> </scroll-view> <script lang="ts"> export default { methods: { handleScroll(e: UniApp.ScrollViewEvent) { console.log('滚动位置:', e.detail.scrollTop) }, loadMore() { console.log('触底加载更多') } } } </script> ``` ### TestTypeComponentPublicInstance 类型引用 #### 一、组件实例类型声明 在 uni-appX 中,使用 TypeScript 声明组件实例类型: ```typescript // types/components.d.ts import { ComponentPublicInstance } from 'vue' declare module 'vue' { interface ComponentPublicInstance { // 自定义组件方法 customMethod(): void // 自定义组件属性 customProperty: string } } // 导出具体组件类型 export type TestTypeComponentPublicInstance = ComponentPublicInstance & { validate: () => boolean reset: () => void } ``` #### 二、在页面中使用类型 ```vue <script lang="ts"> import { defineComponent, ref } from 'vue' import type { TestTypeComponentPublicInstance } from '@/types/components' export default defineComponent({ setup() { const testCompRef = ref<TestTypeComponentPublicInstance | null>(null) const handleSubmit = () => { if (testCompRef.value?.validate()) { console.log('验证通过') } } return { testCompRef, handleSubmit } } }) </script> <template> <test-type-component ref="testCompRef" /> <button @click="handleSubmit">提交</button> </template> ``` #### 三、组件内部实现示例 ```vue <!-- TestTypeComponent.uvue --> <script lang="ts"> import { defineComponent } from 'vue' export default defineComponent({ expose: ['validate', 'reset'], // 暴露给父组件的方法 data() { return { formData: { /* ... */ } } }, methods: { validate() { // 验证逻辑 return true }, reset() { // 重置逻辑 } } }) </script> ``` ### 关键注意事项 1. **平台差异**: - Android 平台需要添加 `scroll-view` 的 `scroll-top` 初始才能生效 - iOS 滚动回弹效果需设置 `-webkit-overflow-scrolling: touch` 2. **性能优化**: ```vue <scroll-view scroll-y enable-flex :enhanced="true" <!-- 启用增强模式 --> :bounces="false" <!-- 禁用回弹 --> > <!-- 使用 recycle-list 优化长列表 --> <recycle-list for="item in longData"> <cell-item :item="item" /> </recycle-list> </scroll-view> ``` 3. **类型安全**: - 使用 `ref<TestTypeComponentPublicInstance | null>(null)` 避免空指针错误 - 通过 `expose` 明确暴露组件公共方法[^2] ###
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值