我是靠谱客的博主 贪玩荷花,这篇文章主要介绍Package与Import,现在分享给大家,希望可以做个参考。

– 定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;定义一个主类A,其所在的包也为bzu.info.com,在A中生成一个Cylinder对象,并输出其体积。编译并运行该类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package bzu.info.software; public class Cricle { double radius; double area; Cricle(double r){ radius=r; } double getArea(){ area=3.14*radius*radius; return area; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package bzu.info.software; public class Cylinder { Cricle bottom;//A类的变量成员是B类的对象 double height; double volumn; Cylinder(Cricle c,double h){//A类中某个方法的参数是B类的对象 bottom=c; height=h; } double getVolumn(){ volumn=bottom.getArea()*height; return volumn; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
package bzu.info.software; public class A { public static void main(String[] args) { // TODO Auto-generated method stub Cricle cr=new Cricle(2.0); Cylinder cy=new Cylinder(cr,5.0); System.out.println(cy.getVolumn()); } }

这里写图片描述

改变所在包的位置

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package bzu.info.software; public class Cricle { double radius; double area; public Cricle(double r){ radius=r; } public double getArea(){ area=3.14*radius*radius; return area; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package bzu.info.com; import bzu.info.software.*; public class Cylinder { Cricle bottom; double height; double volume; public Cylinder(Cricle c,double h){//A类中某个方法的参数是B类的对象 bottom=c; height=h; } public double getVolume(){ volume=bottom.getArea()* height; return volume; } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package bzu.info.com; import bzu.info.software.*; public class A { public static void main(String[] args) { // TODO Auto-generated method stub Cricle cr=new Cricle(2.0); Cylinder cy=new Cylinder(cr,5.0); System.out.println(cy.getVolume()); } }

所在包的位置发生改变,需要使用import bzu.info.software.*;来使用其中类和方法,其中的类和方法 必须是public,如果使用其他的访问权限,调用的类和方法将不可见

最后

以上就是贪玩荷花最近收集整理的关于Package与Import的全部内容,更多相关Package与Import内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部