Java toString()和重写方法的问题
我对此代码有问题。它没有正确计算面积。我需要它来计算汽缸/活塞的横截面积和汽缸的表面积。如果您注意到输出,当我使用getArea()方法时,两个值似乎是相同的值。Java对使用什么getArea()感到困惑?强文本我试图用getArea()[横截面积]覆盖getArea()[表面积],但它不起作用Java toString()和重写方法的问题,java,overriding,tostring,Java,Overriding,Tostring,我对此代码有问题。它没有正确计算面积。我需要它来计算汽缸/活塞的横截面积和汽缸的表面积。如果您注意到输出,当我使用getArea()方法时,两个值似乎是相同的值。Java对使用什么getArea()感到困惑?强文本我试图用getArea()[横截面积]覆盖getArea()[表面积],但它不起作用 import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(
"\n\nInput the radius, then the height, then if the cylinder is filled (input 1 if it is filled and input 0 if not, and then enter the color");
double radius = scan.nextDouble();
double height = scan.nextDouble();
int filled = scan.nextInt();
boolean isFilled;
if (filled == 1) {
isFilled = true;
} else {
isFilled = false;
}
String c = scan.nextLine();
String x = scan.nextLine();
Cylinder cyl = new Cylinder(radius, height, isFilled, x);
System.out.println("\n\n This is a Cylinder and its properties");
System.out.println(cyl);
Piston small= new Piston();
System.out.println ("\n\n This is small Piston");
System.out.println(small);
Piston big = new Piston(55.6, 1234.4, true, "red", 1200, 12.3);
System.out.println ("\n\n This is big Piston");
System.out.println(big);
}
}
class Shape {
private String color = "yellow";
private boolean filled;
public Shape() {
}
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public String toString() {
return "\nThe color is : " + color + " and shape fill is : " + filled;
}
}
class Cylinder extends Shape {
private double cylinderRadius;
private double cylinderHieght;
public Cylinder() {
cylinderHieght = 10;
cylinderRadius = 2.5;
}
public Cylinder(double height, double radius) {
cylinderRadius = radius;
cylinderHieght = height;
}
public Cylinder(double height, double radius, boolean filled, String color) {
super(color, filled);
cylinderRadius = radius;
cylinderHieght = height;
}
public double getRadius() {
return cylinderRadius;
}
public double getHieght() {
return cylinderHieght;
}
public double getArea() {
double p = 3.14;
return 2 * p * cylinderRadius * cylinderRadius + 2 * p * cylinderRadius * cylinderHieght;
}
public double getVolume() {
double p = 3.14;
return p * cylinderRadius * cylinderRadius * cylinderHieght;
}
@Override
public String toString() {
return super.toString() + " \nRadius= " + cylinderRadius + " Height= " + cylinderHieght
+ " Cylinder total surface Area= " + getArea() + " volume= " + this.getVolume() + ".";
}
}
class Piston extends Cylinder{
private double shaftLength;
private double myPressure;
public Piston(){
shaftLength=1;
myPressure=1;
}
public Piston(double height, double radius, boolean filled, String color, double length, double pressure){
super(height, radius, filled, color);
shaftLength=length;
myPressure=pressure;
}
public double getShaftLength(){
return shaftLength;
}
public double getPressure(){
return myPressure;
}
@Override
public double getArea(){
return getRadius()*getRadius()*3.14;
}
public double getVolume(){
return super.getVolume();
}
@Override
public String toString(){
return super.toString()+"\n the cross sectional area of Piston = "+this.getArea()+" shaftlength="+shaftLength+" pressure="+myPressure+" .";
}
}
这里是输入半径为5,高度为10的输出
输入半径,然后输入高度,然后输入圆柱体是否已填充
(如果已填充,则输入1;如果未填充,则输入0,然后输入颜色
5101蓝色
这是一个圆柱体及其属性
颜色为:蓝色,形状填充为:真实半径=10.0高度=5.0
气缸总表面积=942.0体积=1570.0
这是一个小活塞
颜色为:黄色,形状填充为:假半径=2.5高度=
10.0汽缸总表面积=19.625容积=196.25。t活塞横截面积=19.625轴长=1.0压力=1.0
这是大活塞
颜色为:红色,形状填充为:真半径=1234.4高度=
55.6气缸总表面积=4784554.150400002体积=2.6602121076224005E8。活塞横截面积=4784554.150400002轴长=1200.0压力=12.3
澄清一下,情况似乎是这样的:
Cylider
)定义了一个方法(getArea()
)来完成一件事。Cylider上的另一个方法(toString()
)调用该方法活塞
)将Chylidar
分为子类,并重写getArea()
函数以执行不同的操作getArea()
类中编写代码时,您希望它使用getArea()
的Cylider
版本,因为代码是在同一类中编写的,但实际上它使用了getArea()的被重写版本
因为你调用它的对象实际上是一个活塞
Cylinder
上创建一个名为getCylinderArea()
Cylider.getArea()的新方法。默认情况下,getArea()可以调用此函数,但您可以在Cylider.toString()中明确地调用它
如果您希望该函数仅使用平面圆柱体法计算面积
或者,也许活塞实际上不应该是气缸的子类型,因为即使它们具有某些共同的特征,活塞也不能在任何使用气缸的地方替代普通气缸——例如,在计算面积时。请查阅
但就我个人而言,正是出于这种原因,我倾向于完全远离继承。实际上,我建议在这种情况下只使用接口和平面层次结构
如果您想了解有关调用超级方法的更多详细信息,可能会很有趣。请添加Java标记。