java Setter/getter 方法继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25109138/
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
Setter / getter method inheritance
提问by Armands9186
So I was studying about inheritance in java and I didn't get it quite clear. Suppose we have a parent method with a private variable and public setter getter methods. A subclass inherits these methods but not the private variable. Are these methods always connected with the superclass which also runs with the subclass? I can't declare a variable with the same name in the subclass, so inherited method would access it instead. I know doesn't work. Do the setter/getter methods always affect only values in objects where they are declared, even called from a subclass by inheritance?
所以我正在研究java中的继承,但我并没有很清楚。假设我们有一个带有私有变量和公共 setter getter 方法的父方法。子类继承这些方法,但不继承私有变量。这些方法是否总是与也与子类一起运行的超类相关联?我不能在子类中声明一个同名的变量,所以继承的方法会访问它。我知道不起作用。setter/getter 方法是否总是只影响声明它们的对象中的值,甚至通过继承从子类调用?
采纳答案by Leo
maybe an example can help you to understand
也许一个例子可以帮助你理解
public class C1 {
protected int x = 1;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public static void main(String[] args){
System.out.println(new C1().getX());
System.out.println(new C2().getX());
System.out.println(new C3().getX());
System.out.println(new C4().getX());
}
}
and
和
public class C2 extends C1{
}
and
和
public class C3 extends C2{
protected int x = 3;
}
and
和
public class C4 extends C3{
protected int x = 4;
@Override
public int getX() {
return x;
}
}
you'll get
你会得到
C1.x = 1
C2.x = 1
C3.x = 1
C4.x = 4
let's see what's happening.
让我们看看发生了什么。
- C1.x is 1 for obvious reasons
- C2.x is 1 because it's just the same as C1
- C3.x is 1 because getX() can see only the "x" declared at C1 (not intuitive for some people)
- C4.x is 4 because getX() is being overridden at C4, so it can see the "x" declared at C4
- 出于显而易见的原因,C1.x 为 1
- C2.x 是 1 因为它和 C1 一样
- C3.x 是 1 因为 getX() 只能看到在 C1 声明的“x”(对某些人来说不直观)
- C4.x 是 4 因为 getX() 在 C4 被覆盖,所以它可以看到在 C4 声明的“x”
回答by Devilhorn
Maybe this example will help too:
也许这个例子也会有帮助:
package testvehicle;
public class Car extends Vehicle
{
private int numDoors;
private int numWheels;
public Car(String manufacturer,String model,int maxSpeed,double price,int numWheels
,int numDoors)
{
super(manufacturer,model,maxSpeed,price);
this.numDoors=numDoors;
this.numWheels=numWheels;
}
public Car()
{
}
public int getNumDoors()
{
return numDoors;
}
public void setNumDoors(int numDoors)
{
this.numDoors = numDoors;
}
public int getNumWheels()
{
return numWheels;
}
public void setNumWheels(int numWheels)
{
this.numWheels = numWheels;
}
public String toString()
{
return ("Number of doors:"+numDoors+"\n"+"Number of wheels:"+numWheels+""
+ "\n"+
"Manufacturer:"+manufacturer+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n");
}
}
package testvehicle;
public class MotorCycle extends Vehicle
{
private String seat;
public MotorCycle(String manufacturer,String model,int maxSpeed,double price
,String seat)
{
super( manufacturer, model, maxSpeed, price);
this.seat=seat;
}
public MotorCycle()
{
}
public String getSeat()
{
return seat;
}
public void setSeat(String seat)
{
this.seat = seat;
}
public String toString()
{
return ("Manufacturer:"+manufacturer+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n"+"Seat type:"+seat+"\n");
}
}
package testvehicle;
public abstract class Vehicle//This class doesn't do something!
{
protected String manufacturer;
protected String model;
protected int maxSpeed;
protected double price;
public Vehicle(String manufacturer,String model,int maxSpeed,double price)
{
this.manufacturer=manufacturer;
this.model=model;
this.maxSpeed=maxSpeed;
this.price=price;
}
public Vehicle()
{
}
public String getManufacturer()
{
return manufacturer;
}
public void setManufacturer(String manufacturer)
{
this.manufacturer = manufacturer;
}
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public int getMaxSpeed()
{
return maxSpeed;
}
public void setMaxSpeed(int maxSpeed)
{
this.maxSpeed = maxSpeed;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public String toString()
{
return ("Manufacturer:"+manufacturer+"\n"+
"Model:"+model+"\n"+"Maximum Speed:"+maxSpeed+"\n"+"Price in euros:"+price+
"\n");
}
}
package testvehicle;
public class Main
{
public static void main(String[] args)
{
Car C=new Car("Opel","Corsa",220,12000.0,4,5);
MotorCycle M=new MotorCycle("KTM","DUKE-690",250,9000.0,"Agressive");
System.out.println(C.toString());
System.out.println();
System.out.println(M.toString());
}
}
回答by Maurice Perry
Say you have a class A
with a private int variable a
and a getter getA()
and a setter setA(int)
:
假设您有一个A
带有私有 int 变量a
以及一个 gettergetA()
和一个 setter 的类setA(int)
:
public class A {
private int a;
public int getA() {
return a;
}
public void setA(int value) {
a = value;
}
}
Now if you have a class B
that extends class A
, you can ensure that the getter and the setter cannot be overridden by a subclass of B
:
现在,如果您有一个B
扩展 class 的类A
,您可以确保 getter 和 setter 不能被以下的子类覆盖B
:
public class B extends A {
@Override
public final int getA() {
return super.getA();
}
@Override
public final void setA(int value) {
super.setA(value);
}
}
回答by SeattleDerrick
Sub-classes inherit both methods and public variables. They don't inherit private variables.
子类继承方法和公共变量。它们不继承私有变量。
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html