java method of enhancing

Introduce two methods

inherit

By rewriting the need to strengthen the inherited method

public class Cat{
	public void effect() {
		System.out.println("可以驾驶");
	}
}
public class Truck extends Cat{
	@Override
	public void effect() {
		System.out.println("卡车可以拉货");
		super.effect();
	}
}
Decorator Pattern
public class MyConnection implements Connection {
	//3.定义一个变量
	private Connection conn;
	
	private LinkedList<Connection> pool;
	
	// 2.编写一个构造方法(参数使用了面相对象的多态特性)
	public MyConnection(Connection conn,LinkedList<Connection> pool) {
		this.conn=conn;
		this.pool=pool;
	}
	
	//4.书写需要增强的方法
	@Override
	public void close() throws SQLException {
		pool.add(conn);
	}

	/**
	 * 此方法必须覆盖!否则会出现空指针异常!!!
	 */
	@Override
	public PreparedStatement prepareStatement(String sql) throws SQLException {
		return conn.prepareStatement(sql);
	}
	......
Published 24 original articles · won praise 5 · Views 3950

Guess you like

Origin blog.csdn.net/qq_41345281/article/details/89073243