public class Business protected Employee employees protected double

  • Slides: 38
Download presentation

 הורשה ושיטות פרטיות : דוגמה public class Business { protected Employee[] employees; protected

הורשה ושיטות פרטיות : דוגמה public class Business { protected Employee[] employees; protected double monthly. Rent; . . . // calculates total monthly expenses public double monthly. Expenses() { double salaries = calc. Sum(); return this. monthly. Rent + salaries; } Call to a private method // calculates monthly salaries private double calc. Sum() { double sum = 0; for (int i=0; i<this. employees. length; i=i+1) { sum = sum + this. employees[i]. get. Salary(); } return sum; }. . . } 8

 הורשה ושיטות פרטיות : דוגמה public class Shop extends Business { protected Item[]

הורשה ושיטות פרטיות : דוגמה public class Shop extends Business { protected Item[] items; . . . // override: calculates total monthly expenses public double monthly. Expenses() { double item. Prices = calc. Sum(); return item. Prices + super. monthly. Expenses(); } Call to a private method // No override: calculates total item prices private double calc. Sum() { double sum=0; for (int i=0; i<this. items. length; i=i+1) { sum = sum + this. items[i]. get. Price(); } return sum; }. . . } 9

 הורשה ושיטות פרטיות : דוגמה . המחלקה בה היא מוגדרת scope) ) •

הורשה ושיטות פרטיות : דוגמה . המחלקה בה היא מוגדרת scope) ) • שיטה פרטית נגישה רק בתחום . הקוד המופעל הוא הקוד באותה מחלקה בה התקיימה הקריאה , • לכן Business s = new Shop(); s. monthly. Expenses() Shop extends Business { public double monthly. Expenses() { double item. Prices = calc. Sum(); return item. Prices + super. monthly. Expenses(); } private double calc. Sum() {…} } public double monthly. Expenses() { double salaries = calc. Sum(); return this. monthly. Rent + salaries; } private double calc. Sum() {…} } ? protected על ידי calc. Sum אם נגדיר את monthly. Expenses מה תחשב : שאלה

public class A { public class B extends A { a b private int

public class A { public class B extends A { a b private int x; A B public int y; X=1 X=22 public A(int x) { public B(int x. A, int x. B) { Y=24 this. x = x; super(x. A); A this. y = 2*x; this. x = x. B; X=2 } this. y = x. A + x. B; Y=4 public int get. X() { return x; } } public int double. X() { return 2 * get. X(); } public int get. X() { return x; } public int triple. X() { return 3 * x; } public int super. X() { private int sub. Xhelper() { return x - 1; } return super. get. X(); public int sub. X() { } return sub. Xhelper(); public int ten. Times. X() { return 10*x; } } private int sub. Xhelper() { return x-2; } } } A a = new A (1); A b = new B (2, 22); Output / Notes System. out. println(a. get. X()); System. out. println(b. super. X()); 1 22 Comp. Err: super. X() is undefined for type A if (b instanceof B) System. out. println(b. super. X()); Compilation Error !! 12

public class A { public class B extends A { a b private int

public class A { public class B extends A { a b private int x; A B public int y; X=1 X=22 public A(int x) { public B(int x. A, int x. B) { Y=24 this. x = x; super(x. A); A this. y = 2*x; this. x = x. B; X=2 } this. y = x. A + x. B; Y=4 public int get. X() { return x; } } public int double. X() { return 2 * get. X(); } public int get. X() { return x; } public int triple. X() { return 3 * x; } public int super. X() { private int sub. Xhelper() { return x - 1; } return super. get. X(); public int sub. X() { } return sub. Xhelper(); public int ten. Times. X() { return 10*x; } } private int sub. Xhelper() { return x-2; } } } A a = new A (1); A b = new B (2, 22); Output / Notes B bb = (B)b; System. out. println(bb. super. X()); 2 System. out. println(((B)b). super. X()); System. out. println(a. triple. X()); System. out. println(b. triple. X()); 2 3 6 13

public class A { public class B extends A { a b private int

public class A { public class B extends A { a b private int x; A B public int y; X=1 X=22 public A(int x) { public B(int x. A, int x. B) { Y=24 this. x = x; super(x. A); A this. y = 2*x; this. x = x. B; X=2 } this. y = x. A + x. B; Y=4 public int get. X() { return x; } } public int double. X() { return 2 * get. X(); } public int get. X() { return x; } public int triple. X() { return 3 * x; } public int super. X() { private int sub. Xhelper() { return x - 1; } return super. get. X(); public int sub. X() { } return sub. Xhelper(); public int ten. Times. X() { return 10*x; } } private int sub. Xhelper() { return x-2; } } } A a = new A (1); A b = new B (2, 22); Output / Notes System. out. println(((B)a). ten. Times. X()); Class. Cast. Exception: A cannot be cast to B System. out. println(((B)b). ten. Times. X()); System. out. println(b. double. X()); 220 44 System. out. println(b. sub. X()); 1 14

public class A { public class B extends A { a b private int

public class A { public class B extends A { a b private int x; A B public int y; X=1 X=22 public A(int x) { public B(int x. A, int x. B) { Y=24 this. x = x; super(x. A); A this. y = 2*x; this. x = x. B; X=2 } this. y = x. A + x. B; Y=4 public int get. X() { return x; } } public int double. X() { return 2 * get. X(); } public int get. X() { return x; } public int triple. X() { return 3 * x; } public int super. X() { private int sub. Xhelper() { return x - 1; } return super. get. X(); public int sub. X() { } return sub. Xhelper(); public int ten. Times. X() { return 10*x; } } private int sub. Xhelper() { return x-2; } } } A a = new A (1); A b = new B (2, 22); System. out. println(a. y); System. out. println(b. y); System. out. println(((B)b). y); B bb= (B)b; System. out. println(bb. y); System. out. println(((A)bb). y); Output / Notes 2 4 24 24 4 בשימוש , פרטי - גישה לשדה לא : תזכורת נקבעת לפי טיפוס המצביע , '. ' באופרטור . ( וסריקה כלפי מעלה reference) 15

Spy. Robot : דוגמא public abstract class Spy. Robot { private String model; public

Spy. Robot : דוגמא public abstract class Spy. Robot { private String model; public Spy. Robot(String model) { this. model=model; } public String get. Model() { return this. model; } public abstract void move. Forward(); move. Backward(); turn. Left(); turn. Right(); public void take. Picture() {. . . } public void charge. Battery() {. . . } } 19

Spy. Robot : דוגמא public class Legs. Spy. Robot extends Spy. Robot { public

Spy. Robot : דוגמא public class Legs. Spy. Robot extends Spy. Robot { public Legs. Spy. Robot() { super("Roboquad"); } public void move. Forward() { for(int i=0; i<4; i++) {this. move. Leg(i, 1)}; } public void move. Backward() { for(int i=0; i<4; i++) {this. move. Leg(i, -1)}; } public void turn. Left() { this. move. Leg(0, -1); this. move. Leg(1, -1); this. move. Leg(2, 1); this. move. Leg(3, 1); } public void turn. Right() { this. move. Leg(0, 1); this. move. Leg(1, 1); this. move. Leg(2, -1); this. move. Leg(3, -1); } 2 3 1 0 // direction {1=forward, -1=backward} private void move. Leg(int leg. Id, int dir) {. . . }; } 20

Spy. Robot : דוגמא public class Wheels. Spy. Robot extends Spy. Robot { public

Spy. Robot : דוגמא public class Wheels. Spy. Robot extends Spy. Robot { public Wheels. Spy. Robot() { super("Spyke"); } public void move. Forward() { this. turn. Wheels(1, 1); } public void move. Backward() { this. turn. Wheels(-1, -1); } public void turn. Left() { this. turn. Wheels(0, -1); } public void turn. Right() { this. turn. Wheels(-1, 0); } // direction {1=forward, 0=stop, -1=backward} private void turn. Wheels(int right. Dir, int left. Dir) {. . . }; // move features public void wave. Hands() {. . . } } 21

http: //www. youtube. com/watch? v=_PUEo. DYp. Uy. Q 25

http: //www. youtube. com/watch? v=_PUEo. DYp. Uy. Q 25

 מימוש המחלקה המייצגת פעולה כללית public class Action { private String name; Action

מימוש המחלקה המייצגת פעולה כללית public class Action { private String name; Action public Action(String name) { this. name = name; } public String get. Name(){ return this. name; } public boolean equals(Object other) { boolean ans = false; if(other instanceof Action) ans = this. name. equals(((Action)other). name); return ans; } } 27

 מימוש המחלקה המייצגת שחקן כללי public abstract class Player { private String name;

מימוש המחלקה המייצגת שחקן כללי public abstract class Player { private String name; private int score; select. Action Player Action public Player(String name){ this. name = name; this. score = 0; } public abstract Action select. Action(Action[] actions); public boolean is. Winner(Player p){ return (this. score > p. get. Score()); } public void update. Score(int score){ this. score = this. score + score; } public int get. Score(){ return this. score; } } 28

 מימוש שחקן אקראי public class Random. Player extends Player{ public Random. Player(String name)

מימוש שחקן אקראי public class Random. Player extends Player{ public Random. Player(String name) { super(name); } public Action select. Action(Action[] actions){ int rand. Idx = (int)(Math. random()*actions. length); return actions[rand. Idx]; } select. Action Player Action Random Player } 29

 מימוש שחקן עקבי public class Consecutive. Player extends Player { select. Action Player

מימוש שחקן עקבי public class Consecutive. Player extends Player { select. Action Player private int last. Idx; public Consecutive. Player(String name) { super(name); this. last. Idx = 0; } Random Player Action Consec. Player public Action select. Action(Action[] actions) { this. last. Idx = (this. last. Idx + 1) % actions. length; return actions[this. last. Idx]; } } 30

 מימוש משחק כללי public abstract class Game { init. Action select. Action Game

מימוש משחק כללי public abstract class Game { init. Action select. Action Game Player private Player p 1, p 2; private String name; // game name protected Action[] actions; // the set of actions public Game(Player p 1, Player p 2, String name){ this. p 1 = p 1; this. p 2 = p 2; this. name = name; this. init. Actions(); } Random Player Action Consec. Player // There is no real list of actions in a general game protected abstract void init. Actions(); . . . 31

( מימוש משחק כללי )המשך public abstract class Game {. . . public void

( מימוש משחק כללי )המשך public abstract class Game {. . . public void play(int turn. Count) { for (int i=0; i<turn. Count; i=i+1) this. play. Single. Turn(); } init. Action select. Action Game Player Action Reward. Players Random Player Consec. Player private void play. Single. Turn() { /* The order of selection is irrelevant as one * player does not know the other’s choice */ Action a 1 = this. p 1. select. Action(actions); Action a 2 = this. p 2. select. Action(actions); this. reward. Players(a 1, a 2); } // There is no real scoring strategy in a general game protected abstract void reward. Players(Action a 1, Action a 2); . . . 32

( מימוש משחק כללי )המשך public abstract class Game {. . . public Player

( מימוש משחק כללי )המשך public abstract class Game {. . . public Player get. Winner () { if (this. p 1. is. Winner(this. p 2)) return this. p 1; else return this. p 2; } init. Action select. Action Game Player Action Reward. Players Random Player Consec. Player protected Player get. First. Player() { return this. p 1; } protected Player get. Second. Player() { return this. p 2; } } 33

" מימוש המשחק "אבן נייר ומספריים public class Rock. Paper. Scissors extends Game{ init.

" מימוש המשחק "אבן נייר ומספריים public class Rock. Paper. Scissors extends Game{ init. Action select. Action Game Player public Rock. Paper. Scissors(Player p 1, Player p 2) { super(p 1, p 2, "Rock Paper Scissors"); Reward. Players } protected void init. Actions(){ this. actions = new Action[3]; this. actions[0] = new Action("rock"); this. actions[1] = new Action("paper"); this. actions[2] = new Action("scissors"); }. . . Random Player Action Consec. Player 2 בחירת שחקן 1 בחירת שחקן 34

( מימוש המשחק "אבן נייר ומספריים" )המשך . . . protected void reward. Players(Action

( מימוש המשחק "אבן נייר ומספריים" )המשך . . . protected void reward. Players(Action a 1, Action a 2) { int p 1 score = 0; if (!(a 1. equals(a 2))) { // Different actions if ((a 1. get. Name(). equals("rock") && a 2. get. Name(). equals("scissors")) || (a 1. get. Name(). equals("paper") && a 2. get. Name(). equals("rock")) || (a 1. get. Name(). equals("scissors") && a 2. get. Name(). equals("paper"))) { p 1 score = 1; } else { p 1 score = -1; } } this. get. First. Player(). update. Score(p 1 score); this. get. Second. Player(). update. Score(-p 1 score); 2 בחירת שחקן 1 בחירת שחקן } } // Class Rock. Paper. Scissors 35

" מימוש המשחק "דילמת האסיר public class Prisoner. Dilemmas extends Game{ public Prisoner. Dilemmas(Player

" מימוש המשחק "דילמת האסיר public class Prisoner. Dilemmas extends Game{ public Prisoner. Dilemmas(Player p 1, Player p 2) { super(p 1, p 2, "Prisoner's Dilemma"); } protected void init. Actions(){ this. actions = new Action[2]; this. actions[0] = new Action("silent"); this. actions[1] = new Action("blame"); }. . . ' אסיר ב , שנים 15 שנים 0 , שנה , שנים 5 , שנים 0 שנים 15 ' אסיר א שותק מעיד 37

, שנה , שנים 5 , שנים 0 שנים 15 ' אסיר א ,

, שנה , שנים 5 , שנים 0 שנים 15 ' אסיר א , שנים 15 שנים 0 מעיד שותק } protected void reward. Players(Action a 1, Action a 2) { if (a 1. equals(a 2)) { // Same actions if (a 1. get. Name(). equals("blame") { this. get. First. Player(). update. Score(-5); // blame, blame this. get. Second. Player(). update. Score(-5); } else { this. get. First. Player(). update. Score(-1); this. get. Second. Player(). update. Score(-1); } } else { // Different actions if (a 1. get. Name(). equals("blame") { // blame, silent this. get. First. Player(). update. Score(0); this. get. Second. Player(). update. Score(-15); } else { // silent & blame this. get. First. Player(). update. Score(-15); this. get. Second. Player(). update. Score(0); } ' אסיר ב } } מעיד שותק 38