赞
踩
package com.hhit.day07.demo01; /* * 三个老师信息: * 教师编号 姓名 性别 年龄 科目 * t001 薛之谦 男 26 Java * t002 张碧晨 女 24 IOS * t003 张杰 男 28 Java */ public class Teacher { //教师信息 private String teacherId; private String teacherName; private String sex; private int age; private String subject; //set,get方法 public String getTeacherId() { return teacherId; } public void setTeacherId(String teacherId) { this.teacherId = teacherId; } public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } //无参构造方法 public Teacher() {} //全参构造方法 public Teacher(String teacherId,String teacherName,String sex,int age,String subject) { this.teacherId = teacherId; this.teacherName = teacherName; this.sex = sex; this.age = age; this.subject = subject; } }
package com.hhit.day07.demo01; import java.sql.Time; /* * 存储两个科目信息: * 课程编号 名称 创建时间 课程描述 * s001 Java 2007-02-08 Java学科,包含JavaSE和JavaEE * s002 IOS 2007-02-09 IOS系统开发 */ public class Course { //科目信息 private String courseId; private String courseName; private String startTime; private String description; //生成set,get方法 public String getCourseId() { return courseId; } public void setCourseId(String courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } public String getStartTime() { return startTime; } public void setStartTime(String startTime) { this.startTime = startTime; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } //无参构造方法 public Course() {} //全参构造方法 public Course(String courseId,String courseName,String startTime,String description) { this.courseId = courseId; this.courseName = courseName; this.startTime = startTime; this.description = description; } }
package com.hhit.day07.demo01; /* * 测试类 * 创建相应对象,通过构造方法给成员变量赋值 */ public class TestDemo01 { public static void main(String[] args) { //创建对象,通过构造方法给成员变量赋值 Teacher t1 = new Teacher("t001","薛之谦","男",26,"Java"); Teacher t2 = new Teacher("t002","张碧晨","女",24,"IOS"); Teacher t3 = new Teacher("t003","张杰","男",28,"Java"); //打印每个对象的所有属性 System.out.println("教师编号:"+t1.getTeacherId()+",姓名:"+t1.getTeacherName()+","+t1.getSex()+",科目:"+t1.getSubject()); System.out.println("教师编号:"+t2.getTeacherId()+",姓名:"+t2.getTeacherName()+","+t2.getSex()+",科目:"+t2.getSubject()); System.out.println("教师编号:"+t2.getTeacherId()+",姓名:"+t3.getTeacherName()+","+t3.getSex()+",科目:"+t3.getSubject()); Course c1 = new Course("s001","Java","2007-02-08","Java学科,包含JavaSE和JavaEE"); Course c2 = new Course("s002","IOS","2007-02-09","IOS系统开发"); System.out.println("课程编号:"+c1.getCourseId()+",名称:"+c1.getCourseName()+",创建时间:"+c1.getStartTime()+",课程描述:"+c1.getDescription()); System.out.println("课程编号:"+c2.getCourseId()+",名称:"+c2.getCourseName()+",创建时间:"+c2.getStartTime()+",课程描述:"+c2.getDescription()); } }
package com.hhit.day07.demo03; /* * 项目经理类 * 属性: * 姓名name * 工号id * 工资salary * 奖金bonus * 行为: * 工作work() */ public class Manager { //属性 private String managerName; private String managerId; private int salary; private int bonus; //set,get方法 public String getManagerName() { return managerName; } public void setManagerName(String managerName) { this.managerName = managerName; } public String getManagerId() { return managerId; } public void setManagerId(String managerId) { this.managerId = managerId; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } //无参构造 public Manager() {} //带参构造 public Manager(String managerName,String managerId,int salary,int bonus) { this.managerName = managerName; this.managerId = managerId; this.salary = salary; this.bonus = bonus; } //行为 public void work() { System.out.println("工号为"+managerId+"基本工资为"+salary+"奖金为"+bonus+"的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码..."); } }
package com.hhit.day07.demo03; /* * 程序员类Coder * 属性: * 姓名name * 工号id * 工资salary * 行为: * 工作work() */ public class Coder { //属性 private String coderName; private String coderId; private int coderSalary; //set,get方法 public String getCoderName() { return coderName; } public void setCoderName(String coderName) { this.coderName = coderName; } public String getCoderId() { return coderId; } public void setCoderId(String coderId) { this.coderId = coderId; } public int getCoderSalary() { return coderSalary; } public void setCodersalary(int coderSalary) { this.coderSalary = coderSalary; } //无参构造 public Coder() {} //带参构造 public Coder(String coderName,String coderId,int coderSalary) { this.coderName = coderName; this.coderId = coderId; this.coderSalary = coderSalary; } //行为 //工作work() public void work() { System.out.println("工号为"+coderId+"基本工资为"+coderSalary+"的程序员正在努力的写着代码"); } }
package com.hhit.day07.demo03; /* * 测试类 * 在main方法中创建该类的对象并给属性赋值(演示两种方法:setter方法和构造方法) * 调用成员方法,打印格式如下 */ public class TestDemo03 { public static void main(String[] args) { //创建Manager对象并给属性赋值 //方式1 /*Manager manager = new Manager(); manager.setManagerName("张三"); manager.setManagerId("01"); manager.setSalary(15000); manager.setBonus(6000); */ //方式2 Manager manager = new Manager("张三","01",15000,6000); //创建Coder对象并给属性赋值,使用方式2 Coder coder = new Coder("李四","001",10000); //调用成员方法,打印 manager.work(); coder.work(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。