A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 遗忘 中级黑马   /  2013-10-27 23:25  /  2116 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 遗忘 于 2013-10-28 10:30 编辑
  1. import java.util.*;
  2. public class StudentsDemo {
  3.         
  4.         public static void main(String[] args){
  5.                  Students[] stu = new Students[10];        //创建学生类数组,用于接收输入学生身高
  6.                  Scanner input = new Scanner(System.in);
  7.                  for(int y =0;y < stu.length;y++)   //手动循环输入学生身高
  8.                         {
  9.                                 System.out.println("请输入第" + (y+1) + "名学生的身高:");
  10.                                 stu[y] = new Students();
  11.                                 stu[y].height = input.nextInt();
  12.                         }
  13.                  Students.Height h = new Students().Height();//为什么不能创建对象 这出错
  14.                  Students maxHgt = h.getMaxHeight(stu);
  15.                  System.out.println(maxHgt.height);
  16.          }         
  17. }


  18. class Students{
  19.         
  20.         int height;
  21.         class Height
  22.         {        
  23.                 public Students getMaxHeight(Students[] stu)//获取最高身高
  24.                 {
  25.                         Students maxHeight = new Students();
  26.                         for(int x =0;x <stu.length;x++)
  27.                         {        
  28.                                 if(stu[x].height> maxHeight.height)
  29.                                 {
  30.                                         maxHeight.height = stu[x].height;
  31.                                 }
  32.                         }
  33.                         return maxHeight ;
  34.                 }
  35.         }
  36. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

5 个回复

倒序浏览
本帖最后由 近夜之星 于 2013-10-28 00:33 编辑

内部类分为两种,一种是静态内部类,一种是非静态内部类。前者不用产生外部类的实例化对象即可产生内部类的实例化对象,后者必须先产生外部类的实例化对象,才能产生内部类的实例化对象。
实例化静态内部类对象的模板是: 外部类类名.内部类类名 xxx = new 外部类类名.内部类类名()
实例化非静态内部类对象的模板是:外部类类名.内部类类名 xxx = 外部类对象名.new 内部类类名()
  1. Students.Height h = new Students().new Height();
复制代码
第13行代码中,Hight前面少了个关键字new,填上后,编译、运行正常

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
我把你的代码修改了一下,请您参考一下:
  1. package cn.itcast.day1;

  2. import java.util.*;

  3. public class StudentsDemo2 {

  4.         public static void main(String[] args) {
  5.                 Student[] stu = new Student[10]; // 创建学生类数组,用于接收输入学生身高
  6.                 Scanner input = new Scanner(System.in);
  7.                 for (int y = 0; y < stu.length; y++) // 手动循环输入学生身高
  8.                 {
  9.                         //把println换成print跟好看一些
  10.                         System.out.print("请输入第" + (y + 1) + "名学生的身高:");
  11.                         stu[y] = new Student();
  12.                         stu[y].height = input.nextInt();
  13.                 }
  14.                
  15.                 Student maxHgt = StuUtils.getMaxHeight(stu);
  16.                 System.out.println("最高身高是:"+maxHgt.height);
  17.         }
  18. }

  19. //定义一个操作学生的工具类,我觉得把获取最大学生的身高的功能,设计成一个静态的方法更好,更符合面向对象的设计思想
  20. class StuUtils{
  21.        
  22.         //定义一个可以接收一个学生对象的数组,并获取身高最高的学生的静态方法
  23.         public static Student getMaxHeight(Student[] stu)
  24.         {
  25.                 Student maxHeight = new Student();
  26.                 for (int x = 0; x < stu.length; x++) {
  27.                         if (stu[x].height > maxHeight.height) {
  28.                                 maxHeight.height = stu[x].height;
  29.                         }
  30.                 }
  31.                 return maxHeight;
  32.         }
  33. }

  34. class Student {

  35.         int height;

  36. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨增坤 + 1

查看全部评分

回复 使用道具 举报
wenbaoxing 发表于 2013-10-28 08:35
我把你的代码修改了一下,请您参考一下:

谢谢,这个我之前也是和你一样的方式写 的,只是想看练习一下内部类的内容
回复 使用道具 举报
近夜之星 发表于 2013-10-28 00:31
内部类分为两种,一种是静态内部类,一种是非静态内部类。前者不用产生外部类的实例化对象即可产生内部类的 ...

哥们,谢了 !自己没弄清楚 哈哈,还得努力啊,加油~!
回复 使用道具 举报
遗忘 发表于 2013-10-28 10:29
哥们,谢了 !自己没弄清楚 哈哈,还得努力啊,加油~!

不用谢,一起加油:D
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马