简体   繁体   English

关于用循环构造的东西的Java mutator方法

[英]Java mutator method on something that was constructed with a loop

So I have a class that I constructed with a loop. 所以我有一个用循环构造的类。 In the constructor I have two while loops which make "grid" of circles. 在构造函数中,我有两个while循环,它们构成了圆圈的“网格”。 It's supposed to be a scarf for class but I'm calling it chainmail because that sounds cooler. 它应该是一个围巾上课,但我称之为链甲,因为这听起来更酷。 Anyways we're supposed to be able to change the color of the scarf with a different (client) class. 无论如何,我们应该能够用不同的(客户)类改变围巾的颜色。 I obviously need to add a mutator method to change the color. 我显然需要添加一个mutator方法来改变颜色。 Luckily there is a mutator for this in objectdraw called setColor(). 幸运的是,在objectdraw中有一个名为setColor()的mutator。 It works just fine, except when I try to add it to this class it only changes the last circle in the grid. 它工作正常,除非我尝试将其添加到此类时它只更改网格中的最后一个圆。 I know why this happens, but I don't know how to fix it. 我知道为什么会这样,但我不知道如何解决它。 I've commented out the "typical" mutator we've been using in class. 我已经评论过我们在课堂上一直使用的“典型”变种器。

EDIT: Sorry for the confusion guys... this is just the class, I have a client that calls a new ChainMail() and then does a .setColor() on it but it only changes the last framedoval instead of all of them. 编辑:抱歉困惑的人...这只是类,我有一个客户端调用一个新的ChainMail(),然后对它做一个.setColor()但它只更改最后一个framedoval而不是所有这些。 That's the problem 那就是问题所在

import objectdraw.*;
import java.awt.*;

public class ChainMail {

  private FramedOval link;

  public ChainMail(int rows,int links,
                   Location p,Color rgb,
                   DrawingCanvas c) {

    double numRows = 0;

    // create the number of rows specified
    while (numRows < rows) {

      double numLinks = 0;

      // create the number of links specified
      while (numLinks < links) {
        link = new FramedOval(p,12,12,c);
        link.setColor(rgb);

        // update the position
        p.translate(8,0);
        numLinks++;
      }

      // move position back to front col and down one row
      p.translate(-8*links,8);
      numRows++;

    }

  }

  public ChainMail(int rows,int links,Location p,DrawingCanvas c) {
    this(rows,links,p,Color.BLACK,c);
  }

  /* this doesn't work, only changes last circle
   * public void setColor(Color c) {
   *   link.setColor(c);
   * }
   */

}

If you move it to class (as shown in commented code) then from where do you call this function ? 如果你把它移到class(如注释代码中所示)那么你从哪里调用这个函数? I think this is what making the difference. 我认为这是有所作为的。 Suppose you are doing following : 假设您正在执行以下操作:

  1. remove "link.setColor(rgb);" 删除“link.setColor(rgb);” from inside the while loop 来自while循环
  2. Uncomment public void setColor(Color c) function 取消注释public void setColor(Color c)函数
  3. Create instance of ChainMail (say myChainMail) 创建ChainMail的实例(比如myChainMail)
  4. Call myChainMail.setColor(c) 调用myChainMail.setColor(c)

This scenario will give the result which you have reported ie only last circled gets colored. 此方案将给出您已报告的结果,即仅最后圈出的颜色。 There are 2 ways to solve the issue : 有两种方法可以解决这个问题:

  1. First is what you are already doing 首先是你已经在做的事情
  2. Instead of "private FramedOval link;" 而不是“私人FramedOval链接;” create "private ArrayList linkList = new ArrayList; Now before while create link variable of type FramedOval. Then after you do link = new FramedOval(p,12,12,c); in while loop add this to above created ArrayList. Now in setColor method of your class iterate over all the items in arraylist and set color on those items 创建“private ArrayList linkList = new ArrayList;现在在创建类型为FramedOval的链接变量之前。然后在你做link = new FramedOval(p,12,12,c)之后;在while循环中将此添加到上面创建的ArrayList。现在在setColor中您的类的方法迭代arraylist中的所有项目并在这些项目上设置颜色

I say you're throwing them away because you do this: 我说你扔掉它们因为你这样做:

  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

But where do you do anything with the FramedOval that you've created? 但是你在哪里用你创建的FramedOval做任何事情? Every time you create a new FramedOval object and have link refer to it, the previous reference gets lost and is likely garbage collected; 每次你创建一个新的FramedOval对象并有链接引用它时,先前的引用会丢失并且可能是垃圾收集; it's discarded, which doesn't make sense. 它被丢弃了,没有意义。

Usually you'd do something like: 通常你会这样做:

  List<FramedOval> framedOvalList = new LinkedList<FramedOval>(); // or ArrayList
  while (numLinks < links) {
    link = new FramedOval(p,12,12,c);
    link.setColor(rgb);

    // *** here add the created object to some collection
    framedOvalList.add(link);

    // update the position
    p.translate(8,0);
    numLinks++;
  }

But I don't see you doing anything of the kind. 但是我没有看到你做任何类似的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM