java调用另一个包里面的类(方法)

java调用另一个包里面的类结构:
1.文件夹1(包1)[p1]
1.1过程(类)[a1]
1.1.1函数(方法)[funa()]


2.文件夹2(包2)[p2]
2.1过程(类)[a2]
2.1.1函数(方法)[main里]
说明:我们要在 2.1.1 里写一个函数(方法),然后需要调用 1.1.1 的函数(方法)算法;
例如:类1.1.1 中,funa(x+y); 在2.1.1 中运用funa(x+y);这样方法来运算。


方法:
1.(重要)需要在类 a2 中声明继承 a1 的类;

import com.example.demo1.p1.a1; //继承包p1中的a1类

2.然后在任意要调用的方法中(例如main方法中)运用,直接写(x是暂时存储,可以任意名字) x = new a1().funa(12,1);
3.需要 new a1类.funa方法名;

import com.example.demo1.p1.a1; //继承包p1中的a1类

public class a2 {

    public static void main(String[] args){
        int x;

        x = new a1().funa(12,1);

        System.out.println("output: " + x);

    }

OK 可以调用到另一个类的方法了。

725 Views
分享你的喜爱
linwute
linwute

我要像梦一样自由,像大地一样宽容;
在艰辛放逐的路上,点亮生命的光芒;
我要像梦一样自由,像天空一样坚强;
在曲折蜿蜒的路上,体验生命的意义;

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注