帮忙看下这段代码哪里错了
class Student
{
string strname;
int intage;
boolean blsex;
}
void init(string name,int age,boolean sex)
{
strname=name;
intage=age;
blsex=sex;
}
string getname()
{return strname;}
int getage()
{return intage;}
boolean bolsex()
{return blsex;}
}
class Studentdemo
{
public static void main(String args[])
{
Student astudent=new Student();
astudent.init( "jay ",22,ture);
System.out.println( "学生姓名: "+astudent.getname());
System.out.println( "年龄: "+astudent.getage());
}
}
------解决方案--------------------string strname; string应为String
astudent.init( "jay ",22,ture);中使用了,
------解决方案--------------------错误太多了
照着下面的改
class Student
{
String strname;
int intage;
boolean blsex;
void init(String name,int age,boolean sex)
{
strname=name;
intage=age;
blsex=sex;
}
String getname()
{return strname;}
int getage()
{return intage;}
boolean bolsex()
{return blsex;}
}
class Studentdemo
{
public static void main(String args[])
{
Student astudent=new Student();
astudent.init( "jay ",22,true);
System.out.println( "学生姓名: "+astudent.getname());
System.out.println( "年龄: "+astudent.getage());
}
}
------解决方案--------------------class Student
{
String strname;
int intage;
boolean blsex;
void init(String name,int age,boolean sex)
{
strname=name;
intage=age;
blsex=sex;
}
String getname()
{return strname;}
int getage()
{return intage;}
boolean bolsex()
{return blsex;}
}
class Studentdemo
{
public static void main(String args[])
{
Student astudent=new Student();
astudent.init( "jay ",22,true);
System.out.println( "学生姓名: "+astudent.getname());
System.out.println( "年龄: "+astudent.getage());
}
}
------解决方案--------------------程序运行结果:
--------------
学生姓名:jay
年龄:22
------解决方案--------------------你的这段代码有个两处的错误:
1,代码的规范性,请写代码的时候, 方法尽量大写,人家一看方法名就知道你要干什么
2.要注意使用实例对象的关键字this;否则方法中的变量名与类中成员的变量名相同,
方法中的变量,成员变量自动隐藏,你打印不出来
例:
void init(String name,int age,boolean sex)
{
this.strname=name;