Java |父类和子类中的静态变量|从父类

时间:2017-01-06 12:20:17

标签: java variables inheritance static overwrite

我有几个具有独特静态变量的子类,在这种情况下称为' x'。 所有这些子类都以相同的方式使用静态var,因此我希望减少代码重复并将功能放在超类中。 在这种情况下,方法' getX'在超级班。从这里我想要返回x的值。现在我面临的问题是它使用超类的x值而不是子类的值。如何从超类中访问子类的x值?

public class Playground {

  public static void main(String[] args) {
    Parent parent = new Parent();
    Child child = new Child();
    Child1 child1 = new Child1();

    System.out.println("Parent.x " + parent.x);
    System.out.println("child.x " + child.x);
    System.out.println("child.x " + child1.x);

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

class Parent {
  static String x = "static of parent";
  String y = "instance of parent";

  String getX() {
      return x;
  }
}

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
}

class Child1 extends Parent {
  static String x = "static of child1";
  String y = "instance of child";
}

此代码打印出来: Parent.x static of parent child.x static of child child.x static of child1 get x: static of parent get x: static of parent < - 这里应该是儿童的静态

希望有人可以帮助我。

干杯

5 个答案:

答案 0 :(得分:2)

尝试将getX方法添加到子级。像这样:

class Child extends Parent {
  static String x = "static of child";
  String y = "instance of child";
  String getX() {
      return x;
  }
}

答案 1 :(得分:2)

解决方案可能是在Parent中没有静态,而是实例字段。

class Parent{
    private final X x
    public Parent(X x){
        this.x = x;
    }

    public X getX() {
        return x;
    }
}

然后在您的孩子中,您仍然可以拥有静态,但是您可以在构造函数

中传递它
class Child extends Parent {
    static final X x = ....
    public Child() {
        super(x);
    }
}

答案 2 :(得分:1)

在子类中添加getX方法。 父getX方法指向父类的静态变量。

答案 3 :(得分:1)

您尝试的是违反基本OOP原则信息隐藏 / 封装

没有其他类应该知道类如何存储或处理其属性(又名变量字段)。这包括clases不应该有 getter setter (正如@RosárioPereiraFernandes所建议的那样)

当该类表示数据传输对象(DTO)时,此规则有一个例外,它具有(几乎)没有业务逻辑并且只提供对结构化数据的访问。 DTO应该有 getters 和(不常见) setters

其他类应提供与业务相关名称的方法来修改其内部状态(其成员变量的值):

WITH [A] AS 
(
    SELECT (startDate + startTime) AS time1
    FROM [Date] 
 )
SELECT 
    CONVERT(datetime, A.time1, 20)
FROM 
    [A]  

答案 4 :(得分:1)

据我所知,无法从父类访问子变量。

但您可以覆盖子类

中的String getX()
@Override
protected String getX() {        
    return x;
}

然后Java调用“最低”getX()方法

我会将您的示例编写为不同的代码:

public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child1 child1 = new Child1("static of child1");

  public class Playground {

private static Parent parent = new Parent("static of parent");
private static Child child = new Child("static of child");
private static Child child1 = new Child("static of child1");

  public static void main(String[] args) {


    System.out.println("Parent.x " + parent.getX());
    System.out.println("child.x " + child.getX());
    System.out.println("child.x " + child1.getX());

    System.out.println("get x: " + parent.getX());
    System.out.println("get x: " + child.getX());
  }
}

 class Parent {
     private String x;
     public Parent(String x) {
         this.x = x;

     }

     public String getX() {
          return this.x;
      }
 }

class Child extends Parent {
    protected Child(String x) {
        super(x);
    }  
}

- >这将带来以下输出:

Parent.x static of parent
child.x static of child
child.x static of child1
get x: static of parent
get x: static of child

问你是否有问题

相关问题