/** * Created by xabcd on 2019/2/14. */ public class Person5 { private String name; private int age; private void talk() {System.out.println("我是:"+name+",今年:"+age+"岁");} public void say() {talk();} public void setName(String str) {name = str;} public void setAge(int a) { if (a>0) age = a; } public String getName() { return name; } public int getAge() { return age; } public static void main(String args[]) { //声明并实例化一个Person5对象p Person5 p = new Person5(); p.setName("张三"); p.setAge(30); p.say(); } } 结果: 我是:张三,今年:30岁