1 接口java
類的接口 (interface) 就是該類容許其餘類對象訪問的方法和字段的集合。接口做爲對象必須實現的承諾。接口永遠不可能被實例化爲對象,所以只能定義虛方法和常量字段。數組
做用:ui
限制了對象之間的交互(交互能夠只用interface來完成,interface進行限制)this
與抽象類的區別:spa
一個類能夠實現(implements)任意多個接口,但只能繼承(extend)一個抽象類。rest
一個抽象類可有非抽象方法,能夠定義構造器,接口的全部方法都是抽象的。component
接口只能聲明static final 常量,由於通常成員變量沒法實例化。對象
總之,接口只是一種限制形式。blog
2 Adapter模式繼承
目的:利用現有的類,知足須要的接口
2.1 接口適配
情形:應用程序須要調用RequiredInterface接口,包含requiredMethod方法,而一個現存的類ExistingClass是一個已有的實現,但接口與應用程序的接口不一致,所以採用接口適配模式。
方法:
定義一個新類NewClass,繼承ExistingClass,並實現RequiredInterface接口。
2.2 對象適配器
情形:應用程序須要調用RequiredClass類,包含requiredMethod()方法,而一個現存的類ExistingClass是一個相同功能的實現
問題:java中只容許單繼承,不能同時繼承RequiredClass和ExisitingClass兩個類。
解決:定義NewClass,繼承RequiredClass(知足應用程序調用要求),幷包含ExistingClass的實例對象做爲成員變量,NewClass在實現requiredMethod()方法時調用ExistingClass實例對象的usefulMethod()方法就能夠了。
2.3 實例:JTable的使用
本例是一個基於對象適配器的模式實例
Javax.swing.JTable類定義了一個常規的二維單元表。構造一個JTable的實例須要實例化成員變量TableModel(接口),爲了方便,java類庫部分實現了接口構造了抽象類AbstractTableModel,只需實現getColumnCount() getRowCount()和getValueAt()三個抽象方法而已,所以選擇對象適配模式。
具體表格里填的是什麼,固然是須要用戶類本身定義,而後適配TableModel接口。
能夠看到,適配類RocketTableModel內包含了Rocket實例對象的數組,抽象類的方法均可以經過調用Rocket對象來實現。
程序,來自www.oozinoz.com
主程序:
-
public class ShowRocketTable {
-
public static void main(String[] args) {
-
setFonts();
-
JTable table = new JTable(getRocketTable());
-
table.setRowHeight(36);
-
JScrollPane pane = new JScrollPane(table);
-
pane.setPreferredSize(new java.awt.Dimension(300, 100));
-
display(pane, " Rockets");
-
}
-
-
/**
-
* Display a Swing component. We'll refactor this later into a nice facade.
-
*
-
* @param c the component to display
-
* @param title the window title
-
*/
-
public static void display(Component c, String title) {
-
JFrame frame = new JFrame(title);
-
frame.getContentPane().add(c);
-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
-
private static RocketTableModel getRocketTable() {
-
Rocket r1 = new Rocket("Shooter", 1.0, new Dollars(3.95), 50.0, 4.5);
-
Rocket r2 = new Rocket("Orbit", 2.0, new Dollars(29.03), 5000, 3.2);
-
return new RocketTableModel(new Rocket[] { r1, r2 });
-
}
-
-
private static void setFonts() {
-
Font font = new Font("Dialog", Font.PLAIN, 18);
-
UIManager.put("Table.font", font);
-
UIManager.put("TableHeader.font", font);
-
}
-
}
適配類RocketTableModel:
-
public class RocketTableModel extends AbstractTableModel {
-
protected Rocket[] rockets;
-
protected String[] columnNames = new String[] { "Name", "Price", "Apogee" };
-
-
/**
-
* Construct a rocket table from an array of rockets.
-
* @param rockets an array of rockets
-
*/
-
public RocketTableModel(Rocket[] rockets) {
-
this.rockets = rockets;
-
}
-
-
/**
-
* @return the number of columns in this table.
-
*/
-
public int getColumnCount() {
-
return columnNames.length;
-
}
-
-
/**
-
* @param index which column is interesting
-
* @return the name of the indicated column
-
*/
-
public String getColumnName(int i) {
-
return columnNames[i];
-
}
-
-
/**
-
* @return the number of rows in this table.
-
*/
-
public int getRowCount() {
-
return rockets.length;
-
}
-
-
/**
-
* @param row which row is interesting
-
* @param col which column is interesting
-
* @return the value at the indicated row and column.
-
*/
-
public Object getValueAt(int row, int col) {
-
switch (col) {
-
case 0:
-
return rockets[row].getName();
-
case 1:
-
return rockets[row].getPrice();
-
case 2:
-
return new Double(rockets[row].getApogee());
-
default:
-
return null;
-
}
-
}
-
}
原類Rocket:
-
public class Rocket extends Firework {
-
private double apogee;
-
-
private double thrust;
-
-
/**
-
* Allow creation of empty objects to support reconstruction from persistent
-
* store.
-
*/
-
public Rocket() {
-
}
-
-
/**
-
* Create a rocket with all its expected properties. See the superclass for
-
* descriptions of other parameters
-
*
-
* @param apogee
-
* The height (in meters) that the rocket is expected to reach
-
* @param thrust
-
* The rated thrust (or force, in newtons) of this rocket
-
*/
-
public Rocket(String name, double mass, Dollars price, double apogee,
-
double thrust) {
-
super(name, mass, price);
-
setApogee(apogee);
-
setThrust(thrust);
-
}
-
-
/**
-
* The height (in meters) that the rocket is expected to reach.
-
*/
-
public double getApogee() {
-
return apogee;
-
}
-
-
public void setApogee(double value) {
-
apogee = value;
-
}
-
-
/**
-
* The rated thrust (or force, in newtons) of this rocket.
-
*/
-
public double getThrust() {
-
return thrust;
-
}
-
-
public void setThrust(double value) {
-
thrust = value;
-
}
-
}
固然,getName() getPrice()這兩個方法在超類Firework中定義。