Person(人类的声明):
publicclass Person {
private Stringusername;
public Stringsex;
privateint age;
//不带参数的构造方法
public Person(){
System.out.println("=======不带参数的构造方法");
}
//自定义带参数的构造方法
public Person(String username ,String sex ,int age ){
this.username=username;
this.sex=sex;
this.age=age;
}
//自定义seter和geter方法
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
public String getSex() {
returnsex;
}
publicvoid setSex(String sex) {
this.sex = sex;
}
}
***测试调取***
publicclass test1 {
publicstatic void main(String[] args) {
Person p=new Person("张飞","男",11);
p.sex="男";
p.setSex("女");
System.out.println("这个人的信息是:"+p.getUsername()+p.getSex()+p.getAge());
}
}
*** 打印结果 ***
这个人的信息是:张飞女11