+triangle.a+'、'+triangle.b+'、'+triangle.c);什么意思
时间: 2023-07-02 22:20:43 浏览: 162
这是一个字符串拼接的表达式,其中 `triangle.a`、`triangle.b` 和 `triangle.c` 分别表示一个三角形对象(`triangle`)的三条边的长度,加号(`+`)则是用来将它们拼接在一起的。
例如,如果 `triangle.a`、`triangle.b` 和 `triangle.c` 的值分别为 3、4 和 5,则该表达式的结果为一个字符串:`3、4、5`。
相关问题
public class Test { public static void main(String[] args) { Rectangle rectangle = new Rectangle(3.00, 4.00); System.out.println("矩形的长为"+rectangle.length+','+"宽为"+rectangle.wide); System.out.printf("矩形的周长为:%5.2f,面积为:%5.2f",rectangle.perimeter(),rectangle.area()); System.out.println(""); Cone cone = new Cone(rectangle, 3.00,"矩形"); System.out.printf("底面积%5.2f,高为%.2f的%s锥,体积为:%-5.2f",rectangle.area(),cone.height,cone.coneName,cone.V()); Triangle triangle = new Triangle(3.0, 4.0, 5.0); System.out.println("三角形的三边长为:"+triangle.a+'、'+triangle.b+'、'+triangle.c); System.out.printf("三角形的周长为:%5.2f,面积为:%5.2f",triangle.perimeter(),triangle.area()); System.out.println(""); Cone cone1 = new Cone(triangle, 3.00,"三角形"); System.out.printf("底面积%5.2f,高为%.2f的%s锥,体积为:%-5.2f",triangle.area(),cone1.height,cone1.coneName,cone1.V()); Cicle cicle = new Cicle(3.0); System.out.println("圆的半径为:"+cicle.r); System.out.printf("圆的周长为:%5.2f,面积为:%5.2f",cicle.perimeter(),cicle.area()); System.out.println(""); Cone cone2 = new Cone(cicle, 3.00,"圆"); System.out.printf("底面积:%5.2f, 高为%.2f的%s锥, 体积为:%-5.2f", cicle.area(), cone2.height, cone2.coneName, cone2.V()); } }如何修改使用上转型对象
可以使用父类的引用来引用子类对象,这样就可以实现上转型。具体修改如下:
```
public class Test {
public static void main(String[] args) {
Shape shape;
Rectangle rectangle = new Rectangle(3.00, 4.00);
System.out.println("矩形的长为"+rectangle.length+','+"宽为"+rectangle.wide);
System.out.printf("矩形的周长为:%5.2f,面积为:%5.2f",rectangle.perimeter(),rectangle.area());
System.out.println("");
shape = rectangle;
Cone cone = new Cone(shape, 3.00,"矩形");
System.out.printf("底面积%5.2f,高为%.2f的%s锥,体积为:%-5.2f",rectangle.area(),cone.height,cone.coneName,cone.V());
System.out.println("");
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
System.out.println("三角形的三边长为:"+triangle.a+'、'+triangle.b+'、'+triangle.c);
System.out.printf("三角形的周长为:%5.2f,面积为:%5.2f",triangle.perimeter(),triangle.area());
System.out.println("");
shape = triangle;
Cone cone1 = new Cone(shape, 3.00,"三角形");
System.out.printf("底面积%5.2f,高为%.2f的%s锥,体积为:%-5.2f",triangle.area(),cone1.height,cone1.coneName,cone1.V());
System.out.println("");
Cicle cicle = new Cicle(3.0);
System.out.println("圆的半径为:"+cicle.r);
System.out.printf("圆的周长为:%5.2f,面积为:%5.2f",cicle.perimeter(),cicle.area());
System.out.println("");
shape = cicle;
Cone cone2 = new Cone(shape, 3.00,"圆");
System.out.printf("底面积:%5.2f, 高为%.2f的%s锥, 体积为:%-5.2f", cicle.area(), cone2.height, cone2.coneName, cone2.V());
}
}
```
这样就可以通过 `Shape` 类型的引用来引用所有的子类对象,实现了上转型。
补全代码测试输入:9 8 7 6 5 4 3 2 1 预期输出: Circle.Area=254.34 Circle.Circumference=56.52 Rectangle.Area=56.0 Rectangle.Circumference=30.0 Triangle.Area=9.921567416492215 Triangle.Circumference=15.0 Triangle.Area=0.0 Triangle.Circumference=6.0package module3; import java.util.Scanner; public class IShapeTest { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); Double radius = scanner.nextDouble(); Circle circle = new Circle(radius); System.out.println("Circle.Area=" + circle.getArea()); System.out.println("Circle.Circumference=" + circle.getCircumference()); …} /********** End *********/
/********** Start *********/
Double[] sides = new Double[3];
for (int i = 0; i < 3; i++) {
sides[i] = scanner.nextDouble();
}
Triangle triangle = new Triangle(sides[0], sides[1], sides[2]);
System.out.println("Triangle.Area=" + triangle.getArea());
System.out.println("Triangle.Circumference=" + triangle.getCircumference());
Double width = scanner.nextDouble();
Double height = scanner.nextDouble();
Rectangle rectangle = new Rectangle(width, height);
System.out.println("Rectangle.Area=" + rectangle.getArea());
System.out.println("Rectangle.Circumference=" + rectangle.getCircumference());
/********** End **********/
阅读全文