ITPub博客

首页 > IT职业 > > Java 内部类Student如何实现访问外部类People的Message方法

Java 内部类Student如何实现访问外部类People的Message方法

作者:yaoran 时间:2019-09-09 11:21:06 0 删除 编辑

请问:内部类Student如何实现访问外部类People的Message方法?

代码如下:

//外部类People

public class People {

private String name = "LiLei"; //外部类的私有属性

//外部类Message()方法
public void Message(){
    System.out.print("内部类如何实现访问外部类的方法");
}

//内部类Student
public class Student {
    String ID = "20151234";               //内部类的成员属性
    //内部类的方法
    public void stuInfo(){
        System.out.println("访问外部类中的name:" + name);
        System.out.println("访问内部类中的ID:" + ID);
        System.out.println("访问外部类中的方法"+new People.Message());//这里想访问外部类的Message方法为什么实现不了?
    }
}

//测试成员内部类
public static void main(String[] args) {
    People a = new People();     //创建外部类对象,对象名为a
    Student b = a.new Student(); //使用外部类对象创建内部类对象,对象名为b
    // 或者为 People.Student b = a.new Student();
    b.stuInfo();   //调用内部对象的suInfo方法
}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1721/viewspace-3150373/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    112
  • 访问量
    594