public interface Interface Name public String some Method
תזכורת - הגדרת מנשק שם המנשק public interface Interface. Name { public String some. Method(); public void another. Method(int param); } public class Concrete implements Interface. Name { … מחלקה המממשת את @Override המנשק public String some. Method() {…} @Override public void another. Method(int param) {…} }
Square המחלקה public class Square implements Shape { המחלקה מצהירה שהיא מממשת את float side; המנשק public Square(float side) { this. side=side; } public float get. Area() { return (side*side); } public String get. Details() { return "Square: side=" + this. side; } } מהמנשק מתודות המנשק שלהמתודות מימוששל מימוש 6
Circle המחלקה public class Circle implements Shape { float radius; public Circle(float radius) { //Constructor this. radius=radius; } public float get. Area() { //Implementing Shape. get. Area() return (float) (radius*Math. PI); } public String get. Details() { //Implementing Shape. get. Details() return "Circle: radius=" + this. radius; } public float get. Radius() { return this. radius; //Circle specific method } } 7
Playing Mp 3 public class MP 3 Song { public class Player { private boolean repeat; private boolean shuffle; public void play(){ // audio codec calculations, // play the song. . . } public void play. Songs(MP 3 Song[] songs) { do { if (shuffle) Collections. shuffle(Arrays. as. List(songs)); // does complicated stuff // related to MP 3 format. . . } for (MP 3 Song song : songs) song. play(); } while (repeat); } }
Playing Video. Clips public class Video. Clip { public class Player { // same as before. . . public void play(){ // video codec calculations, // play the clip. . . } public void play. Videos(Video. Clip[] clips) { do { if (shuffle) Collections. shuffle(Arrays. as. List(clips)); // does complicated stuff // related to MP 4 format. . . for (Video. Clip video. Clip : clips) video. Clip. play(); } } while (repeat); } }
שכפול קוד public void play. Songs(MP 3 Song[] songs) { do { if (shuffle) Collections. shuffle(Arrays. as. List(songs)); for (MP 3 Song song : songs) song. play(); } while (repeat); play() למרות ששני השרותים נקראים ! אלו פונקציות שונות } public void play. Videos(Video. Clip[] clips) { do { if (shuffle) Collections. shuffle(Arrays. as. List(clips)); for (Video. Clip video. Clip : clips) video. Clip. play(); } while (repeat); } נרצה למזג את שני קטעי הקוד
שימוש במנשק public void play (Playable[] items) { do { if (shuffle) Collections. shuffle(Arrays. as. List(items)); for (Playable item : items) item. play(); } while (repeat); public interface Playable { } public void play(); }
מימוש המנשק ע"י הספקים public class Video. Clip implements Playable { @Override public void play() { // render video, play the clip on screen. . . } // does complicated stuff related to video formats. . . } public class MP 3 Song implements Playable { @Override public void play(){ // audio codec calculations, play the song. . . } // does complicated stuff related to MP 3 format. . . }
מערכים פולימורפים Playable[] playables = new Playable[3]; playables[0] = new MP 3 Song(); playables[1] = new Video. Clip(); playables[2] = new MP 4 Song(); // new Playable class Player player = new Player(); // init player. . . public void play (Playable [] items) { do { player. play(playables); if (shuffle) Collections. shuffle(Arrays. as. List(items)); for (Playable item : items) item. play(); עבור כל איבר במערך המתאים play() יקרא ה } while (repeat); }
פעולות על סיביות אופרטורים לביצוע פעולות על ביטים n (int, short, byte, char) רק על טיפוסים שלמים n 3 00000000000000011 ~ Unary bitwise complement << Signed left shift >> Signed right shift >>> Unsigned right shift & Bitwise AND ^ Bitwise XOR | Bitwise OR 20
פעולות על סיביות - דוגמאות 32 int n ביטים. ייצוג בינארי 00000000000000011 3 11111111111111100 ~3 11111111111111101 -3 000000000000001100 3 << 2 11111111111111110 -3 >> 1 01111111111111110 -3 >>> 1 n מה נקבל מ ? i & 3 22
Interpreting a Stack Trace of an Exception ניתן להשתמש במידע שניתן לנו כדי , כשנתקלים בחריגה במהלך ריצת התוכנית n. לזהות את סוג החריגה ואת המיקום בתוכנית שבו היא ארעה Console: Exception in thread "main" java. lang. Null. Pointer. Exception at com. example. myproject. Book. get. Title(Book. java: 16) at com. example. myproject. Author. get. Book. Titles(Author. java: 25) at com. example. myproject. Bootstrap. main(Bootstrap. java: 14) Book. java: public String get. Title() { System. out. println(title. to. String()); <-- line 16 return title; } 25
Interpreting a Stack Trace of an Exception : דוגמא נוספת n Exception in thread "main" java. lang. Out. Of. Memory. Error: Java heap space at java. util. Arrays. copy. Of(Unknown Source) at java. lang. Abstract. String. Builder. expand. Capacity(Unknown Source) at java. lang. Abstract. String. Builder. ensure. Capacity. Internal(Unknown Source) at java. lang. Abstract. String. Builder. append(Unknown Source) at java. lang. String. Builder. append(Unknown Source) at Small. Test. Multi. Collections. test. Order(Small. Test. Multi. Collections. java: 56) at Small. Test. Multi. Collections. main(Small. Test. Multi. Collections. java: 34 26
- Slides: 27