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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

声明类Student,包含3个成员变量:name、age、score,要求可以通过 new Student("张三", 22, 95) 的方式创建对象,
*          并可以通过set和get方法访问成员变量

  1. public class Test5 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                
  7.                 /*因为主函数静态,所以无法直接调用内部类Student,除了将Student类静态之外
  8.                  *还可以通过外部类对象去创建内部类对象
  9.                  *Test5.Student stu = new Test5().new Student("张三",22,95);
  10.                  */       
  11.                 Student stu = new Student("张三",22,95);
  12.                 System.out.println(stu.getName() + "," + stu.getAge() + "," + stu.getScore());
  13.         }
  14.        
  15.         //因为主函数静态,所以无法直接调用内部类Student,所以要将Student类静态后才能调用。
  16.         public static class Student {
  17.                 private String name;
  18.                 private int age;
  19.                 private int score;
  20.                 // 定义带参数的构造函数
  21.                 public Student(String name,int age,int score){
  22.                         this.name = name;
  23.                         this.age = age;
  24.                         this.score = score;
  25.                 }
  26.                 //定义get方法
  27.                 public String getName() {
  28.                         return name;
  29.                 }
  30.                 //定义set方法
  31.                 public void setName(String name) {
  32.                         this.name = name;
  33.                 }

  34.                 public int getAge() {
  35.                         return age;
  36.                 }

  37.                 public void setAge(int age) {
  38.                         this.age = age;
  39.                 }

  40.                 public int getScore() {
  41.                         return score;
  42.                 }

  43.                 public void setScore(int score) {
  44.                         this.score = score;
  45.                 }
  46.                
  47.         }
  48.        
  49. }
复制代码




5 个回复

正序浏览
用外部类的方法不行...打包问题 如何解决
回复 使用道具 举报
- - 你是在学内部类?要不然我真的无法理解为什么要在主函数的类中创建一个这样的类
回复 使用道具 举报
飞飞飞丨 发表于 2015-8-16 21:04
只会鼠标右击一键生成

这是入学基础测试,入学了当然就继续一键吧
回复 使用道具 举报
只会鼠标右击一键生成
回复 使用道具 举报
单独提取出来一个 Student 类不好么
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马