除了课程视频的办法,还有什么办法能调用Profession里的专业名称和学制年限并输出
package zongheanli; /** * 专业类 * * @author ThinkPad * */ public class Profession { private String ProfessionaLtitle;// 专业名称 private String ProfessionalNumber;// 专业编号 private int LengthOfSchooling;// 学制年限 // 无参构造方法 public Profession() { } // 带参构造,实现对属性的全部赋值 public Profession(String ProfessionaLtitle, String ProfessionalNumber, int LengthOfSchooling) { this.setProfessionaLtitle(ProfessionaLtitle); this.setProfessionalNumber(ProfessionalNumber); this.setLengthOfSchooling(LengthOfSchooling); } public String getProfessionaLtitle() { return ProfessionaLtitle; } public void setProfessionaLtitle(String professionaLtitle) { this.ProfessionaLtitle = professionaLtitle; } public String getProfessionalNumber() { return ProfessionalNumber; } public void setProfessionalNumber(String professionalNumber) { this.ProfessionalNumber = professionalNumber; } public int getLengthOfSchooling() { return LengthOfSchooling; } // 设置学制年限,限制必须>0 public void setLengthOfSchooling(int lengthOfSchooling) { if (lengthOfSchooling <= 0) return; this.LengthOfSchooling = lengthOfSchooling; } /** * 专业介绍的方法 * * @return 专业介绍的相关信息,包括名称、编号、年限 */ // public String kt() { // String str = "专业信息如下:\n专业名称:" + this.getProfessionaLtitle() + "\n专业编号:" + this.getProfessionalNumber() // + "\n学制年限:" + this.getLengthOfSchooling() + "年"; // return str; // } public void kt1() { System.out.println("专业信息如下:"); System.out.println("专业名称:" + this.getProfessionaLtitle()); System.out.println("专业编号:" + this.getProfessionalNumber()); System.out.println("学制年限:" + this.getLengthOfSchooling() + "年"); } }
package zongheanli; //import zongheanli.Profession; public class Student { private String name;// 姓名 private String studentID;// 学号 private String gender;// 性别 private int age;// 年龄 //private String zhuanye;// 所报专业名称 //private int years;// 学制年限 // 无参构造方法 public Student() { } public Student(String name, String studentID, String gender, int age) { //String zhuanye, int years) { this.setName(name); this.setStudentID(studentID); this.setGender(gender); this.setAge(age); //this.setZhuanye(zhuanye); //this.setYears(years); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudentID() { return studentID; } public void setStudentID(String studentID) { this.studentID = studentID; } public String getGender() { return gender; } /** * 限制性别只能是“男”或者“女”,反之,强制赋值为“男” * @param gender 传入的性别 */ public void setGender(String gender) { if(gender!="男"&&gender!="女") this.gender="男"; else this.gender = gender; } public int getAge() { return age; } /** * 给年龄赋值,限定必须在10-100之间,反之赋值为18 * @param age 传入的年龄 */ public void setAge(int age) { if(age<10||age>100) this.age=18; else this.age = age; } // public String getZhuanye() { // return zhuanye; // } // public void setZhuanye(Profession kt) { // this.zhuanye = kt; // } // public int getYears() { // return years; // } // public void setYears(int years) { // if(years <= 0) // return; // this.years = years; // } public void xinxi() { System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getStudentID()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); //System.out.println("所报专业名称:" + this.getZhuanye()); //System.out.println("学制年限:" + this.getYears()); } }
package zongheanli; import zongheanli.Student; import zongheanli.Profession; public class StuentTest { public static void main(String[] args) { Profession kt=new Profession("计算机科学与应用","J0001",4); //System.out.println(kt.kt()); kt.kt1(); System.out.println("==================================="); Student one =new Student("张三","S01","男",18); one.xinxi(); System.out.println("==================================="); Student two =new Student("李四","S02","女",17); two.xinxi(); System.out.println("==================================="); Student three =new Student("王五","S03","男",18); three.xinxi(); } }
我原来使直接在Student里面直接增加的属性。
private String name;// 姓名 private String studentID;// 学号 private String gender;// 性别 private int age;// 年龄 //private String zhuanye;// 所报专业名称 //private int years;// 学制年限 public void xinxi() { System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getStudentID()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); //System.out.println("所报专业名称:" + this.getZhuanye()); //System.out.println("学制年限:" + this.getYears()); }
4
收起
正在回答
1回答
同学你好,这里想问的只是针对课程中" 通过方法实现学生与专业关联--方案1"的获取学科的专业名称和学制年限并输出,还有其他的方法吗? 如果是,还有如下的方法来实现;
获取Profession的专业名称和学制年限,可以通过将这个Profession类作为参数传入xinxi方法中来获取专业明和和学制年限,例如:
public void xinxi(Profession myProfession) { System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getStudentID()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); System.out.println("所报专业名称:" + myProfession.getProfessionaLtitle()); System.out.println("学制年限:" + myProfession.getLengthOfSchooling()); }
也可以将Profession作为类属性,在造方法中直接赋值,然后在xinxi方法中直接通过this来获取专业名称和学制年限哦!
public class Student { private String name;// 姓名 private String studentID;// 学号 private String gender;// 性别 private int age;// 年龄 private Profession myProfession; /*添加对应的无参和有参的构造方法、get/set方法*/ }
//具体信息方法 public void xinxi() { System.out.println("姓名:" + this.getName()); System.out.println("学号:" + this.getStudentID()); System.out.println("性别:" + this.getGender()); System.out.println("年龄:" + this.getAge()); System.out.println("所报专业名称:" + this.getMyProfession().getProfessionaLtitle()); System.out.println("学制年限:" + this.getMyProfession().getLengthOfSchooling()); }
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星