首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在java中更改类的字段

在java中更改类的字段
EN

Stack Overflow用户
提问于 2021-01-03 21:18:16
回答 1查看 97关注 0票数 1

所以,我仍然在学习java和编码,所以分辨率可能是显而易见的,但我就是看不见。我在写一段关于星星和星座的代码。

代码语言:javascript
复制
package com.company;
import java.util.*;


public class Main {

    static public class Constellation {
        public List<Star> constellation;
        public String nameOfConstellation;

        public Constellation(List<Star> constellation, String nameOfConstellation) {
            this.constellation = constellation;
            this.nameOfConstellation = nameOfConstellation;
        }

        public List<Star> getConstellation() {
            return constellation;
        }
    }

    static public class Star {

       // private String categoryName;
        private Constellation constellation;
        private String nameOfConstelation;

        public String getCategoryName() {
            int index = constellation.getConstellation().indexOf(this);
            String categoryName;
            return categoryName = GreekLetter.values[index] + " " + this.constellation.nameOfConstellation;
        }


        public void deleteStar(Star x) {
            this.constellation.constellation.remove(x);


        }
    }
    public enum GreekLetter {

        alfa,
        beta,
        gamma,
        delta,
        epsilon,
        dzeta,
        eta;

        static public final GreekLetter[] values = values();
    }

    public static void main(String[] args)
    {
        Star x = new Star();
        List<Star> fishCon = new ArrayList<>();
        Constellation Fish = new Constellation(fishCon, "Fish");
        x.constellation=Fish;
        fishCon.add(x);
        x.getCategoryName();


        Star y = new Star();
        y.constellation=Fish;
        fishCon.add(y);

        y.getCategoryName();
        x.deleteStar(x);


        for (Star w : Fish.constellation)
        {
            System.out.println(w.getCategoryName());
        }

    }
}

我的观点是删除一颗星后更新字段categoryName。categoryName值是按照添加另一个星号的顺序设置的。例如,我有第一颗星-名字将是阿尔法+ nameOfConstelation。第二颗星- Beta + nameOfConstelation。当我调用方法deleteStar()时,我想在星座中更新我的恒星的所有categoyName。在deleteStar()中调用方法可能是因为setCategoryName中的add()不起作用。我真的很感激任何提示!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-03 21:42:18

由于这似乎是家庭作业,我不是在这个答案中发布代码,而是给出一些建议,这些建议可以帮助您创建自己的可行代码:

创建一个名为“星座”的类,该类保存List<Star> starList = new ArrayList<>();

  • Give星座中的恒星-- public List<Star> getStarList()方法

  • ,给每个恒星一个星座字段来保存包含此星体的星座,给每个恒星一个获取星座对象的getCategoryName()方法,使用for-循环遍历其starList,直到找到this星,然后根据列表中的星星索引返回适当的名称。H 212H 113因此,如果从starList中删除一颗恒星,星座所持有的所有其他恒星的类别名称将自动和动态地更新

另外,

  • --您可以给星座一个public void deleteStar(Star star)方法,在它从starList
  • 中移除星星参数时,还可以给Star一个public void deleteFromConstellation()方法,检查星座字段、星座,如果不是null,则调用constellation.deleteStar(this);,然后将星座字段设置为null
  • --去掉星空中的private String categoryName;字段。这应该是一个计算字段,意思是public String getCategoryName()不返回一个字段,而是一个基于代码的字符串(如上面所述)。
  • 它首先检查星体的星座字段不是空的
  • ,然后在星座的starList中获取星的索引(我已经给星座类一个public int getIndexOfStar(Star star)方法。
  • 然后使用它、GreekLetter类和constellation.getName()方法来创建返回< code >H 235F 236的字符串。)

好了。

既然您已经知道了这一点,这是另一种编码它的方法:

代码语言:javascript
复制
public class SkyMain {
    public static void main(String[] args) {
        Constellation fish = new Constellation("Fish");

        Star x = new Star();
        Star y = new Star();
        fish.addStar(x);
        fish.addStar(y);
        
        System.out.println("before removing x");
        System.out.println("x category name: " + x.getCategoryName());
        System.out.println("y category name: " + y.getCategoryName());
        System.out.println("fish constellation: " + fish);
        
        fish.removeStar(x);
        System.out.println();

        System.out.println("after removing x");        
        System.out.println("x category name: " + x.getCategoryName());
        System.out.println("y category name: " + y.getCategoryName());
        System.out.println("fish constellation: " + fish);
    }
}
代码语言:javascript
复制
public class Star {
    private Constellation constellation;
    
    public void setConstellation(Constellation constellation) {
        this.constellation = constellation;
    }
    
    public void removeFromConstellation() {
        if (constellation != null) {
            constellation.removeStar(this);
        }
    }
    
    public String getCategoryName() {
        if (constellation != null) {
            int index = constellation.getIndexOfStar(this);
            return GreekLetter.getGreekLetter(index).getName() + " " + constellation.getName();
        } else {
            return "";
        }
    }
    
    @Override
    public String toString() {
        return getCategoryName();
    }
}
代码语言:javascript
复制
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Constellation implements Iterable<Star> {
    private String name;
    private List<Star> starList = new ArrayList<>();
    
    public Constellation(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public List<Star> getStarList() {
        return starList;
    }
    
    public void addStar(Star star) {
        starList.add(star);
        star.setConstellation(this);
    }
    
    public void removeStar(Star star) {
        if (starList.contains(star)) {
            starList.remove(star);
            star.setConstellation(null);
        }
    }
    
    public int getIndexOfStar(Star star) {
        return starList.indexOf(star);
    }

    @Override
    public Iterator<Star> iterator() {
        return starList.iterator();
    }

    @Override
    public String toString() {
        return "Constellation [name=" + name + ", starList=" + starList + "]";
    }
    
    
}
代码语言:javascript
复制
public enum GreekLetter
{
    ALPHA("alpha", 0),
    BETA("beta", 1),
    GAMMA("gamma", 2),
    DELTA("delta", 3),
    EPSILON("epsilon", 4),
    ZETA("zeta", 5),
    ETA("eta", 6);
    
    private String name;
    private int index;
    
    private GreekLetter(String name, int index) {
        this.name = name;
        this.index = index;
    }
    
    public String getName() {
        return name;
    }
    
    public int getIndex() {
        return index;
    }
    
    public static GreekLetter getGreekLetter(int index) {
        if (index < 0 || index > values().length) {
            throw new IllegalArgumentException("for index " + index);
        } else {
            return values()[index];
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65555033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档