1 / 28

עצמים מורכבים

עצמים מורכבים. הילה קדמן נובמבר 2008. תרשים UML למחלקה Point. המחלקה Point. /** * Point * @author Hila kadman */ public class Point { //--- תכונות הנקודה --- private int x; private int y;. this. Point. x. y. המחלקה Point. /**

bethan
Download Presentation

עצמים מורכבים

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. עצמים מורכבים הילה קדמן נובמבר 2008 עצמים מורכבים

  2. תרשים UML למחלקה Point עצמים מורכבים

  3. המחלקה Point /** * Point * @author Hila kadman */ publicclass Point { //--- תכונות הנקודה --- privateint x; privateint y; עצמים מורכבים

  4. this Point x y המחלקה Point /** * y -ו x פעולה בונה המייצרת נקודה ששיעוריה הם */ public Point (int x, int y) { this.x = x; this.y = y; } עצמים מורכבים

  5. תרשים UML למחלקה Circle המחלקה Circleמורכבת מנקודה. Circle has a Point עצמים מורכבים

  6. המחלקה Circle /** * Circle * מחלקה שאחת מתכונותיה היא עצם * @author Hila kadman */ publicclass Circle { //--- תכונות המחלקה --- private Point center; privatedouble radius; אחת התכונות של המעגל היא עצם מסוג Point עצמים מורכבים

  7. בעיה 1 עצמים מורכבים

  8. זהירות !!! האם זוהי הפעולה הבונה של Circle ?? /** * Circle פעולה בונה למחלה * @param center * @param radius */ public Circle(Point center, double radius) { this.center = center; this.radius = radius; } מה מקבל מרכז המעגל ???(הפנייה? עצם חדש?) עצמים מורכבים

  9. בדוק - מה יהיה הפלט ב- Main() ? publicstaticvoid Main (String[]args) { Point p1 = new Point (4,4); Circle c = new Circle (p1, 5); Console.WriteLine("c: " + c.ToString()); p1.SetY(1); Console.WriteLine("p1: " + p1.ToString()); Console.WriteLine("c: " + c.ToString()); } עצמים מורכבים

  10. Point p1 c x y Circle 4 4 center radius 5 התוצאה ב- Main() publicstaticvoid Main (String[]args) { Point p1 = new Point (4,4); Circle c = new Circle (p1, 5); } עצמים מורכבים

  11. Point p1 c x y Circle 4 4 1 center radius 5 התוצאה ב- Main() publicstaticvoid Main (String[]args) { Point p1 = new Point (4,4); Circle c = new Circle (p1, 5);p1.SetY(1); } שינוי תכונה של p1 משנה גם את מרכז המעגל עצמים מורכבים

  12. התיקון • נוסיף פעולה בונה מעתיקה למחלקה Point.פעולה בונה מעתיקה – פעולה בונה המקבלת עצם כפרמטר ומחזירה הפנייה לעצם חדש שתכונותיו זהות לתכונות העצם המועבר. • בפעולה הבונה של המחלקה Circle נפעיל את הפעולה הבונה המעתיקה של Point. עצמים מורכבים

  13. Point Point p x y x y this 4 4 4 4 פעולה בונה מעתיקה של המחלקה Point /** * פעולה בונה מעתיקה * @param p הפעולה מקבלת נקודה כפרמטר - */ public Point (Point p) { this.x = p.x; this.y = p.y; } עצמים מורכבים

  14. פעולה בונה של המחלקה Circle /** * Circle פעולה בונה למחלה * @param center * @param radius */ public Circle(Point center, double radius) { this.center = new Point (center); this.radius = radius; } מופעלת הפעולה הבונה מעתיקה של Point עצמים מורכבים

  15. this Point Point p Circle x y x y 4 4 4 4 center radius 5 תוצאת הפעולה הבונה של Circle public Circle(Point center, double radius) { this.center = new Point (center); this.radius = radius; } עצמים מורכבים

  16. c Point Point p1 Circle x y x y 4 4 4 4 1 center radius 5 התוצאה (הנכונה) ב- Main() publicstaticvoid Main (String[]args) { Point p1 = new Point (4,4); Circle c = new Circle (p1, 5);p1.SetY(1); } עצמים מורכבים

  17. בעיה 2 עצמים מורכבים

  18. מה יהיה הפלט ?? publicstaticvoid Main (String[]args) { : // המשך התכנית Point p2 = c.GetCenter(); p2.SetX(7); Console.WriteLine("p2: " + p2.ToString()); Console.WriteLine("c: " + c.ToString());} עצמים מורכבים

  19. זהירות !!! Point x y this p2 4 4 Circle center radius 5 מה מחזירה הפעולה GetCenter() ?? /** * פעולה המחזירה את מרכז המעגל * @return the center */ public Point GetCenter() {return this.center;} מוחזרת הפנייה לתכונה של המעגל (למרכזו) עצמים מורכבים

  20. Point x y c p2 4 7 4 Circle center radius 5 על מי משפיע השינוי ב- p2 ?? p2.SetX(7); שינוי ב- p2 משפיע גם על מרכז המעגל עצמים מורכבים

  21. התיקון • במקום להחזיר הפנייה למרכז המעגל, ניצור נקודה חדשה, שתפעיל את הפעולה הבונה המעתיקה של Point ותקבל כפרמטר את הנקודה שהיא מרכז המעגל.Point p = new Point (this.center); • נחזיר הפנייה לנקודה החדשה שיצרנו.return p; עצמים מורכבים

  22. Point Point y x y x this p 4 4 4 4 Circle center radius 5 נשנה את הפעולה getCenter() /** * פעולה המחזירה את מרכז המעגל * @return the center */ public Point getCenter() { Point p = new Point (this.center); return p; } עצמים מורכבים

  23. בעיה 3 עצמים מורכבים

  24. מה יהיה הפלט ?? publicstaticvoid Main (String[]args) { : // המשך התכנית Point p3 = new Point (2,3); c.SetCenter(p); p3.SetX(5); Console.WriteLine("p3: " + p3.ToString()); Console.WriteLine("c: " + c.ToString());} עצמים מורכבים

  25. זהירות !!! Point Point y x x y this 3 2 4 4 Circle center radius p 5 מה מבצעת הפעולה SetCenter() ?? /** * פעולה המעדכנת את מרכז המעגל */ public Point SetCenter(Point p) {this.center = p;} נוצרת הפנייה לעצם שהועבר כפרמטר עצמים מורכבים

  26. Point x y c p3 2 5 3 Circle center radius 5 על מי משפיע השינוי ב- p3 ?? p3.SetX(5); שינוי ב- p3 משפיע גם על מרכז המעגל עצמים מורכבים

  27. התיקון • מכיוון שמרכז המעגל הוא נקודה, נבצע עדכון לתכונות הנקודה: this.center.SetX(center.GetX()); this.center.SetY(center.GetY()); שים ♥:אין הרשאת גישה לתכונות של Point מתוך המחלקה Circle, ולכן יש להפעיל את הפעולות הקובעות של הנקודה. עצמים מורכבים

  28. Point Point y x y x this p 3 4 3 4 2 2 Circle center radius 5 נשנה את הפעולה setCenter() /** * p פעולה הקובעת את מרכז המעגל להיות * @param p the center to set */ publicvoid SetCenter(Point p) { this.center.SetX (p.GetX()); this.center.SetY (p.GetY()); } עצמים מורכבים

More Related