英文:
java extended class inheritance
问题
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.returnTotal());
}
else if (read.equals("zero")) {
z.zero();
}
read = input.nextLine();
}
class:
public class Sum {
int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
input:
add
add
zero
add
get
add
get
end
output:
3
4
output wanted:
1
2
Shouldn't the subclass inherit the total and set it to zero? What am I doing wrong? I know I could just move the zero
into the main class but I want it to be in a separate class. Thanks for your help.
英文:
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add()
}
else if (read.equals("get")) {
System.out.println(s.returnTotal());
}
else if (read.equals("zero")) {
z.zero();
}
read = input.nextLine();
}
class:
public class Sum {
int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
input:
add
add
zero
add
get
add
get
end
output:
3
4
output wanted:
1
2
Shouldn't the subclass inherit the total and set it to zero? What am I doing wrong? I know I could just move the zero
into the main class but I want it to be in a separate class. thx for your help.
答案1
得分: 1
通过将您的total
变量声明为static
,您可以获得所需的输出。
class Sum {
static int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
英文:
By making your total
variable static
, you can get the desired output.
class Sum {
static int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
答案2
得分: 0
除了在名称中指出的一些问题,比如不要使用小写字母开头作为类名;我认为它不起作用的原因是因为你在 Sum
和 Sum.SetToZero
上使用了两个不同的变量实例。你不需要创建新的变量,因为 SetToZero
具有 Sum
的所有属性。我认为你应该将这部分改为:
Sum.SetToZero s = new Sum.SetToZero(); // 对所有操作使用 s
以下是修改后的 main
方法:
public static void main(String[] args) {
Sum.SetToZero s = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.get());
}
else if (read.equals("zero")) {
s.zero();
}
read = input.nextLine();
}
}
当我运行这段代码时,我得到了预期的输出:
src : $ java Sum
add
add
zero
add
get
1
add
get
2
end
英文:
Apart from things pointed out in names like not using lowecase letters to start your class name; I think the reason why it's not working is because you're using two different variable instances for Sum
and Sum.SetToZero
. You don't need to create a new variables since SetToZero
has all the attributes of Sum
. I think you should change this:
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Sum.SetToZero s = new Sum.SetToZero(); // use s for all operations
Here is how your modified main method would look:
public static void main(String[] args) {
Sum.SetToZero s = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.get());
}
else if (read.equals("zero")) {
s.zero();
}
read = input.nextLine();
}
}
When I ran this, I saw expected output:
src : $ java Sum
add
add
zero
add
get
1
add
get
2
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论