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岁的心脏在跳动