Java扩展示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4188091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java extends example
提问by Martin Kapfhammer
i have a java beginner question: Parent.print() prints "hallo" in the console, but also Child.print() prints "hallo". I thought it has to print "child". How can i solve this?
我有一个 Java 初学者问题:Parent.print() 在控制台中打印“hallo”,但 Child.print() 也会打印“hallo”。我认为它必须打印“孩子”。我该如何解决这个问题?
public class Parent {
private String output = "hallo";
public void print() {
System.out.println(output);
}
}
public class Child extends Parent {
private String output = "child";
}
采纳答案by Jon Skeet
Currently you've got two separate variables, and the code in Parent
only knows about Parent.output
. You need to set the value of Parent.output
to "child". For example:
目前你有两个单独的变量,并且代码Parent
只知道Parent.output
. 您需要将 的值设置Parent.output
为“child”。例如:
public class Parent {
private String output = "hallo";
protected void setOutput(String output) {
this.output = output;
}
public void print() {
System.out.println(output );
}
}
public class Child extends Parent {
public Child() {
setOutput("child");
}
}
An alternative approach would be to give the Parent class a constructor which took the desired output:
另一种方法是为 Parent 类提供一个接受所需输出的构造函数:
public class Parent {
private String output;
public Parent(String output) {
this.output = output;
}
public Parent() {
this("hallo");
}
public void print() {
System.out.println(output );
}
}
public class Child extends Parent {
public Child() {
super("child");
}
}
It really depends on what you want to do.
这真的取决于你想做什么。
回答by Jeremy
Child
doesn't have access to Parent
's output
instance variable because it is private
. What you need to do is make it protected
and in the constructor of Child
set output
to "child"
.
Child
无法访问Parent
的output
实例变量,因为它是private
. 您需要做的是protected
在Child
set output
to的构造函数中创建它"child"
。
In other words, the two output
variables are different.
换句话说,这两个output
变量是不同的。
You could also do this if you change output to be protected
in Parent
:
如果您将输出更改为protected
in ,您也可以这样做Parent
:
public void print(){
output = "child"
super.print();
}
回答by Mike Miller
The reason why child is not printing "child" is that in inheritance in java, only methods are inherited, not fields. The variable output
is not overridden by the child.
child 不打印“child”的原因是在java中的继承中,只继承了方法,而不是字段。该变量output
不会被孩子覆盖。
You could do it like this:
你可以这样做:
public class Parent {
private String parentOutput = "hallo";
String getOutput() {
return output;
}
public void print() {
System.out.println(getOutput());
}
}
public class Child extends Parent {
private String childOutput = "child";
String getOutput() {
return output;
}
}
Also, the String variables do not need to be different names, but I did so here for clarity.
此外,字符串变量不需要是不同的名称,但为了清楚起见,我在这里这样做了。
Another, more readable way would be to do this:
另一种更具可读性的方法是这样做:
public class Parent {
protected String output;
public Parent() {
output = "hallo";
}
public void print() {
System.out.println(output);
}
}
public class Child extends Parent {
public Child() {
output = "child";
}
}
In this example the variable is protected
, meaning it can be read from both the parent and child. The constructor of the classes sets the variable to the desired value. This way you only implement the print function once, and do not need a duplicate overridden method.
在这个例子中,变量是protected
,这意味着它可以从父级和子级读取。类的构造函数将变量设置为所需的值。这样您只需实现一次打印功能,并且不需要重复的重写方法。
回答by KostasA
When I was trying to figure out the extend keyword, I was using two classes. I'll hope that also will help you to understand the basic idea.
当我试图找出 extend 关键字时,我使用了两个类。我希望这也能帮助你理解基本思想。
Parent.java
父程序
public class Parent {
private int a1;
private int b1;
public Parent(int a, int b){
this.a1 = a;
this.b1 = b;
}
public void print() {
System.out.println("a1= " + this.a1 + " b1= " + this.b1);
}
}
Child.java
子程序
public class Child extends Parent {
public Child(int c1, int d1){
super(c1,d1);
}
public static void main(String[] args) {
Parent pa = new Parent(1,2);
pa.print();
Child ch = new Child(5,6);
ch.print();
}
}