java 如何调用另一个包中的类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40885553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke a class in another package?
提问by Leonardo Esperan?a
I'm on a starter project and I split it in two packages in Eclipse and one of them has the main method and another .java class and the other one has anothers .java class with secondary methods. I want to know how do I invoke a method present in the secondary package to use it in the .java class that doesn't have the main method in the primary. How do I import it and how do I access a method from that class? I'm new to java and need this to a project.
我在一个启动项目上,我在 Eclipse 中将它分成两个包,其中一个有 main 方法和另一个 .java 类,另一个有另一个 .java 类和辅助方法。我想知道如何调用辅助包中存在的方法以在主包中没有 main 方法的 .java 类中使用它。如何导入它以及如何访问该类中的方法?我是 Java 新手,需要在项目中使用它。
回答by flavio
If I understood correctly (maybe it is better if you paste some sample code) write your code in the main method, calling what you need. Then write the imports that you need on the top or with CTRL+MAIUSC+O in Eclipse you will import the needed packages.
如果我理解正确(也许粘贴一些示例代码会更好)在 main 方法中编写代码,调用你需要的。然后在顶部写下您需要的导入,或者在 Eclipse 中使用 CTRL+MAIUSC+O 您将导入需要的包。
回答by Tom
You must use the import
in the beginning of the class, but Eclipse makes it automatically for you if you use its shortcut to auto-import (should be Ctrl + Shift + O
).
您必须import
在类的开头使用,但是如果您使用其自动导入的快捷方式(应该是Ctrl + Shift + O
),Eclipse 会自动为您制作。
Something like this:
像这样的东西:
Class User
班级 User
package model;
public class User {
private String name;
private Integer age;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public String getAge() { return this.name; }
public void setAge(Integer age) { this.age = age; }
@Override
public void toString() {
return "Name: " + this.name + "; Age: " + this.age;
}
}
Class UserAttributes
(where you need to import the other class)
类UserAttributes
(您需要导入其他类的地方)
package logic;
import model.User; //this is the line you must include (or hit Ctrl+Shift+O in Eclipse)
public class UserAttributes {
public static void main(String[] args) {
User john = new User("john", 30);
System.out.println(john);
}
}
Eclipse can be a nice tool to develop and makes your life much easier if you use its features. Some of them have direct access via shortcuts. Here's a list of some very useful(in my opinion).
Eclipse 可以成为一个很好的开发工具,如果您使用它的功能,它会使您的生活变得更加轻松。其中一些可以通过快捷方式直接访问。这是一些非常有用的列表(在我看来)。
Hope it's somehow helpful.
希望它在某种程度上有所帮助。
回答by Leonardo Esperan?a
yeah, here's my code
是的,这是我的代码
package ex_19;
import ex_16_17.Date;
import ex_18.HeartRates;
public class HealthProfile {
private String nome;
private String sobrenome;
private String sexo;
private Date dataNascimento;
private float altura;
private float peso;
.......
HeartRates hr = new HeartRates(this.getNome(), this.getSobrenome(), this.dataNascimento.getDia(), this.dataNascimento.getMes(), this.dataNascimento.getAno());
public int calcIdade(){
return hr.calculaIdade();
}
public int freqMaxima(){
return hr.feqMax();
}
public String freqAlvo(){
return hr.feqAlvo();
}
I wanna know how to use a code like this, working on my main, when I call the calcIdade(), what is the syntaxe?
我想知道如何使用这样的代码,在我的 main 上工作,当我调用 calcIdade() 时,语法是什么?