使用 “import 包名.类名”的形式导入另一个主类,然后通过新建对象的方式使用主类的方法
package clothing;
public class Shirt {
public static String getColor() {
return "Green";
}
}
package clothing.pants;
import static clothing.Shirt.getColor;
//import clothing.*;
public class Jeans {
public void matchShirt(){
//String color = Shirt.getColor();
String color = getColor();
if(color.equals("Green")) {
System.out.print("Fit");
}
}
public static void main(String[] args) {
Jeans trouser = new Jeans();
trouser.matchShirt();
}
}