【问题标题】:No suitable constructor found for [duplicate]没有找到适合[重复]的构造函数
【发布时间】:2016-11-17 18:08:39
【问题描述】:

我在让 TestPolygon.java 正确运行时遇到问题。 THE "//创建四个多边形的构造函数,其值不等于默认值或彼此不相等 Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2 ,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)}; “我的代码行不断返回,每条新语句都会出现错误,说“没有合适的构造函数找到 Polygon(int,double,double,double)。图片附上。

/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/

import java.lang.Math;

public class Polygon {
 private int numSides;
 private double sideLength;
 private double xCoord;
 private double yCoord;
 private double apothem;
 private double perimeter;

 //Constructor that creates a Polygon using the defaults
 public Polygon(){
     numSides = 4;
     sideLength = 10.0;
     xCoord = 0.0;
     yCoord = 0.0;
     apothem = 5.0;
}
 //Constructor that uses the specified numbers
 public Polygon(int s,double sL,double x,double y,double a, double p){
     setNumSides(s);
     setSideLength(sL);
     setXCoord(x);
     setYCoord(y);
     setApothem(a);
}

 // A getArea() method that returns a double value for the area of the polygon
 public double getArea() {
 return .5*apothem*perimeter;
}

 // Getter and setter for all data fields
 public int setNumSides(int s){
      numSides = s;
      return numSides;
}
 public double setSideLength(double sL){
      sideLength = sL;
      return sideLength;
}
 public double setXCoord(double x){
      xCoord = x;
      return xCoord;
}
 public double setYCoord(double y){
      yCoord = y;
      return yCoord;
}
 public double setApothem(double a){
      apothem = a;
      return apothem;
}
 public double setPerimeter(double p){
       perimeter = p;
       return perimeter;
}
 public int getNumSides() {
 return numSides; 
 }
 public double getSideLength() { 
 return sideLength; 
 }
 public double getXCoord() { 
 return xCoord; 
 }
 public double getYCoord() {
 return yCoord; 
 }
 public double getApothem() {
 return apothem;
 }
 public double getPerimeter() {
 return perimeter;
 }

 //A toString() that displays the values in string format
 public String toString(){
      return "("+numSides+", "+String.format("%.1f",sideLength)+", "+String.format("%.1f",xCoord)+", "+String.format("%.1f",yCoord)+")";
 }
}

/* File: Polygon.java
* Author: Darren Pirtle Jr.
* Date: November 16, 2016
* Purpose: Design a Java class
* named Polygon
*/

//Import necessary packages
import java.util.Scanner;
import java.text.DecimalFormat;
import java.lang.Math;

class TestPolygon{
    public static void main(String[] args){

    //Constructor that creates one polygon with no argument
    Polygon P = new Polygon();

    //Constructor that creates four polygons with any value that are not equal to the defualt value or to each other
    Polygon[] P_array = {new Polygon(10,5.0,1.0,3.0), new Polygon(6,3,2,5), new Polygon(4,11,4,3), new Polygon(5,12,6,7)};

    //Constructor that calls all methods and displays results
    System.out.println("toString() results: "+P);
    System.out.println("getNumSides() results: "+ P.getNumSides());
    System.out.println("getSideLength() results: "+ P.getSideLength());
    System.out.println("getXCoord() results: "+ P.getXCoord());
    System.out.println("getYCoord() results: "+ P.getYCoord());
    System.out.println("getPerimeter() results: "+ P.getPerimeter());
    System.out.println("setNumSides(4) results: "+ P.setNumSides(4));
    System.out.println("setSideLength(3.0) results: "+ P.setSideLength(3.0));
    System.out.println("setXCoord(2.0) results: "+ P.setXCoord(2));
    System.out.println("setYCoord(2.0) results: "+ P.setYCoord(2));
    for(int i=0; i<P_array.length; i++)
    {
    System.out.println("toString() results: "+P_array[i]);
    System.out.println("getNumSides() results: "+ P_array[i].getNumSides());
    System.out.println("getSideLength() results: "+ P_array[i].getSideLength());
    System.out.println("getXCoord() results: "+ P_array[i].getXCoord());
    System.out.println("getYCoord() results: "+ P_array[i].getYCoord());
    System.out.println("getPerimeter() results: "+ P_array[i].getPerimeter());
    System.out.println("setNumSides(4) results: "+ P_array[i].setNumSides(4));
    System.out.println("setSideLength(3.0) results: "+ P_array[i].setSideLength(3.0));
    System.out.println("setXCoord(2.0) results: "+ P_array[i].setXCoord(2));
    System.out.println("setYCoord(2.0) results: "+ P_array[i].setYCoord(2));
    }
    }
}

enter image description here

【问题讨论】:

  • 错误信息对我来说似乎很明显。您的 Polygon() 构造函数接受一个 int 和 5 个双精度数。您正在尝试使用采用 int 和 4 双打的构造函数。在这里进行一点点研究可能会有很长的路要走。
  • 你到底想用new Polygon(10, 5.0, 1.0, 3.0)调用哪个构造函数?将其预期的参数数量与您实际提供的参数数量进行比较。它们匹配吗?
  • 我确实做过研究。我在这里发帖的原因是我需要帮助,因为我没有找到任何东西。感谢您的粗鲁评论和帮助!
  • 它实际上是一个 int 和 3 其他值,总共 4 个。您还需要将设置器修复为 void 类型并且不返回任何内容.另外,Java 和 JavaScript 是独立的、不相关的语言。
  • 研究包括谷歌搜索错误信息。

标签: java javac


【解决方案1】:

问题是您使用 4 个参数调用 Polygon(),但您没有 4 个参数的构造函数。你没有参数

 public Polygon(){
     numSides = 4;
     sideLength = 10.0;
     xCoord = 0.0;
     yCoord = 0.0;
     apothem = 5.0;
}

和 6 个参数

public Polygon(int s,double sL,double x,double y,double a, double p){
 setNumSides(s);
 setSideLength(sL);
 setXCoord(x);
 setYCoord(y);
 setApothem(a);
}

但是你在打电话

new Polygon(10,5.0,1.0,3.0)

您可以向新的 Polygon 调用添加 2 个参数,或者从 6 arg 构造函数中删除 2 个参数,或者创建一个接受 4 个参数的构造函数。

【讨论】:

  • 构造函数有 6 个参数。
  • 谢谢。数数很难。
【解决方案2】:

您的参数化构造函数接受 6 个参数,即

  • int s
  • 双SL
  • 双倍
  • 双倍
  • 双a
  • 双倍

但是,当你构建多边形时,你只传递了四个值,即 new Polygon(10,5.0,1.0,3.0)

由于您没有使用构造函数中的最后一个参数,即“double p”,因此您的构造函数应该是:public Polygon(int s,double sL,double x,double y,double a)

【讨论】:

    【解决方案3】:

    错误信息说明了一切:

    没有找到适合 Polygon(int,double,double,double) 的构造函数。

    这只是意味着Java无法找到带有参数(int, double, double, double)的Polygon类的构造函数(这是您在创建Polygon对象时使用的)

    如果您查看 Polygon 类的构造函数:

    您创建了 2 个构造函数:

    • 无参数构造函数

    • 具有 6 个参数(int、double、double、double、double、double)的构造函数


    因此,在创建 Polygon 对象时,请确保您正在执行以下操作:

    new Polygon(); 
    or
    new Polygon(int, double, double, double, double, double);
    

    示例:

    Polygon p1 = new Polygon();
    Polygon p2 = new Polygon(10, 2.5, 3.5, 4.5, 5.5, 6.5);
    Polygon p3 = new Polygon(10, 20, 30, 40, 50, 60);    //this is ok too
    

    现在,您只提供 4 个参数:

    new Polygon(10, 5.0, 1.0, 3.0), 
                 ^   ^    ^    ^
                 |   |    |    |
                int  d    d    d  (d for double)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多