我在工作文件的同一文件夹下有StdDraw.java
,而picture()
是StdDraw.java
内的方法。
但是,我没有添加此行来导入方法,建议按
包StdDraw不存在
import StdDraw.picture
我怎么可能这样做?用包?设置路径?或任何修改?我来自python并发现它有点奇怪。
答案 0 :(得分:4)
你不能导入非静态方法(只有类和静态成员),而你不需要!
如果你的两个类都存在于默认包中,那么你应该可以在没有任何import语句的情况下执行以下操作:
myStdDrawObject.picture(); // if picture is non-static
或
StdDraw.picture(); // if picture is static
另请注意,您不能对存在于默认包中的类使用静态导入。
答案 1 :(得分:1)
如果您要导入同一个包中的类,那么我们不需要使用任何导入。
如果你想将类mtods导入到类中,请使用如下所示。您无需在导入时输入方法名称。
import packagename.StdDraw;
导入类后,该类的所有非静态方法都可用于导入的类中。
什么时候应该使用静态导入?只有在你没有的时候才使用它 试图声明常量的本地副本,或滥用继承 (Constant Interface Antipattern)。换句话说,当你使用它时 需要经常访问一个或两个类的静态成员。如果 你过度使用静态导入功能,它可以使你的程序 不可读和不可维护,用所有的方式污染它的命名空间 您导入的静态成员。您的代码的读者(包括您,一些 你写完之后几个月)将不知道哪个类是静态成员 来自。从类中导入所有静态成员即可 对可读性特别有害;如果你只需要一两个 成员,单独导入它们。适当使用,静态导入 通过删除的样板,可以使您的程序更具可读性 重复上课
名。
阅读有关静态导入的更多信息:
https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html
答案 2 :(得分:1)
我建议阅读包以及如何在java中组织代码。它在某种程度上类似于python,其中使用了一个目录结构,但在java中更多。也许这会有所帮助
答案 3 :(得分:1)
即使您没有为同一文件夹中存在的类包含导入,也可以创建该类的对象和调用方法以及静态方法。
您可以创建对象并调用非静态方法。
// Uploaded.net API - Java
// Copyright (C) 2012 Julius Fischer webmaster@it-gecko.de
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package to.uploaded.file;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import to.uploaded.Uploaded;
import to.uploaded.exception.DownloadFailedException;
public class UPDownload extends UPDownloadEventListener{
private Uploaded uploaded;
private String path = "";
private String fileId = "";
private File endFile = null;
private long progressByte = 0;
private long contentLength = -1;
public UPDownload(Uploaded uploaded, String path, String fileId)
{
this.uploaded = uploaded;
this.fileId = fileId;
if(!path.matches("/$"))
path = path + "/";
this.path = path;
}
/**
* Gibt File Objekt der heruntergeladenen Datei zurück
* @return
*/
public File getFile()
{
return endFile;
}
private String getDownloadLink(String fileId) throws DownloadFailedException
{
try {
DefaultHttpClient httpclient = uploaded.getUpHttp().getUpClient();
HttpClientParams.setRedirecting(httpclient.getParams(), false);
HttpResponse response = httpclient.execute(new HttpGet("http://uploaded.net/file/" + fileId +"/ddl"));
Header location = response.getLastHeader("Location");
String content = null;
if(location != null)
{
content = location.getValue();
if(content != null)
{
Matcher mat = Pattern.compile("http://[0-9a-z\\-]*stor\\d+.uploaded.net/dl/([0-9a-z-]+)").matcher(content);
if(mat.find())
{
httpclient.getConnectionManager().shutdown();
return mat.group(0);
}
}
}
content = EntityUtils.toString(response.getEntity());
if(content.matches("<title>(.*?)Wartungsarbeiten</title>")) {
throw new DownloadFailedException("Wartungsarbeiten");
}
if(content != null)
{
Matcher mat = Pattern.compile("http://[0-9a-z\\-]*stor\\d+.uploaded.net/dl/([0-9a-z-]+)").matcher(content);
if(mat.find())
{
httpclient.getConnectionManager().shutdown();
return mat.group(0);
}
}
httpclient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Startet den Downloadvorgang
* @throws DownloadFailedException
*/
public void start() throws DownloadFailedException
{
if(!isFileAvailable())
throw new DownloadFailedException("Datei nicht mehr auf Uploaded.net vorhanden");
endFile = null;
DefaultHttpClient httpclient = uploaded.getUpHttp().getUpClient();
try {
HttpResponse response;
String url = getDownloadLink(fileId);
if(url == null)
throw new DownloadFailedException("Downloadserver konnte nicht gefunden werden");
response = httpclient.execute(new HttpGet(url));
HttpEntity entity = response.getEntity();
String filename = response.getHeaders("Content-Disposition")[0].getValue();
Matcher mat = Pattern.compile("attachment; filename=\"([^\\\\/\\:\\*\\?\\\"\\<\\>\\|]+)\"").matcher(filename);
if(!mat.find())
throw new DownloadFailedException("Dateiname nicht gefunden");
filename = mat.group(1);
File file = new File(path + filename);
if(file.exists())
throw new DownloadFailedException("Dateiname existiert bereits");
file.createNewFile();
OutputStream out = new BufferedOutputStream(new FileOutputStream(file.getAbsoluteFile()));
InputStream in = entity.getContent();
contentLength = entity.getContentLength();
byte[] b = new byte[1024];
int numRead;
while ((numRead = in.read(b)) != -1) {
out.write(b, 0, numRead);
updateProgress(numRead);
}
in.close();
out.close();
httpclient.getConnectionManager().shutdown();
endFile = file;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Überprüft eine Datei auf Verfügbarkeit
*
* @return true = OK | false = 404
*/
public boolean isFileAvailable()
{
try {
DefaultHttpClient httpclient = uploaded.getUpHttp().getUpClient();
HttpResponse response = httpclient.execute(new HttpGet("http://uploaded.net/file/" + this.fileId +"/ddl"));
httpclient.getConnectionManager().shutdown();
if(response.getStatusLine().getStatusCode() == 404)
return false;
return true;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean isFinish()
{
return contentLength == progressByte;
}
/**
* @return the progressByte
*/
public long getProgressByte() {
return progressByte;
}
/**
* @return the contentLength
*/
public long getContentLength() {
return contentLength;
}
private void updateProgress(long i)
{
progressByte += i;
notifyListeners(this);
}
public static interface UPDownloadProgress
{
void progress(UPDownload upDownload);
}
}
对于静态方法,您只能使用类名称来调用它。
StdDraw drawObj = new StdDraw();
drawObj.picture(); // if picture is non-static method
答案 4 :(得分:1)
导入包时,您不需要导入非静态方法。您可以在此处阅读Java - Packages。它很容易解释,我发现它在学习相同的概念时很有用。