java 无法调用原始类型 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14467187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:23:12  来源:igfitidea点击:

Cannot invoke primitive type int

javaarrays

提问by Mark Foley

Its coming up with "Cannot invoke getNumber() on the primitive type int. Why?

它提出了“无法在原始类型 int 上调用 getNumber()。为什么?

public Card findLargest() {

公共卡 findLargest() {

    ArrayList<Card> cardStack;}
    public Card getLargest() {
      Card largest;
      Card c;
      for (int i = 1, largest = cardStack.get(0); i < cardStack.size(); i++) {
        c = cardStack.get(i);
        if (largest.getNumber() > c.getNumber()) {
          largest = c;
          continue;
        } else if (largest.getNumber() == c.getNumber()) {
          if(largest.getSuit().equals("Diamonds"))
              largest = c;
            continue;

回答by Grambot

largest = c;

Since you need to have your largestvariable now reference the card with the new highest value you simply want to point the variable to it.

由于您largest现在需要让变量引用具有新最高值的卡片,因此您只需要将变量指向它即可。

Next time around largest.getNumber()will return what c.getNumber()is returning now.

下一次largest.getNumber()c.getNumber()返回现在返回的内容。

Make sure the declaration for Card largest;is outside of your loop however.

但是,请确保 for 的声明Card largest;在您的循环之外。

EDIT:

编辑:

I'll add some explanation for you and hopefully answer your questions.

我会为你添加一些解释,希望能回答你的问题。

When you declare an Object as such:

当你这样声明一个对象时:

Card c;

You're creating a reference variable that can refer to any object (or subclass) of the Card type. By creating an instance of that object...

您正在创建一个可以引用 Card 类型的任何对象(或子类)的引用变量。通过创建该对象的实例...

Card c = new Card();

you are reserving memory and initializing its children objects/primatives.

您正在保留内存并初始化其子对象/原始对象。

So when you write a function like this:

所以当你写一个这样的函数时:

List<Card> cardStack; //Just assuming you have a List of cards
public Card getLargest() {
  Card largest;
  Card c;
  for (int i = 1, largest = cardStack.get(0); i < cardStack.size(); i++) {
    c = cardStack.get(i);
    if (largest.getNumber() > c.getNumber()) {
      largest = c;
    } else if (largest.getNumber() == c.getNumber()) {
      if(...)//Check suits as you would
    }
  }
  return largest;
}

You are creating variables that refer to the Card objects that exist in the stack. There are no new memory allocations happening for the references (besides the memory they need to hold the reference).

您正在创建引用堆栈中存在的 Card 对象的变量。没有为引用分配新的内存(除了它们需要保存引用的内存)。

As a result, you'd have a list of card objects that you created elsewhere, and two references, cand largestthat simply point to the objects stored in a different location. Returning Cardfrom the function returns that reference so any actions you do on it effect the card it refers to.

其结果是,你必须卡的对象列表,你在其他地方创建,并且两个参考,clargest简单地指向存储在不同位置的对象。返回Card从基准,所以你就可以做任何动作影响它指的是卡在函数返回。

Alternatively you'd clonethe object and send back an identical, but distinct object where any modifications you do to it are not affecting the original Card. In many objects, the ability to only create new ones, never modifying an old one, is called Immutable. String objects are immutable objects for instance.

或者,您将clone对象发送回一个相同但不同的对象,您对它所做的任何修改都不会影响原始卡片。在许多对象中,只创建新对象而不修改旧对象的能力被称为不可变的。例如,字符串对象是不可变对象。

When I talk about needing to have Card largest;written outside the loop it means that the memory allocated to holding the reference only exists in the scope of the loop. If we were to do this:

当我谈到需要Card largest;在循环外写入时,这意味着分配给保存引用的内存仅存在于循环范围内。如果我们这样做:

for (....) {
  Card largest;
  //...
}

...each time the loop iterates the largestvariable is destroyed and recreated, removing and references or data we held in it. For my larger example above, we could put Card c;in the loop because we only care about what cis for the duration of the loop while largest matters later on. This is called Variable Scope.

...每次循环迭代largest变量都会被销毁和重新创建,删除我们保存在其中的引用或数据。对于我上面的较大示例,我们可以将其放入Card c;循环中,因为我们只关心c循环期间的内容,而最大的事情稍后才重要。这称为变量范围。