我正在为一堂课编写程序。当我运行程序时,alphabet 的值永远不会保持不变,并且当我在我的子类中使用 toString() 时,alphabet 始终为空。我知道在第二个构造函数中 this.alphabet 等于我希望它等于但之后它失去了它的价值。奇怪的是 machineName 保持它的价值。我对 java 和 stackoverflow 还是很陌生,所以如果我做了一些愚蠢的事情,我很抱歉。我也不能改变我只能实现它的方法或变量。
public abstract class AbstractDFA{
private String machineName;
private String alphabet;
public AbstractDFA(String nameofmachine)
{
StringBuilder b = new StringBuilder();
for (int i = 0; nameofmachine.charAt(i) != '.'; i++)
{
b.append(nameofmachine.charAt(i));
}
machineName = b.toString();
}
public AbstractDFA(String nameofmachine, String alphabet)
{
this.alphabet = alphabet;
this.machineName = nameofmachine;
setAlphabet(this.alphabet);
}
public String toString()
{
return "Machine Name: " + machineName + "\nAlphabet: " + getAlphabet();;
}
public void setAlphabet(String alpha)
{
StringBuilder beta = new StringBuilder();
beta.append("{");
for (int i = 0; i < alpha.length(); i++)
{
beta.append(alpha.charAt(i));
if (i != (alpha.length() - 1))
{
beta.append(", ");
}
}
beta.append("}");
alphabet = beta.toString();
}
public String getAlphabet()
{
return alphabet;
}
}
公共类 DFA 扩展 AbstractDFA{
public DFA(String fileName){
super(fileName);
readDFA(fileName);
}
public DFA(String name, String alpha, int numStates, int start, int[][] delta, boolean[] finalstates){
super(name,alpha);
this.numberOfStates = numStates;
this.start = start;
this.delta = delta;
this.finalStates = finalstates;
}
}