首页 > 解决方案 > 方法内部是否将布尔值分配给局部变量?

问题描述

  1. 我不太清楚,我们如何在方法中使用布尔值以及如何将其设置为 false。通常,如果您在方法中使用变量,则将其分配给局部变量,对布尔值执行相同操作还是仅使用和更改它们。

  2. 为什么我们要顺便给一个属性分配一个局部变量呢?如果我们不这样做会发生什么?

  3. 如何将设置器用于布尔值?

  4. 有人借书后,我现在如何减小数组列表的大小?

    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;
            }

        }

标签: javamethodsboolean

解决方案


  1. Aboolean是原始数据类型。可以像其他原语一样赋值: boolean active = true; active = false;
    也可以比较active == true
    用作条件if(active) { // do stuff }不需要active == true
  2. active在类Governance中具有覆盖整个类的范围。
    如果其他一些类想要更改它的值,则需要调用yourGovernanceInstance.setActive(false);,因此我们将active此类实例的属性更改为false.
  3. 见上面的例子。

我很难使用格式化程序共享代码,请参阅此链接以获得简洁的版本。仅当目标链接过期时才在下方。

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).

  1. 要看。通常你会在你的上下文中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它很难开始,但最终你会明白的:-)


推荐阅读