如何在Java中使用boolean? [英] How to use boolean in the Java?

查看:774
本文介绍了如何在Java中使用boolean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多介绍,如果符合要求则返回true,否则为false,以及如何详细使用它?

定义一个类,包括性别,性别定义为布尔类型(布尔值为true表示男性)。比'私人布尔性;'是吗?

I've seen a lot of introduction, return true if it meet the requirements , otherwise false, and how to use it in detail?
Define a class, including sex, sex was defined as a Boolean type (Boolean true to indicated male). Than 'private Boolean sex;' Is that right?

推荐答案

检查这个

http://en.wikipedia.org/wiki/Boolean_data_type [ ^ ]

Check this
http://en.wikipedia.org/wiki/Boolean_data_type[^]
来自wiki:

在计算机科学中,布尔值或逻辑数据类型是一种数据类型,有两个值(通常表示为真和假),旨在表示逻辑和布尔代数的真值。

In computer science, the Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra.



如上所述, boolean 用于两个值的事物(真/假)。

例如,如果要检查 IsActive IsAdmin IsNegativeStock IsEmpty , IsFull 等,

请参阅下面的示例。


As stated above, boolean is for two values things(true/false).
For example, if you want to check the status of something like IsActive, IsAdmin, IsNegativeStock, IsEmpty, IsFull, etc.,
See the sample one below.

bool bAdmin;

public void SomeMethod()
{
  bAdmin = IsAdmin();
}
public bool IsAdmin()
{
//Code for checking the user is admin or not
//true - Admin user, false - Normal user
}



在您的情况下,Gender不适合布尔值。以下是性别字段的值。


In your case, Gender is not suitable for boolean. Following are values for Gender field.

Male
Female
Transgender(Transman, Transwomen)
Unspecified
etc.,(I forgot one more value)



我看到很少有网站显示上面提到的性别下拉值,因此性别不适合布尔值。

在数据库方面,我不使用数据类型,而是使用其他数据类型,如char或int取决于数据。因为在过去,我们已经为某些列使用了bit数据类型,但在一种情况下,最终用户想要存储更多的值,因此我们必须使用其他一些数据类型。


I have seen few websites that showing Gender dropdown with above mentioned values so Gender is not suitable for boolean.
In database side, I don't use bit datatype, instead I use someother datatype like char or int depends upon data. Because in past, we have used bit datatype for some columns but in one situation end user wanted to store some more values, so we had to use some other data type.


引用:

定义一个类,包括性别,性别被定义为布尔类型(布尔值为true表示男性)。比'私人布尔性别',是吗?

Define a class, including sex, sex was defined as a Boolean type (Boolean true to indicated male). Than 'private Boolean sex;' Is that right?





这个命名真的很糟糕(甚至在政治上都不正确:-))。 />
您应该定义一个名为 female 的布尔类成员,其含义如下:

  • true :是女性
  • false :不是女性


  • That is really poor naming (and is not even politically correct :-) ).
    You should define a boolean class member named female with the following meaning:

    • true: is female.
    • false: is NOT female.

    • 你真的不应该在那个上使用布尔值,即使你将你的属性命名为'isMale',false也意味着它不是男性这可能是女性或其他像变性人。尝试看起来像Enums,可能更适合您的解决方案。
      You really shouldn't use Boolean on that one, even if you name ur attribute 'isMale', false means its not Male which can be Female or something else like transgender. Try to look something like Enums, might suit better your solution.


      这篇关于如何在Java中使用boolean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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