简体   繁体   English

从一个类调用一个方法到另一个类

[英]Calling a method from a one class to another

I wrote this method in one class for checking if a table exists in my database or not:我在一个类中编写了这个方法来检查我的数据库中是否存在一个表:

public boolean DoesTableExist(String tableName) {
    SQLiteDatabase mDatabase = openOrCreateDatabase("privdb",MODE_PRIVATE,null);

    Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
    if(cursor!=null) {
        if(cursor.getCount()>0) {
            cursor.close();
            return true;

        }
        cursor.close();
    }

    return false;
}

I now would like to call this method in other activites or classes, without writing the same code everytime in every file.我现在想在其他活动或类中调用此方法,而无需每次在每个文件中都编写相同的代码。 How can I do that?我怎样才能做到这一点?

Normally you'd invoke methods in another class by instantiating the class and then calling the method.通常,您会通过实例化该类然后调用该方法来调用另一个类中的方法。 For instance, if the class that the method was in was named DatabaseUtils , you'd do the following:例如,如果该方法所在的类名为DatabaseUtils ,您将执行以下操作:

DatabaseUtils dbUtils = new DatabaseUtils();
boolean exists = dbUtils.doesTableExist("tablename"); 
                      // ^ note that the normal Java convention is to make
                      //   method names start with a lower case letter

However, this has the feel of a "utility" method (the object presumably doesn't store any state, and thus there's no real need to instantiate it), and a common way of handling those are to make the method static, like so:然而,这有一种“实用”方法的感觉(对象大概不存储任何状态,因此没有真正需要实例化它),处理这些的常用方法是使方法静态,就像这样:

public static boolean doesTableExist(String tableName) {

That causes the method to exist on the class definition itself, instead of on a specific instance of a class, and allows you to do the following:这会导致该方法存在于类定义本身而不是类的特定实例上,并允许您执行以下操作:

boolean exists = DatabaseUtils.doesTableExist("tablename");

Make a instance of the class that method is in, create a get method and call method through that instance.创建该方法所在类的实例,创建一个 get 方法并通过该实例调用方法。 Other option is to define method as a static and call it that way.另一种选择是将方法定义为静态并以这种方式调用它。

Instance:实例:

ClassName classInstance = new ClassName();
classInstance.getMethod(parametar);

Static:静止的:

ClassName.methodName(parametar);

Here is more about that: How to call a method in another class in Java?这里有更多关于: 如何在 Java 中调用另一个类中的方法?

That would be a nice one to put in a "utils" package.这将是一个很好的放在“utils”包中的方法。 Create a new Java package named "utils", add a new class, such as "DatabaseUtils.java" and make it look something like this:创建一个名为“utils”的新 Java 包,添加一个新类,例如“DatabaseUtils.java”并使其看起来像这样:

    public class DatabaseUtils {

    public boolean DoesTableExist(String tableName) {
                SQLiteDatabase mDatabase = openOrCreateDatabase("privdb",MODE_PRIVATE,null);

                Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master                 where tbl_name = '"+tableName+"'", null);
                if(cursor!=null) {
                    if(cursor.getCount()>0) {
                        cursor.close();
                        return true;
                    }
                    cursor.close();
                }
                return false;
            }

    }

Now you can reference it from other places like this:现在你可以像这样从其他地方引用它:

    public class main {

            public static void main(String [] args)
                    boolean isTableExisting = DatabaseUtils.doesTableExist("yourTableName")
                    System.out.println("Table existing? " + isTableExisting);
            }
    }

Probably you'll need to pass the name of the database as well ("privdb") or reference to where the method "openOrCreateDatabase(...)" is located.可能您还需要传递数据库的名称(“privdb”)或对方法“openOrCreateDatabase(...)”所在位置的引用。

When your database does exist, it will print "Table existing? true"当您的数据库确实存在时,它将打印"Table existing? true"

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

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