程序应打印形状的周长,面积和平均长度,该形状在超类“Shape”的子类中定义
但所有打印都是“形状”。
我知道它只是在语法上的一个调整,但已经尝试了几个小时来找到问题所在。我想知道你们中是否有人可以给我一些指示?谢谢,非常感谢您的帮助。
→为了便于理解,我粘贴了我的程序的4个部分,
主要:
package shape;
import java.util.ArrayList;
import java.util.Scanner;
@author Fulltime
public class MainExecute {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Shape> list = new ArrayList<>();
Triangle t;
Square s;
Trapezoid r;
while(true){
Scanner Choice = new Scanner(System.in);
System.out.println("Enter a shape: ");
String choice = Choice.nextLine();
if(choice.equalsIgnoreCase("STOP")){
break;
}
else if(choice.equalsIgnoreCase("triangle")){
System.out.print("Enter base of triangle: ");
double base = Choice.nextDouble();
System.out.print("Enter height of triangle: ");
double height = Choice.nextDouble();
t = new Triangle(base, height);
list.add(t);
}
else if(choice.equalsIgnoreCase("square")){
System.out.print("Enter side of square: ");
double side = Choice.nextDouble();
s = new Square(side);
list.add(s);
}
else if(choice.equalsIgnoreCase("trapezoid")){
System.out.print("Enter length1 of trapezoid: ");
double length1 = Choice.nextDouble();
System.out.print("Enter length2 of trapezoid: ");
double length2 = Choice.nextDouble();
System.out.print("Enter height of trapezoid: ");
double height = Choice.nextDouble();
r = new Trapezoid(length1, length2, height);
list.add(r);
}
}
Shape q;
System.out.println("Shapes: ");
for(int i = 0; i <list.size(); i++){
q = list.get(i);
System.out.println(q.getClass().getName());
if(q.getClass().getName().equalsIgnoreCase("Triangle")){
t=(Triangle)q;
System.out.println("Perimeter: " + t.getPerimeter());
System.out.println("Area: " + t.getArea());
}
if(q.getClass().getName().equalsIgnoreCase("Square")){
s=(Square)q;
System.out.println("Perimeter: " + s.getPerimeter());
System.out.println("Area: " + s.getArea());
System.out.println("Average length of sides: " + s.getAverage());
}
if(q.getClass().getName().equalsIgnoreCase("Trapezoid")){
r=(Trapezoid)q;
System.out.println("Perimeter: " + r.getPerimeter());
System.out.println("Area: " + r.getArea());
System.out.println("Average length of sides: " + r.getAverage());
}
}
}
}
形状超类
package shape;
import java.util.*;
public abstract class Shape {
public abstract double getPerimeter ();
public abstract double getArea ();
public double Perimeter;
public double Area;
public void displayInfo(){
//System.out.println("Perimeter: " + this.getPerimeter());
//System.out.println("Area: " + this.getArea());
}
}
平行四边形界面
package shape;
@author Fulltime
public interface Parallelogram {
public double getAverage();
}
Square Subclass
package shape;
import static java.lang.Math.*;
@author Fulltime
public class Square extends Shape implements Parallelogram {
public Square(){}
@Override
public double getPerimeter (){
Perimeter = side * 4;
return Perimeter;
}
@Override
public double getArea (){
Area = side * side;
return Area;
}
@Override
public double getAverage(){
double Sides;
Sides = (this.side + this.side + this.side + this.side) / 4;
return Sides;
}
public double side;
/**
* @return the side
*/
public double getSide() {
return side;
}
/**
* @param side the side to set
*/
public void setSide(double side) {
this.side = side;
}
public Square (double side){
this.side = side;
}
public void printSquare(){
System.out.println("The Perimeter of this shape is " + getPerimeter());
System.out.println("The Area of this shape is " + getArea());
System.out.println("The Average Length of this shape's sides is " + getAverage());
}
}
答案 0 :(得分:3)
使用getSimpleName()返回没有包资格的类名。
if(q.getClass().getSimpleName().equalsIgnoreCase("Triangle")){
}else if(..) // Also use else-if
或者您可以使用instanceof
运算符
if(q instanceof Triangle){
//logic here
}else if(..)
现在作为一个注释,这不是一个很好的OO设计,使用if-else,你应该重新考虑重新设计你的模型。
例如make displayInformation
abstract,那么所有具体的子类都必须覆盖它。
abstract class Shape{
public abstract void displayInformation();
}
三角
public class Triangle extends Shape implements whatyouwant {
@Override
public void displayInformation(){
System.out.println("Perimeter: " + this.getPerimeter());
System.out.println("Area: " + this.getArea());
}
}
广场:
public class Square extends Shape ..{
@Override
public void displayInformation(){
System.out.println("Perimeter: " + this.getPerimeter());
System.out.println("Area: " + this.getArea());
System.out.println("Average length of sides: " + this.getAverage());
}
}
那么在您的主要课程中,您不必编写任何if-else
代码,只需看到polimorphism magic。
for(Shape shape : list){//use enhanced loop
shape.displayInformation();
}