专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Eclipse

一个十分简单的入门程序

发布时间:2010-06-05 15:37:02 文章来源:www.iduyao.cn 采编人员:星星草
一个非常简单的入门程序
我刚接触JAVA,问一个弱智的问题。
Java code

package Chapter3;
/*
 * 定义类并创建类的对象
 */

class Student
{
    float height,weight;
    String name,sex,num;
    void Student(String x,String y,String z)
    {
        name=x;
        sex=y;
        num=z;
        System.out.println(name+sex+num);
    }
    float f(float x,float y)
    {
        float z;
        weight=x;
        height=y;
        z=weight+height;
        return z;
    }
}
public class exercise3_8 {
    public static void main(String[] args)
    {
        Student crystal=new Student();
        crystal.Student(crystal,female,11111);
        
    }

}




这个程序为什么是错的???

------解决方案--------------------
void Student(String x,String y,String z)
是构造函数应该这样写
public Student(String x,String y,String z)

Student crystal=new Student("crystal", "female", "11111");

------解决方案--------------------
void Student(String x,String y,String z)
{
name=x;
sex=y;
num=z;
System.out.println(name+sex+num);
}
构造函数是不能代返回类型的,如果加了返回类型就是一个自定义方法了,只不过方法名和类名一样了,写成public Student(String x,String y,String z)这样就行了
------解决方案--------------------
第一:构造方法没有任何的返回值。。。它和类名是一样。

第二:既然你已经自己定义了一个构造方法,就不会调用系统默认的不带参数的构造方法了。
因此Student crystal=new Student();要改成Student crystal=new Student("crystal", "female", "11111");
如果你没有定义构造方法,或者你在定义个没有参数的构造方法。就可以直接用Student crystal=new Student();
------解决方案--------------------
构造方法没有返回值的
------解决方案--------------------
一个类,比如User类,
Java code

public class User{
   
    private String name;

    public User(){}//无参数构造

    public User(String n){// 有参构造
        this.name = n;
    }
}

------解决方案--------------------
构造函数是不能代返回类型的,如果加了返回类型就是一个自定义方法了,只不过方法名和类名一样了,写成public Student(String x,String y,String z)这样就行了
------解决方案--------------------
public class exercise3_8 {
public static void main(String[] args)
{
Student crystal=new Student();
crystal.Student(crystal,female,11111);

}

}
 
void Student(String x,String y,String z)

参数不匹配

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: