简体   繁体   English

java-类的最终受保护静态类,扩展了另一个类

[英]java - final protected static class for class extending another class

I have a Record class. 我有一个记录课。 In this Record class i have a save() method which saves data to a database. 在这个Record类中,我有一个save()方法将数据保存到数据库中。 I have several other classes that extend this Record class, for example, a Person class. 我还有一些其他扩展此Record类的类,例如,Person类。 In my Person class i would like to define the database table name and columns for the People database table. 在我的Person类中,我想定义数据库表名称和People数据库表的列。 I would like to make these final static properties that all instances of Person reference. 我想使所有Person实例都引用这些最终的静态属性。 How do I let Record, the extended class, know this property will exist in all extended class? 如何让Record(扩展类)知道此属性将存在于所有扩展类中? How do i define a static property for a class that the base class knows exists? 如何为基类知道存在的类定义静态属性?

Would this be an appropriate use of an interface? 这是对接口的适当使用吗?

public class Record {
    protected String table_name;
    protected String[] table_columns;

    private void save(){
        //save data based on table_name and table_columns;
    }
}

public class Person extends Record{
    protected final static String table_name = "People";
    protected final static String[] table_columns = {"_id","name"}
}

Why the need for static final ? 为什么需要static final

If you make Record abstract and change the table name and table columns to be abstract methods, you will be required to implement them in subclasses (and Record will be ensured of their presence). 如果使Record抽象,并将表名和表列更改为抽象方法,则需要在子类中实现它们(并确保Record存在)。

I'm not sure why instances of Person would need to access this information, but it would still be possible, if necessary. 我不确定为什么Person实例需要访问此信息,但是如果有必要,它仍然可能。

public abstract class Record {
  protected abstract String getTableName();
  protected abstract String[] getTableColumns();

  private void save(){
    //save data based on getTableName() and getTableColumns();
  }
}

public class Person extends Record {
  protected String getTableName() {
    return "People";
  }
  protected String[] getTableColumns() {
    return new String[] { "_id", "name" };
  }
}

I think a good way to achieve your reqs is doing something like this: (I assume Record should be abstract) 我认为达​​到要求的一种好方法是执行以下操作:(我认为Record应该是抽象的)

public abstract class Record {
    protected abstract String getTableName();
    protected abstract String[] getTableColumns();

    private void save(){
        String table_name = getTableName();
        String[] table_columns = getTableColumns();
        //save data based on table_name and table_columns;
    }
}

public class Person extends Record{
    protected final static String table_name = "People";
    protected final static String[] table_columns = {"_id","name"}

    protected String getTableName(){
         return table_name;
    }

    protected String[] getTableColumns(){
        return table_columns;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM