封装1-1练习
Student.java
package com.imooc.case1; /** * 学生类 * @author HT.Lin * @version 1.0 2017-12-21 */ public class Student { //私有成员属性: 姓名, 学号, 性别, 年龄, 专业 private String name; private String id; private String sex; private int age; private Speciatly sp; //构造函数 public Student(){} public Student(String name, String id, String sex, int age, Speciatly sp){ setName(name); setId(id); setSex(sex); setAge(age); this.sp = sp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { if (age <0){ System.out.println("年龄不能为负数!"); } else { this.age = age; } } // 信息显示方法 public void show(){ System.out.println("学生信息如下:"); System.out.println("姓名: " + this.getName()); System.out.println("学号: " + this.getId()); System.out.println("性别: " + this.getSex()); System.out.println("年龄: " + this.getAge()); System.out.println("所报专业名称: " + this.sp.getMajorName()); System.out.println("学制年限: " + this.sp.getSchooling()); } }
Speciatly.java
package com.imooc.case1; /** * 专业类. * @author HT.Lin * @version 1.0 2017-12-21 */ public class Speciatly { //私有成员属性: 专业名称, 专业编号, 学制 private String majorName; private String majorId; private int schooling; //构造方法 public Speciatly(){} public Speciatly(String majorName, String majorId, int schooling){ setMajorName(majorName); setMajorId(majorId); setSchooling(schooling); } public String getMajorName() { return majorName; } public void setMajorName(String majorName) { this.majorName = majorName; } public String getMajorId() { return majorId; } public void setMajorId(String majorId) { this.majorId = majorId; } public int getSchooling() { return schooling; } public void setSchooling(int schooling) { if (schooling <0 || schooling >7) System.out.println("学制输入范围在1到7年之间"); else this.schooling = schooling; } //信息显示方法 public void show(){ System.out.println("专业信息如下:"); System.out.println("专业名称:" + getMajorName()); System.out.println(("专业编号:" + getMajorId())); System.out.println("学制年限:" + getSchooling()+"年"); } }
Test.java
package com.imooc.case1; public class Test { public static void main(String[] args) { Speciatly specialty = new Speciatly("计算机科学与应用","J0001",4); specialty.show(); System.out.println("========================================="); Student firstStudent = new Student("张三", "S01", "男", 18, specialty); Student secondStudent = new Student("李四", "S01", "女", 17, specialty); Student thirdStudent = new Student("王五", "S01", "男", 18, specialty); firstStudent.show(); System.out.println("========================================="); secondStudent.show(); System.out.println("========================================="); thirdStudent.show(); } }
这是我的在看案例分析之前做出来的, 但是代码没办法把三个学生信息并列输出. 只能一个接一个. 请老师点评一个.
29
收起
正在回答 回答被采纳积分+1
3回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星