【问题标题】:Setter / getter method inheritanceSetter/getter 方法继承
【发布时间】:2014-08-03 21:09:44
【问题描述】:

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

【问题讨论】:

  • 你不能继承方法,但不能继承变量。您从父类继承所有内容。私有只是意味着你不能直接访问它,但它仍然存在。
  • 私有变量并没有消失,只是子类实例无法访问它。通过超类 getter 访问它仍然有效。
  • 请继续阅读并尝试继承。您可以通过阅读一些指南并打开 Eclipse 自己尝试来回答您自己的问题。
  • 考虑子类化,比如将代码附加到一个类,也许这会帮助你理解这个想法

标签: java


【解决方案1】:

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

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,因为 getX() 在 C4 被覆盖,所以它可以看到在 C4 声明的“x”

【讨论】:

    【解决方案2】:

    假设你有一个类 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;
        }
    }
    

    现在如果你有一个扩展类A的类B,你可以确保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);
        }
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 这根本不准确。方法和变量(字段)都有可见性限制。子类始终可以访问(查看)受保护的和公共的,并且可以(如果在同一个包中)访问默认范围。
        • 错了。子类继承一切。包括带有 private 修饰符的所有内容,例如私有方法和变量。私有方法和变量在子类中根本不可访问,但这意味着它们不存在。
        • @1337 迂腐不正确。子类不继承构造函数。
        • @emory 好吧,当然,但我指的是方法,变量,.. :)
        猜你喜欢
        • 2011-03-11
        • 2019-05-04
        • 2014-06-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 2017-09-09
        • 1970-01-01
        • 2013-11-11
        相关资源
        最近更新 更多