java - 方法内部是否将布尔值分配给局部变量?
问题描述
我不太清楚,我们如何在方法中使用布尔值以及如何将其设置为 false。通常,如果您在方法中使用变量,则将其分配给局部变量,对布尔值执行相同操作还是仅使用和更改它们。
为什么我们要顺便给一个属性分配一个局部变量呢?如果我们不这样做会发生什么?
如何将设置器用于布尔值?
有人借书后,我现在如何减小数组列表的大小?
public class Governance{
ArrayList<Intenger> Books=new ArrayList<Integer>(10);
private boolean active = true;
public void borrowBook{
/** does I assign active to a local variable? why do we assign attributes
to a local variable? what happens if we dont?why I cant just use and
initialize (e.g. set a value) the attribute as a I defined(best practices)**//
boolean checkStatus = isActive(); //correct?
if(Books.isEmpty()){
//how to I set active to false (best practices)
checkStatus = myFunction(); //correct or are there better ways to do it?
}
else if(!Books.isEmpty() && isActive){
System.out.println("You can borrow that book");
else{
System.out.println("You can not borrow that book");
}
}
public static boolean myFunction(){
return false;
}
public boolean isActive() {
return active;
}
//what is a case where I use this and how?
public void setActive(boolean active) {
this.active = active;
}
}
解决方案
- A
boolean
是原始数据类型。可以像其他原语一样赋值:boolean active = true; active = false;
也可以比较active == true
用作条件if(active) { // do stuff }
不需要active == true
active
在类Governance
中具有覆盖整个类的范围。
如果其他一些类想要更改它的值,则需要调用yourGovernanceInstance.setActive(false);
,因此我们将active
此类实例的属性更改为false
.- 见上面的例子。
我很难使用格式化程序共享代码,请参阅此链接以获得简洁的版本。仅当目标链接过期时才在下方。
import java.util.ArrayList;
import java.util.List;
import java.lang.Integer;
public class Governance {
// usually we write variables in lower case.
private List<Integer> books = new ArrayList<Integer>();
// private member. To change it's value it's best practice to call setters and getters to change the value.
private boolean active = true;
public void borrowBook() {
if(books.isEmpty()){
// I use "this" to refer to this class's method "isActive()"
} else if(!books.isEmpty() && this.isActive()){
System.out.println("You can borrow that book");
} else {
System.out.println("You can not borrow that book");
}
}
public boolean isActive() {
return active;
}
// You habe a class that has many Governance instances, e.g.
// Governance myGov = new Governance();
// myGov is no more active, so you call: myGov.setActive(false);
public void setActive(boolean active) {
this.active = active;
}
}
你也可以只使用else if(!books.isEmpty() && active)
.
要看。通常你会在你的上下文中
ArrayList
包含对象。Book
您必须确定某人想要拿走的书。公共类书 { 私人 int id; 私有字符串标题;私人布尔活动;
public Book(int id, String title, boolean active) { this.id = id; this.title = title; this.active = active; } // Setters and Getters
}
在你的Governance
课堂上:
public boolean canBorrowBookWithId(int id) {
Book bookToBeBorrowed = books.get(id);
boolean canBorrow = true;
// If book can is active, it can't be borrowed.
if(bookToBeBorrowed.IsActive()){
canBorrow = false;
} else {
// If not; set it to be and lend it out :-)
bookToBeBorrowed.setActive(true);
}
return canBorrow;
}
旁注:canBorrowBookWithId() 检查这本书是否可用并设置它的状态。根据干净代码的衡量标准,一个方法一次只能执行其中一个操作!
你正在努力学习Object oriented programming
它很难开始,但最终你会明白的:-)
推荐阅读
- javascript - 网站图片库加载缓慢且重叠
- javascript - React Native 警告:在现有状态转换期间无法更新(例如使用渲染)
- java - 在这个java程序中使用嵌套for循环在java中添加逐渐减少的空格,初学者问题
- django - Django:TestCase的permission_required大小限制?(PermissionRequiredMixin)
- c++ - 如何在 C++ 中显示将结构数据作为数据获取的链表?
- faunadb - FaunaDB 自定义角色,仅限制对自己数据的访问
- r - 多个回归模型来自 R 中的列表
- r - 用R从字符中提取行、文本和数字
- c# - Automapper 生成一个奇怪的 SQL 查询
- java - Golf return output logic issue