Java getter和setter的方法及内部类的调用

class Test{
    public static void main(String[]args){
        Person person=new Person();
        person.age=22;
        person.setName("何浩源");
        person.setSalary(390000);

        System.out.println("姓名为:"+person.getName());
        System.out.println("年龄为:"+person.age);
        System.out.println("年薪为:"+person.getSalary());
    }
}
class Person{
    int age;
    String name;
    int salary;

    public int getAge() {
        return age;
    }
    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

}

运行结果:

 

内部类的调用:

    class Father {

        private String name = "zhangjun";


        class Child {

            public String intoFather() {

                return Father.this.name;
            }

        }



    }
public class Test{
    public static void main(String[] args) {
        Father.Child per = new Father().new Child();

        System.out.println(per.intoFather());
    }
}

 

运行结果:

 

posted @ 2019-03-20 18:19  何浩源  阅读(3965)  评论(0编辑  收藏  举报
//一下两个链接最好自己保存下来,再上传到自己的博客园的“文件”选项中