java 在类的内部调用方法

2019年2月14日23:41:55 发表评论 263 views
/**
 * 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岁

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: