class Demo_Person_This {
public static void main(String[] args) {
Person p1=new Person(); // 创建对象p1
p1.gender="man";
p1.setName("马里奥");
p1.setAge(87);
System.out.println(p1.getName()+",,,,,"+p1.getAge());
Person p2=new Person(); //创建对象p2
p2.gender="woman";
p2.setName("莉莉");
p2.setAge(35);
System.out.println(p2.getName()+",,,,"+p2.getAge());
p2.Speak();
p1.Speak();
}
}
class Person { //创建一个类
private String name; //名字私有
String gender;
private int age; //年龄私有
public void setAge(int age){ //设置年龄
if(age>0&&age<200){
this.age=age;
}
else{
System.out.println("请您回去火星");
}
}
public int getAge(){ //获取年龄
return age;
}
public void setName(String name) { //设置年龄n =msliao;
this.name =name;
}
public String getName(){ //获取年龄
return name;
}
public void Speak(){
System.out.println("我来自火星");
}
}
|
|