【发布时间】:2016-02-19 01:34:26
【问题描述】:
我正在尝试创建一个抽象类,并且在实例化该类的新对象时,我正在尝试在该类中设置一个变量。
abstract class TestClass { // Class I'm trying to change a variable in.
public String testString;
}
public class Main {
public static void main(String[] args) {
TestClass t = new TestClass() {
public String testString = "TEST"; // Where I'm trying to set it
};
System.out.println(t.testString);
}
}
输出:
null
我不希望它输出“TEST”。
有没有什么方法可以使用我正在尝试的方法来实现这一点?
【问题讨论】: