在静态情况下不能使用'这个' [英] Cannot use 'this' in static context

查看:153
本文介绍了在静态情况下不能使用'这个'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了在C#编码一个公平的,但我完全新的爪哇。我想了解如何使与接口SQLite的Andr​​oid应用程序。

I have done a fair amount of coding in C# but I am completely new to java. I am trying to learn how to make an android application that interfaces with SQLite.

我的目的是让含有干净利索地执行数据库操作的所有函数的类 - 这也可以用code,例如单行调用; DbFunctions.AddContact(其中fname,LNAME);

My aim is to have a class containing all functions which perform database operations cleanly and neatly - which can also be called with a single line of code, eg; DbFunctions.AddContact("fName", "lName");

我有我的教程已经阅读DBAdapter类:

I have a DBAdapter class which I have read in a tutorial:

public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_NAME = "name";
static final String KEY_EMAIL = "email";
static final String TAG = "DBAdapter";

static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "contacts";
static final int DATABASE_VERSION = 1;

static final String DATABASE_CREATE =
        "create table contacts (_id integer primary key autoincrement, "
        + "name text not null, email text not null);";

final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try
        {
            db.execSQL(DATABASE_CREATE);
        }
        catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }
}

//---opens the database---
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---
public void close()
{
    DBHelper.close();       
}   

// some other database functions here... inserts, updates etc
}

和我已经创建了自己的类来处理所有来电的DBAdapter:

And I have created my own class to handle all calls to the DBAdapter:

    public static class DatabasesActivity extends Activity
{       
    static DBAdapter db;

    // Called when activity is first created
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public static long addContact(String name, String email)
    {
        if (db == null) {
            db = new DBAdapter(this); // <--- compiler error here
        }

        db.open();
        long id = db.insertContact("Joe Bloggs", "joe@bloggs.com");
        db.close();
        return id;
    }
}

的addContact 方法,就行了: DB =新DBAdapter(本); ,我得到的以下错误:不能使用'这'在静态情况下

In the addContact method, on the line: db = new DBAdapter(this);, I get the following error: Cannot use 'this' in a static context.

我所熟悉的OOP概念我是这么理解的为什么的我得到这个错误 - 但作为新的Java本身,我在寻找替代方法是什么,我想要的目的。在DBAdapter类的构造函数在背景参数,但我不确定为什么,因为我没有写这个类自己。

I am familiar with OOP concepts so I understand why I am getting this error - but being new to java itself, I am looking for alternate methods on what I'm trying to achieve. The DBAdapter class constructor takes in a context parameter, but I am unsure why as I have not written that class myself.

澄清:
我明白了为什么错误发生。在DBAdapter类的构造函数在背景参数,我不知道在作为上下文参数传递时,我用它静态。我希望类是静态的,因为我不希望有我想用它每次都实例化。

To Clarify: I understand why the error is occurring. The DBAdapter class constructor takes in a context parameter, and I don't know what to pass in as the context parameter when I'm using it statically. I want the class to be static as I don't want to have to instantiate it every time I want to use it.

我想我真正的问题是为什么SQLiteOpenHelper需要一个环境?

I guess my real question would be "why does SQLiteOpenHelper require a context?"

推荐答案

您已经定义了一个静态此方法:

You have defined a static method here :

  public static long addContact(String name, String email)

静态方法和类变量的绑到类而不是类的任何特定实例。你不能使用里面这个关键字作为它指的是当前实例。其中选择将申报方法实例方法删除静态在方法声明中的关键字,如果确实是该方法的逻辑取决于当前实例的状态。

Static methods and class variables are tied to the Class and not to any specific instance of the Class. You cannot use the this keyword inside it as it refers to the current instance. One of the choice will be to declare the method as instance method removing the static keyword from the method declaration, if indeed the method logic depends on the state of the current instance.

我相信使用问题这个静态方法将是在运行时,如果你的code调用静态方法的 ClassName.staticMethodName()的,运行时将不知道如何解决这个在这种情况下。

I believe the problem in using this inside a static method will be that during runtime if your code calls the static method as ClassName.staticMethodName() , the runtime will have no idea how to resolve this in this context.

这篇关于在静态情况下不能使用'这个'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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