המשך תכנות מונחה עצמים תרגול מס' 9.

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Based on OOP with Java, by D.J. Barnes 1 Review 4 View classes as modules Encapsulate operations 4 View classes as struct types Encapsulate data 4 View.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Encapsulation CMSC 202. Types of Programmers Class programmers – Developers of new classes – Goal: Expose the minimum interface necessary to use a new.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
Objects and Classes Mostafa Abdallah
CMSC 341 Java Packages, Classes, Variables, Expressions, Flow Control, and Exceptions.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
More on Objects Mehdi Einali Advanced Programming in Java 1.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
יסודות מדעי המחשב – תרגול 6
Chapter 13 Exception Handling
Chapter 10 – Exception Handling
Introduction to Exceptions in Java
Exceptions, Interfaces & Generics
Lecture 17: Polymorphism (Part II)
Java’s World in UML Object Shape {abstract} This is done implicitly
Chapter 8 Classes and Objects
University of Central Florida COP 3330 Object Oriented Programming
Exceptions The Need for Exceptions Throwing Exceptions
Advanced Programming in Java
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
CS 302 Week 11 Jim Williams, PhD.
Advanced Programming in Java
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Handling Exceptions.
Overloading and Constructors
בניית מחלקות.
null, true, and false are also reserved.
Defining New Types of Objects, part 3
Introduction to Java Programming
Everything the light touches, Simba, will be yours
בניית מחלקות.
CS 302 Week 9 Jim Williams.
Today’s topics UML Diagramming review of terms
Basics of OOP A class is the blueprint of an object.
Session 2: Introduction to Object Oriented Programming
Lecture 18: Polymorphism (Part II)
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
Chapter 12 Exception Handling and Text IO Part 1
Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CMP 167 Programming Methods One
Presentation transcript:

המשך תכנות מונחה עצמים תרגול מס' 9

היום בתרגול this Deep Copy Shallow Copy, הכמסה (Encapsulation) הגבלת גישה (visibility modifiers) Getters ו- Setters חריגות (Exceptions)

תזכורת: מחלקות ואובייקטים הנה המחלקה MyString: public class MyString { //class fields public char[] elements; public int length; //class method public int length(){ return length; } שדות – מייצגים מצב של האובייקט שיטה – מתארת התנהגות של האובייקט

תזכורת: מחלקות ואובייקטים על מנת ליצור אובייקט מסוג MyString, נשתמש במילה new: אך כיצד ניתן לשים ערכים בתוך המחרוזת שלנו? לשם כך נגדיר בנאי, שבעזרתו נאתחל את שדה האובייקט MyString s = new MyString();

תזכורת: בנאי (constructor) בנאי – שיטה מיוחדת עבור מחלקה שתפקידה לייצר אובייקט ולאתחל את שדותיו ועכשיו נוכל לאתחל אובייקט מסוג MyString ישירות: public class MyString { public char[] elements; public int length; public MyString(String s){ length = s.length(); elements = new char[length]; for (int i = 0; i < s.length(); i++) { elements[i] = s.charAt(i); } … השם זהה לשם המחלקה ואין ערך החזר MyString s = new MyString("bimba");

תזכורת: בנאי (constructor) נוסיף בנאים אחרים: public class MyString { //class fields public char[] elements; public int length; //constructor with a parameter public MyString(char[] otherElements){ length = otherElements.length; elements = new char[length]; for (int i = 0; i < otherElements.length; i++) { elements[i] = otherElements[i]; } ...

תזכורת: בנאי ברירת מחדל (default constructor) ברגע שמגדירים בנאי כלשהו, לא יסופק לנו בנאי חסר פרמטרים מה יקרה כאשר נבצע: public class MyString { //class fields public char[] elements; public int length; //constructor with a parameter public MyString(char[] otherElements){ … } MyString s1 = new MyString(); שגיאת קומפילציה

תזכורת: בנאי מעתיק (copy constructor) משתמשים בבנאי זה באופן הבא: public MyString(MyString other) { length = other.length; elements = new char[length]; for (int i = 0; i < length; i=i+1) elements[i] = other.elements[i]; } מעתיקים את השדות של האובייקט (העתקת המצב) MyString s1 = new MyString(“Bob”); MyString s2 = new MyString(s1);

מה היתרון בשימוש בדרך זו? this ראינו כי ניתן להגדיר מספר בנאים למחלקה בעזרת העמסה (Overloading) ניתן לקרוא מבנאי אחד לבנאי אחר בעזרת המילה השמורה: לדוגמה: הקריאה חייבת להתבצע בשורה הראשונה של הבנאי this public MyString(MyString other){ this(other.elements); } ( ) מה היתרון בשימוש בדרך זו?

this דוגמה נוספת: מה עושה הבנאי השני? public class MyString{ public char[] elements;   public int length; public MyString(String s) { … } public MyString() { this("");

this ניתן להשתמש ב - this על מנת להבדיל בין שדה לבין פרמטר או משתנה לוקאלי בעלי אותו שם . לדוגמה: בעזרת האופרטור (.) public class MyString{ public char[] elements; public int length; public MyString(char[] elements, int length) { this.length = length; this.elements = new char[this.length]; for (int i = 0; i < this.length; i=i+1) this.elements[i] = elements[i]; }

Deep Copy ו- Shallow Copy תזכורת (מההרצאות) – המחלקות Point ו- Circle: public class Point { public double x; public double y; public Point() { x = 0; y = 0; } public Point(Point p) { x = p.x; y = p.y; בנאי חסר פרמטרים בנאי מעתיק

Deep Copy ו- Shallow Copy public class Circle{ public Point center; public double radius; //constructors public Circle() { center = new Point(); radius = 0; } public Circle(Point cen, double rad) { center = new Point(cen); if (rad >= 0) radius = rad; else rad = 0; … }//Circle בנאי חסר פרמטרים בנאי המקבל את כל השדות

Deep Copy ו- Shallow Copy בנאי מעתיק אפשרי ל- Circle: מה יקרה אם נבצע את הפעולות הבאות: הבנאי המעתיק משתמש בגישת ה- Shallow Copy. public Circle(Circle other) { center = other.center; radius = other.radius; } Copy constructor public static void main(String[] args){ Circle circ1 = new Circle(); Circle circ2 = new Circle(circ1); circ2.center.x = 4; }

Deep Copy ו- Shallow Copy בנאי מעתיק אחר ל- Circle: מה יקרה עכשיו אם נבצע את הפעולות הבאות: הבנאי המעתיק הזה משתמש בגישת ה- Deep Copy. public Circle(Circle other) { center = new Point(other.center); radius = other.radius; } Circle circ1 = new Circle(); Circle circ2 = new Circle(circ1); circ2.center.x = 4;

toString() equalsהשיטה היינו רוצים לבצע השוואה לוגית בין שני אובייקטים תזכורת: ישנן שיטות המשותפות לכל האובייקטים. למשל... היינו רוצים לבצע השוואה לוגית בין שני אובייקטים על מנת לעשות זאת נשתמש בשיטה equals (כמו שראינו ב- String) שיוויון לוגי אינו בהכרח השוואה של כל שדות האובייקט toString()

מחזיר true רק אם other אינו Null ומטיפוס Point equalsהשיטה מחזיר true רק אם other אינו Null ומטיפוס Point נממש מחדש את השיטה equals עבור המחלקה Point public class Point { public double x; public double y; public boolean equals(Object other){ boolean ans = false; if (other instanceof Point){ Point otherPoint = (Point)other; if (x==otherPoint.x && y==otherPoint.y) ans = true; } return ans;

השוואה בין כתובות (הערך בטבלת המשתנים) equals השיטה אם לא היינו ממשים את equals במחלקה Point , Java מספק לנו שיטת equals אשר משווה בין כתובות. public boolean equals(Object other){ return this == other; } השוואה בין כתובות (הערך בטבלת המשתנים)

הכמסה (encapsulation) עקרון המאפשר לאובייקט להכיל את המצב שלו ואת השיטות הקשורות לו. הרעיון: הצגת היכולות של אובייקט ע"י השיטות שמוגדרות כ- public, וללא הצגת המבנה הפנימי של האובייקט (כיצד הוא בנוי) המשתמש לא צריך לדעת כיצד האובייקט עובד, אלא רק מה הוא עושה רעיון זה דומה לרעיון שפונקציה מבצעת פעולות מסוימות, ומי שמשתמש בפונקציה, לא צריך לדעת כיצד היא ממומשת

הכמסה (encapsulation) מה משיגים בעזרת הכמסה והגבלת גישה: שמירה על חוקיות המצב של האובייקט – ניתן לקבוע אילו ערכים מייצגים מצב חוקי ואילו לא פשטות – מי שכותב את המחלקה קובע מה המשתמש צריך לדעת על האובייקט ומה לא הגנה על המידע הפנימי באובייקט – ניתן להבטיח שאף אחד לא יוכל לקרוא את הערכים הפנימיים של האובייקט גמישות במימוש ובשינוי המימוש

Getters ו- Setters על מנת להבטיח את עקרון ההכמסה נשתמש בשיטות על מנת לחשוף את המצב של האובייקט getter – שיטה אשר מחזירה ערך של שדה מסוים setter – שיטה אשר קובעת ערך של שדה מסוים בעזרת השיטות האלו נוכל לשנות את מצב האובייקט, תוך כדי שמירה על עקרון ההכמסה

Getters ו- Setters דוגמה – קביעת מהירות במכונית: public class Car { public final int MAX_SPEED = 210; public final int MIN_SPEED = -20; public int speed; public int getSpeed() { return speed; } public void setSpeed(int speed) { if ((speed >= MIN_SPEED) && (speed <= MAX_SPEED)){ this.speed = speed; קבועים של המחלקה getter setter

Visibility Modifiers מה הבעיה בדוגמה הקודמת? השדה speed מוגדר כ- public לכן עדיין ניתן לשנות את ערכו מחוץ למחלקה, לדוגמה: public class Car { … public int speed; } Car car1 = new Car(); car1.speed = 500;

Visibility Modifiers ב- Java, ניתן להגביל גישה לשדה בעזרת שינוי ה- visibility modifier של השדה כבר ראינו שימוש ב- visibility modifier: public, אשר מאפשר גישה לשדה לכולם (גם מחוץ למחלקה) ישנו visibility modifier נוסף: private אשר מגביל את הגישה לשדות, כך שרק שיטות של המחלקה יוכלו לגשת אליו

Visibility Modifiers מה יקרה בדוגמה הבאה: כאשר ננסה לגשת לשדה מחוץ למחלקה? נקבל שגיאת קומפילציה public class Car { … private int speed; } Car car1 = new Car(); car1.speed = 500;

Visibility Modifiers ראינו כי ניתן להגביל גישה לשדה, אך ניתן להגביל גישה גם עבור שיטות של המחלקה ניתן לקבוע חלק מהשיטות כ- private (במקום public) שיטה שמוגדרת כ- private משמשת כשיטת עזר לביצוע פעולות פנימיות של המחלקה

Visibility Modifiers public double getPrice() { public class Car { … //we are not FRAIERIM public double getPrice() { return getMarketPrice()*1.1; } private double getMarketPrice(){ ...

Getters ו- Setters לא תמיד נרצה להוסיף Getter ו- Setter לכל שדה במחלקה לדוגמה: אם מגדירים שמכונית מקבלת צבע בעת יצירתה, ואין אפשרות לשנותו, אך עדיין רוצים לאפשר להחזיר את הצבע public class Car { private String color; … public Car(String color) { this.color = color; } public String getColor() { return color; הגדרנו רק Getter (ללא Setter)

Getters ו- Setters דוגמה נוספת: נניח שישנו קוד הפעלה למכונית, ולא נרצה לחשוף אותו public class Car { private String code; … public Car(String initialCode) { code = initialCode; } public void startCar(String code) { if (!code.equals(this.code)) System.out.println("Wrong code"); else

הגדרנו רק Setter (ללא Getter) Getters ו- Setters (המשך משקף קודם) public class Car { … public void setNewCode(String oldCode, String newCode) { if (!oldCode.equals(code)) System.out.println(“Wrong code”); else code = newCode; } הגדרנו רק Setter (ללא Getter)

חריגות (Exceptions) חריגה היא אירוע המתרחש במהלך תוכנית המפר את תהליך הריצה הנורמאלי של פקודות התוכנית לעיתים חריגות מתרחשות בגלל תקלות בלתי צפויות, כגון בעיה בקובץ אליו כותבים (למשל אין הרשאות כתיבה), ולעיתים בגלל תקלות תוכנה, כגון שליחת פרמטר לא מתאים לפונקציה 31

בעצם כבר נתקלנו ב-Exceptions ArithmeticException: ניסיון חלוקה באפס IndexOutOfBoundsException: חריגה ממערך NullPointerException: ניסיון לפעול על מערך בעל ערך null Runtime Exceptions 32

NullPointerException סוגי Exceptions Exception IOException RuntimeException NullPointerException 33

Exception ניתן לייצר חריגה ע"י פקודת throw המייצרת את אירוע החריגה. ישנן שתי דרכים לטפל בחריגה: לתפוס את ה- Exception על ידי שימוש במילים השמורות try ו-catch להעביר את ה- Exception הלאה על ידי שימוש במילה השמורה throws בכותרת הפונקציה שאנו כותבים. (לא הכרחי עבור (RuntimeExceptions

Throw and Catch Exceptions public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void setSpeed(int speed){ if ((speed >= MIN_SPEED) & (speed <= MAX_SPEED)) this.speed = speed; else throw new RuntimeException(“illegal speed”); } public static void main(String[] args) { Car car = new Car(); car.setSpeed(300); } Output: Exception in thread "main" java.lang.RuntimeException: Illegal Speed at Car.setSpeed(Car.java:11) at Car.main(Car.java:17)

Throw and Catch Exceptions public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void setSpeed(int speed) throws Exception { if ((speed >= MIN_SPEED) & (speed <= MAX_SPEED)) this.speed = speed; else throw new Exception(“illegal speed”); } public static void main(String[] args) { Car car = new Car(); car.setSpeed(100); } Compilation Error

Throw and Catch Exceptions public class Car { private final int MAX_SPEED = 210; private final int MIN_SPEED = -20; private int speed; … public void setSpeed(int speed) throws Exception { if ((speed >= MIN_SPEED) & (speed <= MAX_SPEED)) this.speed = speed; else throw new Exception(“illegal speed”); } public static void main(String[] args) { Car car = new Car(); try{ car.setSpeed(300); } catch(Exception e){ System.err.println("Caught Exception: " + e.getMessage()); Output: Caught Exception: Illegal Speed