本帖最后由 杨鹏鹏 于 2012-8-27 20:34 编辑
class person
{
private String name;
private int age;
private static String country = "CN";
//构造代码块
{
System.out.println("cry.......");
}
person(String name,int age,String gaga)// 随便加个参数。构造函数
{
没有把传来的参数赋值给对象中的属性,this.name=name;
gaga=gaga;//没报错
this.gaga=gaga;// 报错:gaga cannot be resolved or is not a field
name =name;
age = age;
System.out.println("name="+name+" age="+age);
cry();
}
public void setName(String name)
{
this.name = name;//this应用
}
public void getName()
{
System.out.println("name="+name+" age="+age);
}
public void cry()
{
System.out.println("cry.......");
}
}
public class persondemo
{
public static void main(String[] args)
{
person p = new person("may",20,"凑数的");
p.setName("jack");
p.getName();
}
}
|