import n import import java util System out

  • Slides: 54
Download presentation

import משפט כאשר עושים שימוש נרחב במחלקות מחבילה מסויימת ניתן n : יחיד import

import משפט כאשר עושים שימוש נרחב במחלקות מחבילה מסויימת ניתן n : יחיד import לייבא את שמות כל המחלקות במשפט import java. util. *; . . . System. out. println("Before: x=" + Arrays. to. String(arr)); import כלומר יש צורך במשפט , * אינו רקורסיבי - השימוש ב n : נפרד עבור כל תת חבילה // for classes directly under subpackage import package. subpackage. *; // for classes directly under subsubpackage 1 import package. subsubpackage 1. *; // only for the class some. Class import package. subsubpackage 2. some. Class; Java בשפת 1 תוכנה אוניברסיטת תל אביב 6

static import משפט ניתן לייבא למרחב השמות את השרות Java 5 החל מ n

static import משפט ניתן לייבא למרחב השמות את השרות Java 5 החל מ n ( ובכך להימנע static import) או המשתנה הסטטי : מציון שם המחלקה בגוף הקוד package il. ac. tau. cs. software 1. examples; import static il. ac. tau. cs. software 1. examples. Some. Other. Class. some. Method; public class Some. Class { public static void main(String[] args) { some. Method(); } } * - ניתן להשתמש ב static import גם ב n Java בשפת 1 תוכנה אוניברסיטת תל אביב 7

/** Documetntaion for the package */ package some. Package; /** Documetntaion for the class

/** Documetntaion for the package */ package some. Package; /** Documetntaion for the class * @author your name here */ public class Some. Class { /** Documetntaion for the class variable */ public static int some. Variable; /** Documetntaion for the class method * @param x documentation for parameter x * @param y documentation for parameter y * @return * documentation for return value */ public static int some. Method(int x, int y, int z){ // this comment would NOT be included in the documentation return 0; } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 12

 דוגמת שימוש public class Turle. Client { public static void main(String[] args) {

דוגמת שימוש public class Turle. Client { public static void main(String[] args) { Turtle leonardo = new Turtle(); if(!leonardo. is. Tail. Down()) leonardo. tail. Down(); leonardo. move. Forward(50); leonardo. turn. Right(90); } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 23

 עוד דוגמת שימוש public class Turle. Client { public static void main(String[] args)

עוד דוגמת שימוש public class Turle. Client { public static void main(String[] args) { Turtle leonardo = new Turtle(); leonardo. tail. Down(); draw. Square. Pattern(leonardo, 50, 10); } public static void draw. Square(Turtle t, int size) { for (int i = 0; i < 4; i++) { t. move. Forward(size); t. turn. Right(90); } } public static void draw. Square. Pattern(Turtle t, int size, int angle) { for (int i = 0; i < 360/angle; i++) { draw. Square(t, size); t. turn. Right(angle); } } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 24

 כדי להכליל את הדוגמא נחליף שמסמן d את שם המשתנה שיסמל עצם this

כדי להכליל את הדוגמא נחליף שמסמן d את שם המשתנה שיסמל עצם this - ב date מטיפוס כלשהו public class My. Date { private int day; private int month; private int year; public static void increment. Date(My. Date d){ // changes d to be the consequent day } public static String to. String(My. Date d){ return d. day + "/" + d. month + "/" + d. year; } public static void set. Day(My. Date d, int day){ /* changes the day part of d to be day if * the resulting date is legal */ } public static int get. Day(My. Date d){ return d. day; } private static boolean is. Legal(My. Date d){ // returns if d represents a legal date } // more. . . } Java בשפת 1 תוכנה אוניברסיטת תל אביב 31

public class My. Date { private int day; private int month; private int year;

public class My. Date { private int day; private int month; private int year; ! שימו לב ! כמציין שם של משתנה הוא אסור this השימוש במילה ואסורה java היא מילה שמורה בשפת this המילה . לשימוש עבור משתני משתמש public static void increment. Date(My. Date this){ // changes d to be the consequent day } בהמשך הדוגמא יובהר מדוע בחרנו דווקא להשתמש בשם זה public static String to. String(My. Date this){ return this. day + "/" + this. month + "/" + this. year; } public static void set. Day(My. Date this, int day){ /* changes the day part of d to be day if * the resulting date is legal */ } public static int get. Day(My. Date this){ return this. day; } private static boolean is. Legal(My. Date this){ // returns if d represents a legal date } // more. . . } Java בשפת 1 תוכנה אוניברסיטת תל אביב 32

! הקוד הזה חוקי public class My. Date { private int day; private int

! הקוד הזה חוקי public class My. Date { private int day; private int month; private int year; מוכר בתוך this המשתנה שרותי המופע כאילו הועבר ע"י . המשתמש public static void increment. Date(My. Date this){ // changes itself to be the consequent day } אולם לא חובה להשתמש בו public static String to. String(My. Date this){ return this. day + "/" + this. month + "/" + this. year; } public static void set. Day(My. Date this, int day){ /* changes the day part of itself to be day if * the resulting date is legal */ } public static int get. Day(My. Date this){ return this. day; } private static boolean is. Legal(My. Date this){ // returns if the argument represents a legal date } // more. . . } Java בשפת 1 תוכנה אוניברסיטת תל אביב 39

public class My. Date { private int day; private int month; private int year;

public class My. Date { private int day; private int month; private int year; public void increment. Date(){ // changes current object to be the consequent day } public String to. String(){ return day + "/" + month + "/" + year; } public void set. Day(int day){ /* changes the day part of the current object to be day if * the resulting date is legal */ } public int get. Day(){ return day; } private boolean is. Legal(){ // returns if the current object represents a legal date } // more. . . } Java בשפת 1 תוכנה אוניברסיטת תל אביב 40

public class My. Date { public My. Date(int day, int month, int year) {

public class My. Date { public My. Date(int day, int month, int year) { this. day = day; this. month = month; this. year = year; } //. . . My. Date הגדרת בנאי ל } public class My. Date. Client { public static void main(String[] args) { My. Date d 1 = new My. Date(29, 2, 1984); d 1. increment. Date(); System. out. println(d 1. to. String()); } } My. Date - קוד לקוח המשתמש ב Java בשפת 1 תוכנה אוניברסיטת תל אביב 42

Default constructor is created only if there are no constructors. If you define any

Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created. Java בשפת 1 תוכנה אוניברסיטת תל אביב 43

public class Point { private static double num. Of. Points; private double x; private

public class Point { private static double num. Of. Points; private double x; private double y; public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } public double get. X() { return x; } /** tolerant method, no precondition - for nonresponsible clients * @post (new. X > 0. 0 && new. X < 100. 0) $implies get. X() == new. X * @post !(new. X > 0. 0 && new. X < 100. 0) $implies get. X() == $prev(get. X()) */ public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); } /** only business logic. Has a preconditon - for responsible clients * @pre (new. X > 0. 0 && new. X < 100. 0) * @post get. X() == new. X */ public void do. Set. X(double new. X) { x = new. X; } // More methods. . . } Java בשפת 1 תוכנה אוניברסיטת תל אביב 46

Point. User public class Point. User { public static void main(String[] args) { Point

Point. User public class Point. User { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); System. out. println("p 1. x == " + p 1. get. X()); } } Java בשפת 1 תוכנה אוניברסיטת תל אביב 47

 בנאי ריצת במהלך (target מצביעה ) מטרה this ההפניהעצם בנאי יש , או

בנאי ריצת במהלך (target מצביעה ) מטרה this ההפניהעצם בנאי יש , או מופע שרות בכל הפעלה של הוקצה עתה העצם שזה שעליו הופעלעל מצביעה לעצם זה this ההפניה . השרות " "בנץ הוא זה this y 2. 0 x 1. 0 STACK HEAP 0 1 Point(x, y) p 1 args [] main public class Point. User { x y 1. 0 0 2. 0 0 public class Point { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } CODE public double get. X() { return x; } System. out. println("p 1. x == " + p 1. get. X()); } } Point. num. Of. Points Java בשפת 1 תוכנה אוניברסיטת תל אביב public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); } 49 public void do. Set. X(double new. X) { x = new. X; }

" "בנץ הוא זה this y 20. 0 x 10. 0 STACK HEAP Point(x,

" "בנץ הוא זה this y 20. 0 x 10. 0 STACK HEAP Point(x, y) x y p 2 p 1 args [] main public class Point. User { 1 2 10. 0 0 20. 0 0 x y 1. 0 2. 0 public class Point { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } CODE public double get. X() { return x; } System. out. println("p 1. x == " + p 1. get. X()); } } Point. num. Of. Points Java בשפת 1 תוכנה אוניברסיטת תל אביב public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); } 50 public void do. Set. X(double new. X) { x = new. X; }

this new. X 11. 0 STACK this new. X בזימון של שרות מופע עצם

this new. X 11. 0 STACK this new. X בזימון של שרות מופע עצם המטרה הוא העצם this. x = new. X בעצם x = new. X ההשמה ? הקריאה מטרת עצם הוא איזה this. do. Set. X(new. X) בעצם היאהיא do. Set. X(new. X) הקריאה תצביע לעצם זה this ההפניה. שעליו הופעל השרות do. Set. X 11. 0 HEAP 2 set. X x y p 2 p 1 args [] main public class Point. User { 10. 0 20. 0 x y 11. 0 2. 0 public class Point { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } CODE public double get. X() { return x; } System. out. println("p 1. x == " + p 1. get. X()); } } Point. num. Of. Points Java בשפת 1 תוכנה אוניברסיטת תל אביב public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); this. do. Set. X(new. X); } 51 public void do. Set. X(double new. X) x = new. X; } {{ this. x = new. X; }

this new. X 21. 0 STACK this new. X העצם המטרה הוא עצם הקריאהשל

this new. X 21. 0 STACK this new. X העצם המטרה הוא עצם הקריאהשל שרות מופע בזימון תחבירי סוכר היא do. Set. X(new. X) this. x = new. X של תחבירי סוכר היא x = new. X ההשמה ? הקריאה מטרת עצם הוא איזה תצביע לעצם זה this. הופעל השרות this. ההפניה do. Set. X(new. X) שעליו של do. Set. X 21. 0 HEAP set. X x y p 2 p 1 args [] main public class Point. User { 2 21. 0 10. 0 20. 0 x y 11. 0 2. 0 public class Point { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } CODE public double get. X() { return x; } System. out. println("p 1. x == " + p 1. get. X()); } } Point. num. Of. Points Java בשפת 1 תוכנה אוניברסיטת תל אביב public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); this. do. Set. X(new. X); } 52 public void do. Set. X(double new. X) x = new. X; } {{ this. x = new. X; }

? הקריאה מטרת עצם הוא איזה "return this. x" הוא בעצם "return x" המשפט

? הקריאה מטרת עצם הוא איזה "return this. x" הוא בעצם "return x" המשפט STACK this HEAP get. X x y p 2 p 1 args [] main public class Point. User { 2 21. 0 “ 11” 20. 0 x y “p 1. x == ” 11. 0 2. 0 “p 1. x == 11” public class Point { public static void main(String[] args) { Point p 1 = new Point(1. 0, 2. 0); Point p 2 = new Point(10. 0, 20. 0); p 1. set. X(11. 0); p 2. set. X(21. 0); public Point(double x, double y){ this. x = x; this. y = y; num. Of. Points++; } CODE public double get. X() { return this. x; } System. out. println("p 1. x == " + p 1. get. X()); } } Point. num. Of. Points Java בשפת 1 תוכנה אוניברסיטת תל אביב public void set. X(double new. X) { if(new. X > 0. 0 && new. X < 100. 0) do. Set. X(new. X); } 53 public void do. Set. X(double new. X) x = new. X; } {{ this. x = new. X; }