Eclipse IDE java 相当于 PHP 包含吗?
我一直在四处挖掘,但找不到任何有用的东西。
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与 Java 中 PHP 的
include
最接近的是import
。阅读有关使用导入
和包
。
The closest thing to PHP's
include
in Java isimport
. Read more about usingimport
andpackage
in Java.如果您将项目创建为包,则使用该包的任何类都可以访问任何其他类。因此,如果您有
SomeFileA.java
并且它使用包com.something.me
,并且SomeFileB.java
使用包com. some.me
然后他们就可以互相引用。通常,如果您只是在 SomeFileA 中直接写入SomeFileB b = new SomeFileB();
,Eclipse 会自动添加任何导入。如果没有你可以使用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 packagecom.something.me
, andSomeFileB.java
that uses packagecom.something.me
then they can reference each other. Typically Eclipse will auto-add any imports if you just flat out writeSomeFileB b = new SomeFileB();
inside SomeFileA. If not you can just do use您可以使用
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 assubs.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
那么“进口”呢?您可以导入一个简单的类,而不仅仅是一个库...
将常用例程作为静态方法放入类中(例如“MyAppHelper”),并在“屏幕”中调用它们:
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" :