一个非常简单的入门程序
我刚接触JAVA,问一个弱智的问题。
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); }}
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)
参数不匹配