Java中的抽象变量? [英] Abstract variables in Java?

查看:45
本文介绍了Java中的抽象变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自 c#,这很容易,而且可能.

I am coming from c# where this was easy, and possible.

我有这个代码:

public abstract class clsAbstractTable {

    public abstract String TAG;
    public abstract void init();

}

但是 Eclipse 告诉我我使用了非法修饰符.

but Eclipse tells me I use illegal modifier.

我有这门课:

public class clsContactGroups extends clsAbstractTable {


}

我希望以这种方式定义变量和方法,以便 Eclipse 提示我,我有未实现的抽象变量和方法.

I want the variable and method defined in such way, that Eclipse to prompt me, I have unimplemented abstract variables and methods.

我需要如何定义抽象类,以便提示我实现抽象?

How do I need to define my abstract class so I should be prompted to implement the abstracts?

编辑 1

我将为不同的数据库表创建不同的类.每个类都应该有它自己的 TABLENAME 变量,也不例外.每次创建扩展抽象类的新类时,我都必须确保此变量是静态的.

I will create different classes for different db tables. Each class should have it's own TABLENAME variable, no exception. I have to make sure this variable is static each time when I create a new class that extends the abstract class.

然后在抽象类中我将有一个方法,例如:init();

Then in the abstract class I will have a method eg: init();

如果在这个 init() 方法中我调用 TABLENAME,它应该从子类中获取值.

If in this init() method I call TABLENAME, it should take the value from the sub-class.

这样的事情也应该解决

String tablename=(clsAbstract)objItem.TABLENAME;
// where objItem can be any class that extended clsAbstract;

编辑 2

我想要在每个类中定义一个常量(静态),并在抽象中定义它的名称.

I want a constant(static) defined in each class having it's name defined in abstract.

  • 我抽象地定义了变量 TABLENAME,但没有给出值.
  • 我创建了一个 clsContactGroups,应该会提示我执行 TABLENAME,这是获取一些数据的地方.例如:TABLENAME="contactgroups";
  • 我创建了第二个类 clsContacts,应该会提示我执行 TABLENAME,这是获取一些数据的地方.例如:TABLENAME="联系人";
    等等...

推荐答案

我认为您的困惑在于 C# 属性与字段/变量.在 C# 中,您不能定义抽象字段,即使在抽象类中也是如此.但是,您可以定义抽象属性,因为它们是有效的方法(例如编译为 get_TAG()set_TAG(...)).

I think your confusion is with C# properties vs. fields/variables. In C# you cannot define abstract fields, even in an abstract class. You can, however, define abstract properties as these are effectively methods (e.g. compiled to get_TAG() and set_TAG(...)).

正如一些人所提醒的那样,即使在 C# 中,也不应该在类中使用公共字段/变量.有几个答案暗示了我的建议,但没有说清楚.您应该使用 getTAG() 将您的想法作为 JavaBean 属性转换为 Java.然后你的子类将不得不实现这个(我也写了一个带有执行此操作的表类的项目).

As some have reminded, you should never have public fields/variables in your classes, even in C#. Several answers have hinted at what I would recommend, but have not made it clear. You should translate your idea into Java as a JavaBean property, using getTAG(). Then your sub-classes will have to implement this (I also have written a project with table classes that do this).

所以你可以像这样定义一个抽象类......

So you can have an abstract class defined like this...

public abstract class AbstractTable {

    public abstract String getTag();
    public abstract void init();

    ...
}

然后,在任何具体的子类中,您都需要定义一个静态最终变量(常量)并从 getTag() 返回它,如下所示:

Then, in any concrete subclasses you would need to define a static final variable (constant) and return that from the getTag(), something like this:

public class SalesTable extends AbstractTable {

    private static final String TABLE_NAME = "Sales";

    public String getTag() {
        return TABLE_NAME;
    }

    public void init() {
        ...
        String tableName = getTag();
        ...
    }

}

编辑:

您不能覆盖继承的字段(在 C# 或 Java 中).您也不能覆盖静态成员,无论它们是字段还是方法.所以这也是最好的解决方案.我更改了上面的 init 方法示例以展示如何使用它 - 再次将 getXXX 方法视为一个属性.

You cannot override inherited fields (in either C# or Java). Nor can you override static members, whether they are fields or methods. So this also is the best solution for that. I changed my init method example above to show how this would be used - again, think of the getXXX method as a property.

这篇关于Java中的抽象变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆