黑马程序员技术交流社区

标题: 今天试着编写了一个小程序 不知道哪错了 求指导(谢谢各..... [打印本页]

作者: 戎石锁    时间: 2012-8-6 19:41
标题: 今天试着编写了一个小程序 不知道哪错了 求指导(谢谢各.....
本帖最后由 戎石锁 于 2012-8-8 19:43 编辑

class Point
{        
        double a,b,c;
        Point(double a1,double b1,double c1)
        {         
                a = a1;         b = b1;         c = c1;        
        }
        void setA(double a1)
        {         
                a = a1;        
        }        
        void setB(double b1)
        {        
                b = b1;        
        }        
        void setC(double c1)
        {        
                c = c1;        
        }
        double getDistance(Point p)
        {         
                return (a-p.a)*(a-p.a)+(b-p.b)*(b-p.b)+(c-p.c)*(c-p.c);        
        }
}

public class TestPoint
{        
        public static void main(String[] args)
        {
                Point p = new Point();        
                Point p1 = new Point(2.2,2.2,2.2);        
                System.out.println(p.getDistance(p1));
        }
}

1212.jpg (12.92 KB, 下载次数: 0)

1212.jpg

作者: 杜佳瑞    时间: 2012-8-6 19:48
本帖最后由 杜佳瑞 于 2012-8-6 19:52 编辑

你已经定义了一个带参构造函数,就不会再有系统隐式的构造参数了,除非你再自己定义
楼主你写这个程序干吗用啊
作者: 刘润辰    时间: 2012-8-6 19:51
Point p = new Point();        
你的构造函数啊!!!
class Point
{        
        double a,b,c;
        Point(double a1,double b1,double c1)
        {         
                a = a1;         b = b1;         c = c1;        
        }
你写了一个构造函数,里边有三个参数。但是下边你new出来的 p对象并没有传参。
class Point
{        
        double a,b,c;
        Point(double a1,double b1,double c1)
        {         
                a = a1;         b = b1;         c = c1;        
        }
        Point()
        {         
                     
        }
        void setA(double a1)
        {         
                a = a1;        
        }        
        void setB(double b1)
        {        
                b = b1;        
        }        
        void setC(double c1)
        {        
                c = c1;        
        }
        double getDistance(Point p)
        {         
                return (a-p.a)*(a-p.a)+(b-p.b)*(b-p.b)+(c-p.c)*(c-p.c);        
        }
}
在重写一个构造函数记不会出错误了。
作者: 赵俊杰    时间: 2012-8-6 19:57
本帖最后由 赵俊杰 于 2012-8-6 19:59 编辑

class Point
{        
        double a,b,c;
        Point(double a1,double b1,double c1)
        {         
                a = a1;         b = b1;         c = c1;        
        }
  //有参构造函数      
   void setA(double a1)
        {         
                a = a1;        
        }        
        void setB(double b1)
        {        
                b = b1;        
        }        
        void setC(double c1)
        {        
                c = c1;        
        }
        double getDistance(Point p)
        {         
                return (a-p.a)*(a-p.a)+(b-p.b)*(b-p.b)+(c-p.c)*(c-p.c);        
        }
}

public class TestPoint
{        
        public static void main(String[] args)
        {
                Point p = new Point();        
                Point p1 = new Point(2.2,2.2,2.2);        
                System.out.println(p.getDistance(p1));
        }
}

你已经主动构造了一个有参函数,编译器就不再帮你做无参函数了。因此Point类其实只有一个有参构造函数。
所以,Point p1 = new Point(2.2,2.2,2.2); 正确。而Point p1 = new Point();因Point其实不存在"无参构造函数",所以错误。

作者: 金鑫    时间: 2012-8-6 20:09
   Point(double a1,double b1,double c1)
        {         
                a = a1;         b = b1;         c = c1;        
        }
因为你在这里已经定义了一个有3个参数的构造方法,因此就不会再自动给你定义一个无参构造方法。
你在测试类里想通过Point p = new Point();来创建一个对象P,因为他是通过new point()这个无参构造方法来创建P对象的。所以他找不到Point()无参构造方法,自然就会在这里报错。
因为你在Point{}类里定义了一个Point(double a1,double b1,double c1){}构造方法,所以Point p1 = new Point(2.2,2.2,2.2)对象可以成功创建。

对象的构建要看是否存在构造方法:类名  对象名 = new 构造方法名();定义的有构造方法就能成功创建对象,没有就会失败。         
作者: 王舜民    时间: 2012-8-6 20:55
赵俊杰 发表于 2012-8-6 19:57
class Point
{        
        double a,b,c;

我报的是 实际参数列表和形式参数长度不同。的错误
构造了一个有参函数,那Point为什么还要想要弄无参的呢?只要里面加个值就行了啊,你就是加
(0,0,0)也行,只要不是空参数,null就可以




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2