thrift1.0支持异构容器吗,想用它定义Java的一个Object数组,不知道怎么实现啊 50
1个回答
展开全部
public class ObjectArray {
public static void main(String[] args) {
Object[] array = new Object[5];
Rect rect = new Rect(3, 4);//矩形
array[0] = rect;
Circle circle = new Circle(7, 3.14);// 圆
array[1] = circle;
Double d = new Double(5.2);// 双精度小数
array[2] = d;
Float f = new Float(5.2);// 单精度小数
array[3] = f;
Integer i = new Integer(7);//整数
array[4] = i;
for(Object o : array){
System.out.println(o);
}
}
}
class Rect {
private double length;// 长
private double width;// 宽
public Rect() {
super();
}
public Rect(double length, double width) {
super();
this.length = length;
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
// 计算面积
public double getArea(){
return this.length*this.width;
}
@Override
public String toString() {
return "Rect [length=" + length + ", width=" + width + "]";
}
}
class Circle {
private double r;// 半径
private double pi;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circle() {
}
public Circle(double r) {
this.r = r;
}
public double getPi() {
return pi;
}
public void setPi(double pi) {
this.pi = pi;
}
public Circle(double r, double pi) {
this.r = r;
this.pi = pi;
}
// 计算面积
public double getArea(){
return pi*Math.pow(r, 2);
}
// 计算周长
public double getZhouChang(){
return 2*pi*r;
}
@Override
public String toString() {
return "Circle [r=" + r + ", pi=" + pi + "]";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询