java - 在java中更改类的字段
问题描述
所以,我还在学习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 值是按添加另一个星的顺序设置的。例如,我有第一颗星 - 名称将是 Alfa + nameOfConstelation。第二颗星 - Beta + nameOfConstelation。当我调用方法 deleteStar() 时,我想更新星座中我的星星的所有 categoyName。调用 deleteStar() 中的方法不起作用可能是由于 setCategoryName 中的 add()。我真的很感激任何提示!
解决方案
由于这似乎是家庭作业,因此我不会在此答案中发布代码,而是提供可以帮助您创建自己的可行代码的建议:
- 创建一个名为 Constellation 的类,将星星保存在一个
List<Star> starList = new ArrayList<>();
- 给星座一个
public List<Star> getStarList()
方法 - 给每个 Star 一个 Constellation 字段来保存包含该 Star 的 Constellation
- 为每个 Star 提供一个
getCategoryName()
获取 Constellation 对象的方法,使用 for 循环遍历其 starList 直到找到this
Star,然后根据列表中 Star 的索引返回适当的名称。 - 因此,如果从 starList 中删除一个 Star,则该 Constellation 拥有的所有其他 Star 的类别名称将自动动态更新
还,
- 您可以给 Constellation 一个
public void deleteStar(Star star)
方法,从它的 starList 中删除 Star 参数 - 您还可以为 Star 提供一个
public void deleteFromConstellation()
方法,用于检查其 Constellation 字段,constellation,如果不为 null,则调用constellation.deleteStar(this);
然后将 constellation 字段设置为 null - 摆脱
private String categoryName;
Star 中的领域。这应该是一个计算字段,这意味着public String getCategoryName()
它不返回一个字段,而是一个基于代码的字符串(如上所述)。 - 它首先检查 Star 的星座字段不为空
- 然后它在 Constellation 的 starList 中获取 Star 的索引(我给了我的 Constellation 类一个
public int getIndexOfStar(Star star)
方法。 - 然后它使用这个、GreekLetter 类和
constellation.getName()
方法来创建要返回的字符串
完毕。
既然你已经弄清楚了,这是另一种编码方式:
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];
}
}
}
推荐阅读
- ruby-on-rails - Ruby on Rails - 将参数附加到 URL 的末尾
- node.js - 通过 JS 推广 TFS npm 包
- javascript - Angular - 使用附加的集合测试@Input
- excel - 在 Excel 中提取字符串 Beetwen 2 字符
- azure - 从 xamarin 移动应用程序动态更改 azure 数据库连接字符串
- javascript - 用ajax重新加载php文件后保持div内的滚动位置
- python - 从 for 循环中删除注释
- android - 应用显示和移动显示不匹配反应原生
- java - eclipse无法自动导入某些类
- javascript - 从具有父子结构的对象数组中删除对象