Eclipse IDE java 相当于 PHP 包含吗?

发布于 2024-12-10 17:51:04 字数 2578 浏览 0 评论 0原文

我一直在四处挖掘,但找不到任何有用的东西。

我正在开发一个 Android 应用程序。

基本上我有一个包,到目前为止它里面有三个java文件;我的主屏幕页面、设置页面以及我所谓的“subs.java”,我在其中放置有用的函数、例程。

我想做的是创建这个“subs.java”文件,其中可以存储在多个地方使用的例程。

所以我有我的主应用程序页面和设置页面。这两个“页面”都需要使用这些通用功能。

所以我打算把它们放在我的“subs.java”中,这样​​我就不会增加代码。

我现在遇到的问题是我有这个 subs.java 文件,如何链接到它?

在 PHP 中,如果我想使用另一个文件,我只需包含它,然后我就可以访问它的所有功能。

我想我正在尝试建立一个库,但 Java 对我来说是新的。

那么我该如何在 Eclipse/Java 中执行此操作呢?

这是我的 subs 文件,其中包含我在其他地方找到的一些有用的函数:

package com.example.helloandroid;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class Subs extends Activity {

    // Read settings
    public String ReadSettings(Context context){
        FileInputStream fIn = null;
        InputStreamReader isr = null;

        char[] inputBuffer = new char[255];
        String data = null;

        try {
            fIn = openFileInput("settings.dat");      

            isr = new InputStreamReader(fIn);
            isr.read(inputBuffer);
            data = new String(inputBuffer);

            Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {      
            e.printStackTrace();
            Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
        }
        finally {
            try {
                isr.close();
                fIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

 // Save settings
    public void WriteSettings(Context context, String data){
        FileOutputStream fOut = null;
        OutputStreamWriter osw = null;

        try {
              fOut = openFileOutput("settings.dat",MODE_PRIVATE);      
              osw = new OutputStreamWriter(fOut);
              osw.write(data);
              osw.flush();
              Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {      
            e.printStackTrace();
            Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
        }
        finally {
            try {
                osw.close();
                fOut.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}

I've been digging around but can't find anything useful.

I'm working on an Android App.

Basically I have a package and there are three java files in it so far; my main screen page, a settings page and what I have called my 'subs.java' where I am putting useful functions, routines.

What I am trying to do is create this 'subs.java' file where routines that get used in more than one place can be stored.

So I have my main app page and I have a settings page. Both of these 'pages' need to use these common functions.

So I was going to put them in my 'subs.java' so I don't end up doubling up code.

Where I am stuck is now I have this subs.java file how do I link to it ?

In PHP if I want to use another file I just include it and I have access to all it's functions.

I suppose I am trying to build up a library, but Java is new to me.

How then would I do this in Eclipse/Java please ?

Here's my subs file, with some useful functions that I found else where :

package com.example.helloandroid;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class Subs extends Activity {

    // Read settings
    public String ReadSettings(Context context){
        FileInputStream fIn = null;
        InputStreamReader isr = null;

        char[] inputBuffer = new char[255];
        String data = null;

        try {
            fIn = openFileInput("settings.dat");      

            isr = new InputStreamReader(fIn);
            isr.read(inputBuffer);
            data = new String(inputBuffer);

            Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {      
            e.printStackTrace();
            Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
        }
        finally {
            try {
                isr.close();
                fIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

 // Save settings
    public void WriteSettings(Context context, String data){
        FileOutputStream fOut = null;
        OutputStreamWriter osw = null;

        try {
              fOut = openFileOutput("settings.dat",MODE_PRIVATE);      
              osw = new OutputStreamWriter(fOut);
              osw.write(data);
              osw.flush();
              Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
        }
        catch (Exception e) {      
            e.printStackTrace();
            Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
        }
        finally {
            try {
                osw.close();
                fOut.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }




}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-12-17 17:51:04

与 Java 中 PHP 的 include 最接近的是 import。阅读有关使用导入

The closest thing to PHP's include in Java is import. Read more about using import and package in Java.

殤城〤 2024-12-17 17:51:04

如果您将项目创建为包,则使用该包的任何类都可以访问任何其他类。因此,如果您有 SomeFileA.java 并且它使用包 com.something.me,并且 SomeFileB.java 使用包 com. some.me 然后他们就可以互相引用。通常,如果您只是在 SomeFileA 中直接写入 SomeFileB b = new SomeFileB(); ,Eclipse 会自动添加任何导入。如果没有你可以使用

import com.something.me.SomeFileB;

If you've created a project as a package then any class using that package can access any other ones. So if you had SomeFileA.java and it uses package com.something.me, and SomeFileB.java that uses package com.something.me then they can reference each other. Typically Eclipse will auto-add any imports if you just flat out write SomeFileB b = new SomeFileB(); inside SomeFileA. If not you can just do use

import com.something.me.SomeFileB;
属性 2024-12-17 17:51:04

您可以使用 import 语句导入您的子类。然后,您可以将 subs 类中的任何函数引用为 subs.functionName()

如果 subs.java 与其他 java 文件位于同一文件夹中,您只需键入 import subs代码>.

有关包和导入的更多信息,请访问 http:// /www.article.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html

You would use the import statement to import your subs class. Then you can refer to any function in the subs class as subs.functionName()

If subs.java is in the same folder as your other java files, you can simply type import subs.

Read more about packages and imports at http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html

故人的歌 2024-12-17 17:51:04

那么“进口”呢?您可以导入一个简单的类,而不仅仅是一个库...

将常用例程作为静态方法放入类中(例如“MyAppHelper”),并在“屏幕”中调用它们:

MyAppHelper.function1(...)

What about "import" ? You can import a simple class, not only a library...

Put your common routines as static methods in a class ("MyAppHelper" for example), and call them in your "screens" :

MyAppHelper.function1(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文