66666666600000 2022-03-23 08:37 采纳率: 87.5%
浏览 383
已结题

在程序中定义一个School类。在School类中定义一个内部类Student,分别创建这两个类的对象,访问两个对象中的方法

class School
{
String name;public class Student
{
String name;
int age;
public Student(String schoolName,String studentName,int newAge){
//将schoolName赋值给School类的name属性
//将studentName赋值给Student类的name属性
//将newAge赋值给Student类的age属性
}
public void output(){
System.out.println(“学校:”+School.this.name);
System.out.println(“姓名:”+this.name);
System.out.println(“年龄:”+this.age);
}
}
public void output(){Student stu =new Student(“金融学院”,”张三”,24);stu.output();
}

public class Inner{public static void main(String[] args){
System.out.println(“--通过外部类成员访问内部类员--”);
School a=new School();
a.output();
System.out.println(“--直接访问内部类成员--”);
School.Student b=a.new Student(“金融学院”,“李”,23);
b.output();
}
}

  • 写回答

1条回答 默认 最新

  • zcrazy胡说八道 2022-03-23 09:57
    关注
    
    class School {
    
        String name;
    
        public class Student {
    
            String name;
    
            int age;
    
            public Student(String schoolName,String studentName,int newAge){
        //将schoolName赋值给School类的name属性
        //将studentName赋值给Student类的name属性
        //将newAge赋值给Student类的age属性
                School.this.name = schoolName;
                this.name = studentName;
                this.age = newAge;
            }
    
            public void output(){
                System.out.println("学校:"+School.this.name);
                System.out.println("姓名:"+this.name);
                System.out.println("年龄:"+this.age);
            }
        }
        public void output(){Student stu =new Student("金融学院","张三",24);
            stu.output();
        }
    
        public static class Inner{
            public static void main(String[] args){
                System.out.println("--通过外部类成员访问内部类员--");
                School a=new School();
                a.output();
                System.out.println("--直接访问内部类成员--");
                School.Student b=a.new Student("金融学院","李",23);
                b.output();
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月31日
  • 已采纳回答 3月23日
  • 创建了问题 3月23日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)