I code java with BlueJ for Mac. I have added the stdlib.jar library (From princeton http://introcs.cs.princeton.edu/java/stdlib/). Before added this library I had my own class named StdDraw.java (The specific class I was using on the project) and copy/pasted the code. I also adjusted some of the code and added some new lines. Since I cannot edit the libraries code, how may I override or extend library classes to add additional functionality?
2593 次
2 に答える
0
クラスを拡張するだけで、
public MyClass extends ClassFromLib
ライブラリのjarファイルがクラスパス上にあることを確認してください。そのクラスの作成者がそれをfinalとして宣言した場合、それはサブクラス化に適していないことを示します。その場合、最良の代替策はデリゲートパターンを使用することです。
私はこのエディターで以下のコードを書いたので、それが準拠するという約束はありませんが、うまくいけばあなたはアイデアを得るでしょう。
public Myclass {
private ClassFromLib cfl = new ClassFromLib();
public void methodA(){
//Do whatever you need here
cfl.methodA(); //Doesn't have to be the same name.
//Do whatever you need here
}
}
于 2012-08-10T21:59:51.340 に答える