ackage onlyfun.caterpillar;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class SimpleDBSource implements DBSource {
private Properties props;
private String url;
private String user;
private String passwd;
public SimpleDBSource() throws IOException,
ClassNotFoundException {
this("jdbc.properties");
}
public SimpleDBSource(String configFile) throws IOException,
ClassNotFoundException {
props = new Properties();
props.load(new FileInputStream(configFile));
url = props.getProperty("onlyfun.caterpillar.url");
user = props.getProperty("onlyfun.caterpillar.user");
passwd = props.getProperty("onlyfun.caterpillar.password");
Class.forName(
props.getProperty("onlyfun.caterpillar.driver"));
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, passwd);
}
public void closeConnection(Connection conn) throws SQLException {
conn.close();
}
}
以上是一个例子中的代码,相信大家也看得懂,我就是不明白,类中的无参构造方法中的this("jdbc.properties");,是什么意思的,还有这种写法???还有它与下面的有参构造方法是什么联系的?求大家详解
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class SimpleDBSource implements DBSource {
private Properties props;
private String url;
private String user;
private String passwd;
public SimpleDBSource() throws IOException,
ClassNotFoundException {
this("jdbc.properties");
}
public SimpleDBSource(String configFile) throws IOException,
ClassNotFoundException {
props = new Properties();
props.load(new FileInputStream(configFile));
url = props.getProperty("onlyfun.caterpillar.url");
user = props.getProperty("onlyfun.caterpillar.user");
passwd = props.getProperty("onlyfun.caterpillar.password");
Class.forName(
props.getProperty("onlyfun.caterpillar.driver"));
}
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, passwd);
}
public void closeConnection(Connection conn) throws SQLException {
conn.close();
}
}
以上是一个例子中的代码,相信大家也看得懂,我就是不明白,类中的无参构造方法中的this("jdbc.properties");,是什么意思的,还有这种写法???还有它与下面的有参构造方法是什么联系的?求大家详解
无参构造器中的this("jdbc.properties");其实是调用的是自己的有参构造器SimpleDBSource(String configFile),jdbc.properties就相当于有参构造器中的configFile。
1、应用一:引用成员变量
2、应用二:调用类的构造方法
http://www.cnblogs.com/-cyb/articles/Java-this.html
this是学java基础的时候就应该知道的,你是初学吧。