程序报错找不到合适的构造器请问是什么原因?怎么解决?

xy678910 2019-12-03 05:23:50
class Sphere{
static final double PI=3.14;
static int count=0;
double radius;
double xCenter;
double yCenter;
double zCenter;
// Construct a unit sphere at a point
Sphere(double x,double y,double z){
xCenter=x;
yCenter=y;
zCenter=z;
radius=1.0;
++count; // Update object count
}
static int getCount(){
return count;
}
//Construct a unit sphere at the origin
Sphere() {
xCenter = 4.0;
yCenter = 0.0;
zCenter = 0.0;
radius = 1.0;
++count; // Update object count
}
// Instance method to calculate volume
double volume(){
return 4.0/3.0*PI*radius*radius*radius;
}
}
public class CreateSphere {
public static void main(String []args){
System .out .println("Number of objects="+ Sphere.getCount());
Sphere ball=new Sphere(4.0,0.0,0.0,0.0); // Create a sphere
Sphere globe=new Sphere(12.0,1.0,1.0,1.0); // Create a sphere
System .out .println("Number of objects="+Sphere.getCount());
System .out .println("Number of objects="+ball.getCount());
Sphere eightBall=new Sphere(10.0,10.0,0.0);
Sphere oddBall=new Sphere();
System .out .println("Number of objects="+Sphere.getCount());
// Output the volume of each sphere
System .out .println("ball volume="+ball.volume());
System .out .println("globe volume="+globe.volume());
System .out .println("eightBall volume="+eightBall.volume());
System .out .println("oddBall volume="+oddBall.volume());
}
}
Error:(35, 21) java: 对于Sphere(double,double,double,double), 找不到合适的构造器
构造器 Sphere.Sphere(double,double,double)不适用
(实际参数列表和形式参数列表长度不同)
构造器 Sphere.Sphere()不适用
(实际参数列表和形式参数列表长度不同)
Error:(36, 22) java: 对于Sphere(double,double,double,double), 找不到合适的构造器
构造器 Sphere.Sphere(double,double,double)不适用
(实际参数列表和形式参数列表长度不同)
构造器 Sphere.Sphere()不适用
(实际参数列表和形式参数列表长度不同)


...全文
798 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你没有定义四个参数的构造方法,没找到,要么删掉参数, 要么多定义一个构造方法
hwj运维之路 2019-12-03
  • 打赏
  • 举报
回复
Sphere(double x,double y,double z){
xCenter=x;
yCenter=y;
zCenter=z;
radius=1.0;
++count; // Update object count
}

Sphere类只有三个参数的构造方法,你new的时候写了四个参数,要么删掉最后一个参数,要么改
Sphere(double x,double y,double z,double radius){
xCenter=x;
yCenter=y;
zCenter=z;
radius=radius;
++count; // Update object count
}

按你的业务需求来

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧