所以,我仍然在学习java和编码,所以分辨率可能是显而易见的,但我就是看不见。我在写一段关于星星和星座的代码。
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()不起作用。我真的很感激任何提示!
发布于 2021-01-03 21:42:18
由于这似乎是家庭作业,我不是在这个答案中发布代码,而是给出一些建议,这些建议可以帮助您创建自己的可行代码:
创建一个名为“星座”的类,该类保存List<Star> starList = new ArrayList<>();
public List<Star> getStarList()方法
getCategoryName()方法,使用for-循环遍历其starList,直到找到this星,然后根据列表中的星星索引返回适当的名称。H 212H 113因此,如果从starList中删除一颗恒星,星座所持有的所有其他恒星的类别名称将自动和动态地更新另外,
public void deleteStar(Star star)方法,在它从starListpublic void deleteFromConstellation()方法,检查星座字段、星座,如果不是null,则调用constellation.deleteStar(this);,然后将星座字段设置为nullprivate String categoryName;字段。这应该是一个计算字段,意思是public String getCategoryName()不返回一个字段,而是一个基于代码的字符串(如上面所述)。public int getIndexOfStar(Star star)方法。constellation.getName()方法来创建返回< code >H 235F 236的字符串。)好了。
既然您已经知道了这一点,这是另一种编码它的方法:
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);
}
}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();
}
}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 + "]";
}
}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];
}
}
}https://stackoverflow.com/questions/65555033
复制相似问题