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

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

问题描述

我来自c#,这很容易,可能。



我有这个代码:

  public abstract class clsAbstractTable {

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

}

但Eclipse告诉我使用非法修饰符。



我有这个类:

  public class clsContactGroups extends clsAbstractTable { 


}

我想要定义的变量和方法这样,Eclipse 提示我,我没有实现抽象变量和方法。



要定义我的抽象类,以便提示您实现抽象?



编辑1



我将为不同的数据库表创建不同的类。每个类都应该有自己的TABLENAME变量,没有异常。



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

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



这样的东西也应该工作。

  String tablename =(clsAbstract)objItem.TABLENAME; 
//其中objItem可以是任何扩展clsAbstract的类;

EDIT 2



我想要一个常量(静态)定义在每个类的名称定义在抽象。




  • 我以抽象的方式定义变量TABLENAME,但未指定值。

  • 我创建了一个clsContactGroups,我应该提示实现TABLENAME,这是在哪里获取一些数据。例如:TABLENAME =contactgroups;

  • 我创建了第二个类clsContacts,我应该提示实现TABLENAME,这是获取一些数据的地方。例如:TABLENAME =contacts;

    etc ...


解决方案

我想你的困惑是C#属性与字段/变量。在C#中,您不能定义抽象字段,即使在抽象类中。然而,您可以定义抽象属性,因为这些是有效的方法(例如编译为 get_TAG() set_TAG(...))。



有些人已经提醒过,你不应该在你的类中有公共字段/变量,即使在C#。几个答案暗示了我会推荐什么,但没有说明。您应该使用getTAG()将您的想法转换为Java作为JavaBean属性。然后你的子类将必须实现这个(我也写了一个项目与表类,这样做)。



所以你可以有一个抽象类定义...

  public abstract Class AbstractTable {

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

...
}

子类,你需要定义一个静态的最终变量(常量),并返回从 getTag(),类似这样:

  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方法作为属性。


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

I have this code:

public abstract class clsAbstractTable {

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

}

but Eclipse tells me I use illegal modifier.

I have this class:

public class clsContactGroups extends clsAbstractTable {


}

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?

EDIT 1

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.

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

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

something like this should also work out

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

EDIT 2

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

  • I define variable TABLENAME in abstract, but no value given.
  • I create a clsContactGroups, I should be prompted to implement TABLENAME, this is where gets some data. eg: TABLENAME="contactgroups";
  • I create a second class clsContacts, I should be prompted to implement TABLENAME, this is where gets some data. eg: TABLENAME="contacts";
    etc...

解决方案

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(...)).

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

    ...
}

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

}

EDIT:

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天全站免登陆