创建一个人类
属性:姓名,年龄,性别,工作
方法:睡觉,工作,自我介绍
定义不同的方法内容,并调用
public class T4 {
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("xiaoming");
p1.setAge(20);
p1.setGender("男");
p1.setWork("programmer");
System.out.println("我叫:"+p1.getName()+",我的自我介绍:");
p1.intro("我是一个帅气的程序员");
}
}
class Person{
private String name;
private int age;
private String gender;
private String work;
public void sleep() {
System.out.println("人类睡觉");
}
public void working() {
System.out.println("人类工作");
}
public void intro(String info) {
System.out.println(info);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getWork() {
return work;
}
public void setWork(String work) {
this.work = work;
}
}
运行结果: