parent
bf0303d343
commit
cfea1c3378
@ -0,0 +1,44 @@
|
||||
张三 18
|
||||
package case1;
|
||||
|
||||
public class TestPersonDemo {
|
||||
public static void main(String[] args) {
|
||||
/********* begin *********/
|
||||
// 声明并实例化一Person对象p
|
||||
Person p1=new Person();
|
||||
// 给p中的属性赋值
|
||||
String name=p1.getName();
|
||||
name="张三";
|
||||
p1.setName(name);
|
||||
int age=p1.getAge();
|
||||
age=18;
|
||||
p1.setAge(age);
|
||||
// 调用Person类中的talk()方法
|
||||
p1.talk();
|
||||
/********* end *********/
|
||||
}
|
||||
}
|
||||
// 在这里定义Person类
|
||||
class Person {
|
||||
/********* begin *********/
|
||||
private String name;
|
||||
private int age;
|
||||
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 void talk(){
|
||||
System.out.println("我是:"+name+",今年:"+age+"岁");
|
||||
}
|
||||
/********* end *********/
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue