Setter / getter方法继承

时间:2014-08-03 21:09:44

标签: java

所以我正在研究java中的继承,我并没有明白这一点。假设我们有一个带有私有变量和公共setter getter方法的父方法。子类继承这些方法,但不继承私有变量。这些方法是否始终与也使用子类运行的超类相关联?我不能在子类中声明一个具有相同名称的变量,因此继承的方法会访问它。我知道不行。 setter / getter方法是否总是只影响声明它们的对象中的值,甚至通过继承从子类调用?

4 个答案:

答案 0 :(得分:0)

也许一个例子可以帮助你理解

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());
    }

}

public class C2 extends C1{
}

public class C3 extends C2{
    protected int x = 3;
}

public class C4 extends C3{

    protected int x = 4;

    @Override
    public int getX() {
        return x;
    }

}

你会得到

C1.x = 1
C2.x = 1
C3.x = 1
C4.x = 4

让我们看看发生了什么。

  • 由于显而易见的原因,C1.x为1
  • C2.x是1,因为它与C1
  • 相同
  • C3.x是1,因为getX()只能看到在C1中声明的“x”(某些人不直观)
  • C4.x是4,因为在C4中重写了getX(),所以它可以看到在C4上声明的“x”

答案 1 :(得分:0)

也许这个例子也会有所帮助:

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());

        }


    }

答案 2 :(得分:0)

假设您有一个类A,带有一个私有int变量a和一个getter getA()和一个setter setA(int)

public class A {
    private int a;
    public int getA() {
        return a;
    }
    public void setA(int value) {
        a = value;
    }
}

现在,如果您拥有扩展类B的类A,则可以确保B的子类不能覆盖getter和setter:

public class B extends A {
    @Override
    public final int getA() {
        return super.getA();
    }
    @Override
    public final void setA(int value) {
        super.setA(value);
    }
}

答案 3 :(得分:-1)

子类继承方法和公共变量。他们不会继承私有变量。

http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html