public private public class Bucket private int capacity
הרשאות גישה (public) • פומבי (private) • פרטי public class Bucket { private int capacity; private double current. Amount; public Bucket (int capacity) { this. capacity = capacity; this. current. Amount = 0; } : public boolean is. Empty() { return (this. current. Amount == 0); } } public class Test. Bucket { public static void main (String[] args) { Bucket b 1 = new Bucket(4); int cap = b 1. capacity; boolean check = b 1. is. Empty(); } } ©המרכז להוראת המדעים האוניברסיטה העברית בירושלים 9
פעולות נוספות public class Bucket { private int capacity; private double current. Amount; פעולה . לבניית מחרוזת המתארת את העצם : public String to. String() { return(“The capacity: “ + this. capacity + “n” + “The current amount of water: “ + this. current. Amount); } } ©המרכז להוראת המדעים האוניברסיטה העברית בירושלים 12
? פעולה המקבלת עצם כפרמטר – למי פונים public class Bucket{ public void example (Bucket bucket. Into) { int capacity 1 = this. capacity; int capacity 2 = bucket. Into. get. Capacity(); bucket. Info. capacity; int current. Amount 1 = this. current. Amount; int current. Amount 2 = bucket. Into. get. Current. Amount(); bucket. Info. current. Amount; bucket. Into. fill (5); this. fill (3); } } ©המרכז להוראת המדעים האוניברסיטה העברית בירושלים 13
פעולה המקבלת עצם כפרמטר - פעולות נוספות public void pour. Into (Bucket bucket. Into) { double free. Space = bucket. Into. get. Capacity() – bucket. Into. get. Current. Amount(); if (this. current. Amount < free. Space) { bucket. Into. fill (this. current. Amount); this. current. Amount = 0; } הפעולה else שפוך מהדלי לדלי אחר { bucket. Into. fill (free. Space); this. current. Amount – = free. Space; } } ©המרכז להוראת המדעים האוניברסיטה העברית בירושלים 14
- Slides: 19