Java中如何使用对象数组
Java中类的对象可以形成对象数组,然后我们可以定义,作为形参传给函数。需要注意的是对象数组的类型是classNum [] 。对象数组中的每个对象都需要new申请一下。
/*** * * @author Administrator * */ public class TestingObjectArray { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Circle4[] circle = new Circle4[5]; for(int i = 0; i < circle.length; i++) { circle[i] = new Circle4(Math.random() * 100); } for(int j = 0; j < circle.length; j++) { System.out.println("The area of " + j + " circle is " + circle[j].getArea()); } } } class Circle4 { /** * CLASS CIRCLE4 * * Data: radius * Functions: getRadius, setRadius, getArea */ double radius; Circle4() { radius = 0; } Circle4(double newRadius) { radius = newRadius; } double getRadius() { return radius; } void setRadius(double newRadius) { radius = newRadius; } double getArea() { return Math.PI * radius * radius; } }
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: python中array和matrix的区别