Java 在类扩展中使用静态值

Java 在类扩展中使用静态值,java,Java,我想根据调用的类使用一个常数的方法。 很抱歉,这是一个错误的解释,下面是一个示例: public class Mom{ public void execute(parameters){ // Some actions String nf = String.format(C_CARS) } } public class Son1 extends Mom{ private static final String C_CARS=" myFormat "

我想根据调用的类使用一个常数的方法。 很抱歉,这是一个错误的解释,下面是一个示例:

public class Mom{
   public void execute(parameters){
      // Some actions
       String nf = String.format(C_CARS)
   }
}

public class Son1 extends Mom{
   private static final String C_CARS=" myFormat " 
}


public class Son2 extends Mom{
   private static final String C_CARS=" myFormat2 " 
}

public static void main(String[] args){
   Son1 son1=new Son1();
   Son2 son2=new Son2();
   son1().execute(myparameters);
   son2().execute(myparameters);
}

我想做那样的事,有办法吗?这里的问题是C_CARS在Mom类中是未知的。

您不能用这种方式重写变量。请改用以下方法:

abstract public class Mom {
    abstract protected String cCars();

    public void execute(parameters){
       // Some actions
       String nf = String.format(cCars())
    }
}

public class Son1 extends Mom{
    private static final String C_CARS=" myFormat ";

    @Override
    protected String cCars() {
        return C_CARS;
    }
}

或者,您可以在
Mom
中提供一个默认实现,这样就不需要将类和方法抽象化。

不要使用
静态
,这会带来麻烦

public class Mom {
    private final String cCars;
    protected Mom(String cCars) {
        this.cCars = cCars;
    }
    public void execute(parameters){
        // Some actions
        String nf = String.format(cCars)
    }
}

public class Son1 extends Mom {
    public Son1() {
        super(" myFormat ");
    }
}


public class Son2 extends Mom {
    public Son2() {
        super(" myFormat2 ");
    }
}

这无法工作,因为超类无法访问其子类的成员。此外,不能像重写方法那样重写变量。请使用以下代码进行尝试:

public static abstract class Mom {
    public void execute() {
        System.out.println(getCCars());
    }

    abstract String getCCars();
}

public static class Son1 extends Mom {

    @Override
    String getCCars() {
        return " myFormat ";
    }
}

public static class Son2 extends Mom {

    @Override
    String getCCars() {
        return " myFormat2 ";
    }
}

public static void main(String[] args) {
    Son1 son1 = new Son1();
    Son2 son2 = new Son2();
    son1.execute();
    son2.execute();
}
这是用java实现它的方法


我已经测试了上面的代码。它确实编译并生成所需的结果。

标记为
static
的字段属于类而不是实例。此外,
Son1
中的
CU车
Son2
中的
CU车
没有任何关联

  • 实现这一目标的方法是:

    class Mom {
        public abstract String getCCars();
    }
    
    然后,
    Mom
    的每个上升点都必须覆盖
    getCCars()
    方法

  • 您还可以在
    Mom
    的构造函数中接受字符串
    cCars
    。然后,每个上升点必须调用
    Mom
    中定义的超级构造函数:

    class Mom {
    
        final String cCars;
    
        Mom(String cCars) {
            this.cCars = cCars;
        }
    
        void execute(String parameters) {
            System.out.println(this.cCars);
        }
    }
    
    class Son1 {
    
        Son1() {
            super("MyFormat"); // We have to call the super constructor
        }
    }
    

    • 这不是Java中继承的工作方式

      您需要保证
      Mom
      的所有实例都有一个值。通过实例方法执行此操作,例如

       public abstract class Mom{
         public void execute(parameters){
           String nf = String.format(getCCars());
         }
         protected abstract String getCCars();
       } 
      
      然后在子类中实现:

      class Son1 extends Mom {
        @Override protected String getCCars() {
          return "something";
        }
      }
      

      您的面向对象设计有些问题

      记住:
      Son1
      类的每个实例也是
      Mom
      的实例。但在现实世界中,大多数儿子不是母亲


      extends
      在这里不适合使用。

      Mom类是Son1和Son2类的超类。因此,子类变量在超类中是不可访问的。即使Java字段以多态方式工作,您如何期望它对
      类Son3扩展Mom{/*no constant*/}
      ?覆盖Son1和Son2类中的
      execute
      方法。即使在现实世界中,我们也不能对Mom保密,但在Java世界中,任何
      private
      到类的内容都是私有的;)@萨加罗汉卡我喜欢这个解释xD