class Worker
{
//姓名
private String name;
//年龄
private int age;
//性别
private char sex;
//成员方法
public void show()
{
System.out.println("name:"+name+"\t"+"age:"+age+"\t"+"sex:"+sex);
}
//公有的getXxx()和setXxx()方法
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setAge(int age)
{
//对传入的参数进行检查
if(age<=0||age>200)
{
System.out.println("您输入的年龄"+age+"不合法");
}
else
{
this.age=age;
}
}
public int getAge()
{
return age;
}
public void setSex(char sex)
{
this.sex=sex;
}
public char getSex()
{
return sex;
}
}
class WorkerTest
{
public static void main(String[] args)
{
//创建对象
Worker w=new Worker();
//给成员变量赋值
w.setName("李亚妮");
w.setAge(-20);
w.setSex('女');
//调用方法
w.show();
}
}
|
|