Java实训学习笔记09---接口编程
Java 接口
接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。
接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。
除非实现接口的类是抽象类,否则该类要定义接口中的所有方法。
接口无法被实例化,但是可以被实现。一个实现接口的类,必须实现接口内所描述的所有方法,否则就必须声明为抽象类。另外,在 Java 中,接口类型可用来声明一个变量,他们可以成为一个空指针,或是被绑定在一个以此接口实现的对象。
接口的声明:
接口的声明语法格式如下:
[可见度] interface 接口名称 [extends 其他的接口名] {
// 声明变量
// 抽象方法
}
重写接口中声明的方法时,需要注意以下规则:
-
类在实现接口的方法时,不能抛出强制性异常,只能在接口中,或者继承接口的抽象类中抛出该强制性异常。
-
类在重写方法时要保持一致的方法名,并且应该保持相同或者相兼容的返回值类型。
-
如果实现接口的类是抽象类,那么就没必要实现该接口的方法。
编程时针对于接口编程。
例子
接上一个实验,新建.impl包把原dao下的所有文件移动到impl下,重命名SubjectDao、UserDao为SubjectDaoImpl、UserDaoImpl,并在dao下新建两个接口SubjectDao、UserDao
UserDao接口
package com.chengxian.subject05.dao;
import java.sql.SQLException;
public interface UserDao {
/**
* 根据学号和密码查询学生账号是否正确
* @param stuNo
* @param pwd
* @return
* @throws SQLException
*/
public boolean queryByUnameAndPwd(String stuNo, String pwd) throws SQLException;
}
SubjectDao接口
package com.chengxian.subject05.dao;
import java.sql.SQLException;
import java.util.List;
import com.chengxian.subject05.pojo.Subject;
public interface SubjectDao {
/**
* 查询全部课题
* @return
* @throws SQLException
*/
public List<Subject> queryAllSubject() throws SQLException;
/**
* 添加课题
* @param subject
* @return
* @throws SQLException
*/
public boolean addSubject(Subject subject) throws SQLException;
/**
* 根据id删除课题
* @param id
* @return
* @throws SQLException
*/
public boolean delSubject(int id) throws SQLException;
/**
* 根据id修改课题名
* @param title
* @param id
* @return
* @throws SQLException
*/
public boolean updateTitleById(String title, int id) throws SQLException;
/**
* 查询未选题的数量
* @return
* @throws SQLException
*/
public int unSelectedNumber() throws SQLException;
/**
* 判断该同学是否已经选题
* @param stuNo
* @param stuName
* @return
* @throws SQLException
*/
public boolean isSelected(String stuNo, String stuName) throws SQLException;
/**
* 随机id是否可用
* @param number
* @return
* @throws SQLException
*/
public List isUser(int number) throws SQLException;
}
具体实现:
当类实现接口的时候,类要实现接口中所有的方法。否则,类必须声明为抽象的类。
类使用implements关键字实现接口。在类声明中,Implements关键字放在class声明后面。
实现一个接口的语法,可以使用这个公式:
...implements 接口名称[, 其他接口名称, 其他接口名称..., ...] ...
在实现接口的时候,也要注意一些规则:
- 一个类可以同时实现多个接口。
- 一个类只能继承一个类,但是能实现多个接口。
- 一个接口能继承另一个接口,这和类之间的继承比较相似。
实现SubjectDao接口—SubjectDaoImpl
package com.chengxian.subject05.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chengxian.subject05.dao.SubjectDao;
import com.chengxian.subject05.pojo.Subject;
import com.chengxian.subject05.util.JdbcTemplate;
import com.chengxian.subject05.util.JdbcUtil;
/**
* 专门操作subject表的类--数据操作层
* @author Administrator
*
*/
public class SubjectDaoImpl implements SubjectDao{
private Connection connection = JdbcUtil.getConnection();
/**
* 查询全部
* @return
* @throws SQLException
*/
public List<Subject> queryAllSubject() throws SQLException{
String sql = "select * from t_subject";
List subjectList = JdbcTemplate.executeQuery(sql, new SubjectMapper());
return subjectList;
}
/**
* 插入数据
* @param subject
* @return
* @throws SQLException
*/
public boolean addSubject(Subject subject) throws SQLException{
String sql = "insert into t_subject(title, stuNo, stuName, state)" +
"values(?, ?, ?, ?)";
int count = JdbcTemplate.executeUpdate(sql, subject.getTitle()
, subject.getStuNo()
, subject.getStuName(), subject.getState());
return count>0;
}
/**
* 删除数据
* @param id
* @return
* @throws SQLException
*/
public boolean delSubject(int id) throws SQLException{
String sql = "delete from t_subject where id = ?";
int count = JdbcTemplate.executeUpdate(sql, id);
return count>0;
}
/**
* 修改数据
* @param title
* @param id
* @return
* @throws SQLException
*/
public boolean updateTitleById(String title, int id) throws SQLException{
String sql = "update t_subject set title = ? where id = ?";
int count = JdbcTemplate.executeUpdate(sql, title, id);
return count>0;
}
/**
* 未被选择的题目还有多少条
* @return
*/
public int unSelectedNumber() throws SQLException{
int count = 0;//默认是0条
String sql = "select count(*) as cnt from t_subject where state = '0'";
List dataList = JdbcTemplate.executeQuery(sql, new SubjectCntMapper());
if(null != dataList && dataList.size()>0){
count = (Integer) dataList.get(0);
}
return count;
}
/**
* 判断该学生是否已经选择过毕设题目
* @param stuNo
* @param stuName
* @return
* @throws SQLException
*/
public boolean isSelected(String stuNo, String stuName) throws SQLException{
String sql = "select * from t_subject where stuNo = ? and stuName = ?";
List dataList = JdbcTemplate.executeQuery(sql, new SubjectMapper()
, stuNo, stuName);
int count = 0;
if(null != dataList){
count = dataList.size();
}
return count>0;
}
/**
* 随机的编号id是否可用
* @param number
* @return
* @throws SQLException
*/
public List isUser(int number) throws SQLException{
String sql = "select * from t_subject where id = ?";
List dataList = JdbcTemplate.executeQuery(sql, new SubjectMapper(), number);
return dataList;
}
}
实现UserDao接口—UserDaoImpl
package com.chengxian.subject05.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chengxian.subject05.dao.UserDao;
import com.chengxian.subject05.pojo.User;
import com.chengxian.subject05.util.JdbcTemplate;
import com.chengxian.subject05.util.JdbcUtil;
/**
* 专门操作user表的类
* @author Administrator
*
*/
public class UserDaoImpl implements UserDao{
/**
* 登录校验查询
* @param username
* @param pwd
* @return
* @throws SQLException
*/
public boolean queryByUnameAndPwd(String stuNo, String pwd) throws SQLException{
boolean flag = false;
//?表示占位符
String sql = "select * from t_user where stuNo = ? and pwd = ?";
List dataList = JdbcTemplate.executeQuery(sql, new UserMapper(), stuNo, pwd);
if(null != dataList && dataList.size()>0){
flag = true;
}
return flag;
}
}
需求:重构service,改为针对于接口编程
UserService接口
package com.chengxian.subject05.service;
import com.chengxian.subject05.exception.SubjectException;
public interface UserService {
/**
* 登录
* @param stuNo
* @param pwd
* @return
* @throws SubjectException
*/
public boolean login(String stuNo, String pwd) throws SubjectException;
}
SubjectService接口
package com.chengxian.subject05.service;
import java.util.List;
import com.chengxian.subject05.exception.SubjectException;
import com.chengxian.subject05.pojo.Subject;
public interface SubjectService {
/**
* 添加毕设课题
* @param subject
* @return
* @throws SubjectException
*/
public boolean addSubject(Subject subject) throws SubjectException;
/**
* 查询所有的课题信息
* @return
* @throws SubjectException
*/
public List<Subject> querySubjects() throws SubjectException;
/**
* 根据编号id修改课题名
* @param title
* @param id
* @return
* @throws SubjectException
*/
public boolean updateTitleById(String title, int id) throws SubjectException;
/**
* 根据id删除对应的课题
* @param id
* @return
* @throws SubjectException
*/
public boolean delSubjectById(int id) throws SubjectException;
/**
* 查询未被选择的数量
* @return
* @throws SubjectException
*/
public int unSelectedNumber() throws SubjectException;
/**
* 判断该学生是否已经选题
* @param stuNo
* @param stuName
* @return
* @throws SubjectException
*/
public boolean isSelected(String stuNo, String stuName) throws SubjectException;
/**
* 判断该随机id是否可用
* @param number
* @return
* @throws SubjectException
*/
public boolean isUser(int number) throws SubjectException;
}
具体实现
实现SubjectService接口—SubjectServiceImpl
package com.chengxian.subject05.service.impl;
import java.sql.SQLException;
import java.util.List;
import com.chengxian.subject05.dao.impl.SubjectDaoImpl;
import com.chengxian.subject05.exception.SubjectException;
import com.chengxian.subject05.factory.ObjectFactory;
import com.chengxian.subject05.pojo.Subject;
import com.chengxian.subject05.service.SubjectService;
import com.chengxian.subject05.transcation.Transcation;
/**
* 业务层
* @author Administrator
*
*/
public class SubjectServiceImpl implements SubjectService{
//private SubjectDao subjectDao = new SubjectDao();
private SubjectDaoImpl subjectDao=(SubjectDaoImpl)ObjectFactory.getObject("subjectDao");
private Transcation tx=(Transcation) ObjectFactory.getObject("tx");
/**
* 添加毕设信息
* @param subject
* @return
* @throws SubjectException
*/
public boolean addSubject(Subject subject) throws SubjectException{
boolean flag=false;
try {
tx.begin();//开启事务
flag= subjectDao.addSubject(subject);
tx.commit();//提交事务
} catch (SQLException e) {
try {
tx.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new SubjectException("数据库回滚异常");
}//回滚事务
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return flag;
}
/**
* 查询所有毕设信息
* @return
* @throws SubjectException
*/
public List<Subject> querySubjects() throws SubjectException{
List<Subject> subjectList=null;
try {
subjectList= subjectDao.queryAllSubject();
} catch (SQLException e) {
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return subjectList;
}
/**
* 根据编号id修改毕设题目信息
* @return
* @throws SubjectException
*/
public boolean updateTitleById(String title, int id) throws SubjectException{
boolean flag=false;
try {
tx.begin();
flag= subjectDao.updateTitleById(title, id);
tx.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
tx.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new SubjectException("数据库回滚异常");
}
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return flag;
}
/**
* 根据编号id删除对应的毕设信息
* @param id
* @return
* @throws SubjectException
*/
public boolean delSubjectById(int id) throws SubjectException{
boolean flag=false;
try {
tx.begin();
flag= subjectDao.delSubject(id);
tx.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
tx.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new SubjectException("数据库回滚异常");
}
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return flag;
}
/**
* 未被选择的条数
* @return
* @throws SubjectException
*/
public int unSelectedNumber() throws SubjectException{
int count=0;
try {
count= subjectDao.unSelectedNumber();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return count;
}
/**
* 判断该学生是否已经选择过毕设题目
* @param stuNo
* @param stuName
* @return
* @throws SubjectException
*/
public boolean isSelected(String stuNo, String stuName) throws SubjectException{
boolean flag=false;
try {
flag= subjectDao.isSelected(stuNo, stuName);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return flag;
}
/**
* 判断随机编号是否可用
* @param number
* @return
* @throws SubjectException
*/
public boolean isUser(int number) throws SubjectException{
boolean flag = false;
List dataList;
try {
dataList = subjectDao.isUser(number);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new SubjectException("数据库异常");
}
if(null != dataList && dataList.size()>0){
Subject subject = (Subject) dataList.get(0);
flag = subject.getState().equals("0");
}
return flag;
}
}
实现UserService接口—UserServiceImpl
package com.chengxian.subject05.service.impl;
import java.sql.SQLException;
import com.chengxian.subject05.dao.impl.UserDaoImpl;
import com.chengxian.subject05.exception.SubjectException;
import com.chengxian.subject05.factory.ObjectFactory;
import com.chengxian.subject05.service.UserService;
public class UserServiceImpl implements UserService{
//private UserDao userDao = new UserDao();
private UserDaoImpl userDao=(UserDaoImpl)ObjectFactory.getObject("userDao");
/**
* 登录
* @param stuNo
* @param pwd
* @return
* @throws SubjectException
*/
public boolean login(String stuNo, String pwd) throws SubjectException{
boolean flag=false;
try {
flag= userDao.queryByUnameAndPwd(stuNo, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new SubjectException("数据库异常");
}
return flag;
}
}
继续需求:重构事务Transcation
事务接口:
package com.chengxian.subject05.transcation;
import java.sql.SQLException;
public interface Transcation {
/**
* 开启事务
* @throws SQLException
*/
void begin() throws SQLException;
/**
* 提交事务
* @throws SQLException
*/
void commit() throws SQLException;
/**
* 回滚事务
* @throws SQLException
*/
void rollback() throws SQLException;
}
事务接口的具体实现类:
package com.chengxian.subject05.transcation.impl;
import java.sql.Connection;
import java.sql.SQLException;
import com.chengxian.subject05.transcation.Transcation;
import com.chengxian.subject05.util.JdbcUtil;
/**
* 事务
* @author 74670
*
*/
public class TranscationImpl implements Transcation{
/**
* 开启事务
* @throws SQLException
*/
public void begin() throws SQLException{
Connection connection=JdbcUtil.getConnection();
connection.setAutoCommit(false);
}
/**
* 提交事务
* @throws SQLException
*/
public void commit() throws SQLException{
Connection connection=JdbcUtil.getConnection();
connection.commit();
}
/**
* 回滚事务
* @throws SQLException
*/
public void rollback() throws SQLException{
Connection connection=JdbcUtil.getConnection();
connection.rollback();
}
}