布尔VS布尔(S)为trilean开关 [英] Boolean vs boolean(s) as trilean switches

查看:109
本文介绍了布尔VS布尔(S)为trilean开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我只是碰到了一些东西。我有一些匿名类(事件监听器)。他们是从数据库参数。我所做的是这样的:

  buttonA.setOnTouchListener(新View.OnTouchListener(){
                        私人布尔isActive = NULL;
                        私人布尔isTrigger;
                        私人诠释onLevel;
                        私人诠释offlevel中;
                        私人诠释chIdx;                        @覆盖
                        公共布尔onTouch(视图V,MotionEvent事件){
                            如果(isActive == NULL){
                                光标btnSettings = dbHelper.getButtonsTable()fetchButton(1,提供ProfileID,CURRENTMODE)。
                                ...
...
                            }
                            返回true;
                        }

它是一种良好实践使用Boolean对象为trilean开关(它如果听者尚未参数化的空值)或者我应该用两个布尔变量...也许一个整数<? / p>

你有和想法?


解决方案

最好的是,使用具有三种状态的准确的描述一个类型(可能是一个枚举)。布尔值不给大量的信息是谁打来的(作为一个三态使用时尤为突出)功能的人。

 公共枚举ActiveStatus {
  上,
  关,
  未知
}

I'm developing an Android application and I just ran into something. I have some anonymous classes (event listeners). They are parameterized from the database. What I did is this:

buttonA.setOnTouchListener(new View.OnTouchListener() {
                        private Boolean isActive = null;
                        private boolean isTrigger;
                        private int onLevel;
                        private int offLevel;
                        private int chIdx;

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (isActive == null) {
                                Cursor btnSettings = dbHelper.getButtonsTable().fetchButton(1, profileId, currentMode);
                                ...
...
                            }
                            return true;
                        }

Is it considered a good practice to use the Boolean object as a trilean switch (it has a null value if the listener haven't been parameterized yet) or I should use two boolean variables...or maybe an integer?

Do you have and ideas?

解决方案

Best yet, use an type (probably an enum) with accurate descriptions of the three states. Booleans don't give a lot of information to the person who is calling the function (especially when used as a tristate).

public enum ActiveStatus {
  On,
  Off,
  Unknown
}

这篇关于布尔VS布尔(S)为trilean开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆