-1

通过设计一个计算不同形状周长的程序,我被分配在 Java 中执行继承中的方法重载,我设计了如下所示的代码,但是当我尝试编译时,出现错误。

import java.io.*;
import java.util.*;
public class Perimeter {
public double getperimeter(int constant,double pi,double radius){
return(constant*pi*radius);
}
public double getperimeter(int sconstant,double length){
return(sconstant*length);
}
public double getperimeter(int rconstant,double rlength,double widith){
return(rconstant*(rlength+widith));
}
public double getperimeter(double base,double height,double hypotenuse){
return(base+height+hypotenuse);
}
public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;

final int sconstant=4;
double length;

final int rconstant=2;
double rlength;
double widith;

double base;
double height;
double hypotenuse;

Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
 System.out.println("Enter The Radius");
 g.radius=s.nextDouble();
 System.out.println("Enter The Square Length");
 g.lenght=s.nextInt();
 System.out.println("Enter The Rectangle Lenght");
 g.rlength=s.nextInt();
 System.out.println("Enter The Rectangle widith");
 g.widith=s.nextInt();
 System.out.println("Enter The Triangle Base");
 g.base=s.nextInt();
 System.out.println("Enter The Triangle height");
 g.height=s.nextInt();
 System.out.println("Enter The Triangle hypotenuse");
 g.hypotenuse=s.nextInt();

System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
4

4 回答 4

5

这是学者们喜爱的“经典”压倒一切的问题。(其他涉及动物或车辆。)

从一个Shape界面开始:

public interface Shape {
    double getPerimeter();
}

然后让子类以自己的方式实现它:

public class Rectangle implements Shape {
    private double height;
    private double width;

    public Rectangle(double w, double h) {
        this.width = w;
        this.height = h;
    }

    public double getPerimeter() { return 2.0*(this.width + this.height); }
}


public class Circle implements Shape {
    private double radius; 

    public Circle(double r) {
        this.radius = r; 
    }

    public double getPerimeter() { return 2.0*Math.PI*this.radius; }
}

您将能够执行以下操作:

List<Shape> shapes = new ArrayList<Shape>();
shapes.add(new Rectangle(1.0, 2.0));
shapes.add(new Circle(10.0));
for (Shape shape : shapes) {
    System.out.println(shape.getPerimeter()); // every Shape will do it their own way
}

添加一个新的 Shape 实现,您的原始代码仍然可以正常工作。它是多态性和动态绑定的本质。

于 2013-03-15T12:22:04.640 回答
4

您在一个类中定义了两个具有相同签名的方法。这是一个错误。你的方法:

double getperimeter(int constant,double pi,double radius);
double getperimeter(int rconstant,double rlength,double widith);

此外,您的主要方法必须声明为静态

于 2013-03-15T12:08:38.363 回答
0

您似乎对 java 仍然不熟悉,因此开始帮助:

  • 主要方法是静态的,即首先执行。

  • 您正在使用重载方法 getperimeter,您可以在其中轻松混淆 int 和 double。也许选择一个独特的名字。Java 约定是使用有趣的驼峰式案例:getPerimeter。

    import java.util.*;
    
    public class Perimeter {
    
    public static void main(String args[]) {
        new Perimeter().execute();
    }
    
    public double getperimeter(int constant, double pi, double radius) {
        return (constant * pi * radius);
    }
    
    public double getperimeter(int sconstant, double length) {
        return (sconstant * length);
    }
    
    public double getperimeterRLenghtWidith(int rconstant, double rlength, double widith) {
        return (rconstant * (rlength + widith));
    }
    
    public double getperimeter(double base, double height, double hypotenuse) {
        return (base + height + hypotenuse);
    }
    
    private void execute() {
        final double pi = Math.PI; //22 / 7;
        final int constant = 2;
        double radius;
    
        final int sconstant = 4;
        double length;
    
        final int rconstant = 2;
        double rlength;
        double widith;
    
        double base;
        double height;
        double hypotenuse;
    
        Scanner s = new Scanner(System.in);
        System.out.println("Enter The Radius");
        radius = s.nextDouble();
        System.out.println("Enter The Square Length");
        length = s.nextInt();
        System.out.println("Enter The Rectangle Lenght");
        rlength = s.nextInt();
        System.out.println("Ener The Rectangle widith");
        widith = s.nextInt();
        System.out.println("Enter The Triangle Base");
        base = s.nextInt();
        System.out.println("Enter The Triangle height");
        height = s.nextInt();
        System.out.println("Enter The Triangle hypotenuse");
        hypotenuse = s.nextInt();
    
        System.out.println("Perimeter = " + getperimeter(constant, pi, radius));
        System.out.println("Perimeter = " + getperimeter(sconstant, length));
        System.out.println("Perimeter = " + getperimeterRLenghtWidith(rconstant, rlength, widith));
        System.out.println("Perimeter = " + getperimeter(base, height, hypotenuse));
    
    }
    }
    
于 2013-03-15T12:18:38.283 回答
0

嗨,您应该在不同的文件中有一个 Perimeter 类。我看到缺少关闭类 Perimeter 的括号。也像 Xavier 提到的那样,您有两种具有相同签名的方法。您还将从 Scanner 读取值到 Perimeter 实例变量,然后将未初始化的值传递给 Perimeter 方法。所以我会分离 Perimeter 类,主要将值读取到局部变量并将它们传递给 Perimeter 方法。

public void main (String args []){
final double pi=22/7;
final int constant=2;
double raduius;

final int sconstant=4;
double length;

final int rconstant=2;
double rlength;
double widith;

double base;
double height;
double hypotenuse;

Perimeter g= new Perimeter();
Scanner s=new scanner(System.in);
 System.out.println("Enter The Radius");
 radius=s.nextDouble();
 System.out.println("Enter The Square Length");
 lenght=s.nextInt();
 System.out.println("Enter The Rectangle Lenght");
 rlength=s.nextInt();
 System.out.println("Enter The Rectangle widith");
 widith=s.nextInt();
 System.out.println("Enter The Triangle Base");
 base=s.nextInt();
 System.out.println("Enter The Triangle height");
 height=s.nextInt();
 System.out.println("Enter The Triangle hypotenuse");
 hypotenuse=s.nextInt();

System.out.println("Perimeter = " + g.getperimeter(constant,pi,radius));
System.out.println("Perimeter = " + g.getperimeter(sconstant,length));
System.out.println("Perimeter = " + g.getperimeter(rconstant,rlength,widith));
System.out.println("Perimeter = " + g.getperimeter(base,height,hypotenuse));
于 2013-03-15T12:15:47.450 回答