其他分享
首页 > 其他分享> > 方法内部类

方法内部类

作者:互联网

在这里插入图片描述

package java_person;

//外部类
public class Person {
    int age;

    public Object getHeart(){
        // new Heart().temp = 12;
        // return new Heart();
        class Heart{
            int age = 13;
            int temp = 22;
            public void say(){
                System.out.println("hello");
            }

            public void eat(){

            }
            public String beat(){
                new Person().eat();
                return new Person().age + "岁的心脏在跳动";
            }
        }
        return new Heart().beat();
    }

    public void eat(){
        System.out.println("人会吃东西");
    }
}
package java_person;

public class PersonTest {
    public static void main(String[] args) {
        Person lili = new Person();
        lili.age = 12;

        System.out.println(lili.getHeart());
    }
}

输出:

人会吃东西
0岁的心脏在跳动

总结

标签:Heart,内部,void,class,Person,new,方法,public
来源: https://blog.csdn.net/Thumb_/article/details/120106325