1)定义一个student类(int id,String name,int age,String sex) 2)定义StudentDao接口和实现类,将student对象注入StudentDao 3)定义StudentService接口和实现类,将StudentDao实现类注入StudentService 4)定义StudentController接口和实现类,将StudentService实现类注入StudentController 5)调用StudentController打印方法打印
时间: 2023-06-08 15:09:08 浏览: 136
1) 好的,以下是定义一个student类的代码:
public class Student {
private int id;
private String name;
private int age;
private String sex;
public Student(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2) 接下来是StudentDao接口和实现类的代码:
public interface StudentDao {
public void addStudent(Student student);
}
public class StudentDaoImpl implements StudentDao {
private Student student;
public void addStudent(Student student) {
this.student = student;
}
}
3) 接下来是StudentService接口和实现类的代码:
public interface StudentService {
public void addStudent(Student student);
}
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void addStudent(Student student) {
studentDao.addStudent(student);
}
}
4) 最后是StudentController接口和实现类的代码:
public interface StudentController {
public void addStudent(Student student);
}
public class StudentControllerImpl implements StudentController {
private StudentService studentService;
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public void addStudent(Student student) {
studentService.addStudent(student);
}
}
5) 调用StudentController打印方法的代码:
public class Main {
public static void main(String[] args) {
Student student = new Student(1, "张三", 18, "男");
StudentDao studentDao = new StudentDaoImpl();
studentDao.addStudent(student);
StudentService studentService = new StudentServiceImpl();
studentService.setStudentDao(studentDao);
studentService.addStudent(student);
StudentController studentController = new StudentControllerImpl();
studentController.setStudentService(studentService);
studentController.addStudent(student);
System.out.println("打印完毕");
}
}
请问您是否已经理解了我的回答呢?
阅读全文