Point public class Point public double x public
Point המחלקה public class Point { public double x; public double y; public void print. Point(){ System. out. println("("+ x +", "+ y +")"); } public double return } } distance(Point other){ dx = x - other. x; dy = y - other. y; Math. sqrt(dx*dx + dy*dy); ? static איפה המילה ? מבלי לקבל אותם כפרמטר y - ול x– איך השיטה יודעת לפנות ל ? איך מפעילים את השיטה
הפעלת שיטה public static void main(String[] args){ Point p 1 = new Point(); p 1. x = 1; p 1. y = 5; p 1. print. Point(); Point p 2 = new Point(); p 2. x = 2; p 2. y = 3; p 2. print. Point(); double dist = p 1. distance(p 2); System. out. println(dist); } (1. 0, 5. 0) (2. 0, 3. 0) 2. 23606797749979
השוואה בין אובייקטים false public static void main(String[] args){ Point p 1 = new Point(); p 1. x = 1; p 1. y = 2; Point p 2 = new Point(); p 2. x = 1; p 2. y = 2; System. out. println(p 1 == p 2); } ? • מה הפלט
נקודות 2 דוגמא להשוואה בין public static void main(String[] args){ ? • מה יודפס Point p 1 = new Point(); p 1. x = 1; p 1. y = 2; Point p 2 = new Point(); p 2. x = 1; p 2. y = 2; Point p 3 = p 1; System. out. println(p 1 == p 2); System. out. println(p 1 == p 3); System. out. println(p 2 == p 3); System. out. println(p 1. equals(p 2)); System. out. println(p 1. equals(p 3)); System. out. println(p 2. equals(p 3)); } false true
public class Point { public double x; public double y; public Point(int x, int y){ this. x = x; this. y = y; } בנאים • שימוש בבנאי ליצירת אובייקט } public static void main(String[] args){ Point p 1 = new Point(1, 2); } Name Type Value p 1 Point 1231231 Point x=1 y=2
Setters - ו Getters public class Point { • דוגמה public double x; public double y; public Point(int x, int y){ this. x = x; this. y = y; } getter public double get. X(){ return x; } public void set. X(int x){ this. x = x; } setter public double get. Y(){ return y; } public void set. Y(int y){ this. y = y; } } 23
תוך שמירה על Point מימוש שונה למחלקה התנהגות אותה public class Point { private double[] xy; public Point(int x, int y){ xy = new double[2]; xy[0] = x; xy[1] = y; } public double get. X(){ return xy[0]; } public void set. X(int x){ xy[0] = x; }
public double get. Y(){ return xy[1]; } public void set. Y(int y){ xy[1] = y; } public void print. Point(){ System. out. println("("+ xy[0] +", "+ xy[1] +")"); } public double distance(Point other){ double dx = xy[0] - other. xy[0]; double dy = xy[1] - other. xy[1]; return Math. sqrt(dx*dx + dy*dy); } public boolean equals(Point other){ if (other == null){ return false; } return xy[0] == other. xy[0] && xy[1] == other. xy[1]; } }
Complex המחלקה public class Complex { private double real; private double im; public Complex(double a, double b){ real = a; im = b; } public Complex add(Complex other){ double res. Re = this. real + other. get. Re(); double res. Im = this. im + other. get. Im(); return new Complex(res. Re, res. Im); }
Complex המחלקה public Complex double other. get. Im(); double other. get. Re(); return } multiply(Complex other){ res. Re = this. real * other. get. Re() - this. im * res. Im = this. real * other. get. Im() + this. im * new Complex(res. Re, res. Im); public double abs(){ return Math. sqrt(real*real + im*im); } public String to. String(){ return real + " + im +"*i"; }
Complex המחלקה public double get. Re(){ return this. real; } public double get. Im(){ return this. im; } public void set. Re(double re){ this. real = re; } public void set. Im(double im){ this. im = im; } }
שימוש במחלקה . 4+3 i – ו 2+5 i • הגדירו את המספרים המרוכבים והדפיסו את המכפלה שלהם public static void main(String[] args){ Complex c 1 = new Complex(4, 3); Complex c 2 = new Complex(2, 5); System. out. println(c 1. multiply(c 2). to. String()); }
- Slides: 34