我如何解决此问题等于原始类型(int) [英] How can i fix this equals on primitive type(int)

查看:80
本文介绍了我如何解决此问题等于原始类型(int)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的图书馆应用代码

heres my code for a library application

package com.accenture.totalbeginner;

public class Person {
  private String name;
  private int maximumbooks;

  public Person() {
    name = "unknown name";
    maximumbooks = 3;
  }

  public    String getName() {
    return name;
  }

  public void setName(String anyname)   {
    name = anyname;
  }

  public int getMaximumbooks() {
    return maximumbooks;
  }

  public void setMaximumbooks(int maximumbooks) {
    this.maximumbooks = maximumbooks;
  }

  public String toString() {
    return this.getName() + " (" + this.getMaximumbooks()  + " books)";
  }

  public boolean equals(Person p1) {
    if(!this.getName().equals(p1.getName()))    {
        return false;
    }

    if(!this.getMaximumbooks().equals(p1.getMaximumbooks()))    {
        return false;
    }

    return true;
  }
}

(!this.getMaximumbooks().equals(p1.getMaximumbooks())) 

这就是说不能在原始类型(int)上调用.equals

this is saying cannot invoke .equals on primitive type(int)

我知道这是什么意思,但是我已经尝试了所有方法,而且我想不出如何纠正它.

I know what that means, but I have tried everything and I can't think how to correct it.

如果您需要其他类中的任何代码,请告诉我.

If you need any of the code from other classes let me know.

推荐答案

equals()用于Object s(StringInteger等)

对于基元(例如intbooleanchar等),您必须使用==

For primitives like int, boolean, char etc, you have to use ==

这篇关于我如何解决此问题等于原始类型(int)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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