java:data_encapsulation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:data_encapsulation [2015/01/21 13:24] – gthanos | java:data_encapsulation [2015/01/21 13:56] (current) – [Τύποι πεδίων της κλάσης] gthanos | ||
---|---|---|---|
Line 52: | Line 52: | ||
===== Τύποι πεδίων της κλάσης ===== | ===== Τύποι πεδίων της κλάσης ===== | ||
- | Μία κλάση μπορεί να διαθέτει **primitive** τύπους δεδομένων (πχ. int, long, char, double, float κλπ) ή **reference** τύπους δεδομένων. | + | Μία κλάση μπορεί να διαθέτει **primitive** |
- | Οι reference τύποι δεδομένων | + | <code java> |
+ | class Coordinates { | ||
+ | private double latitude; | ||
+ | private double longitude; | ||
+ | |||
+ | public void Coordinates(double lat, double long) { | ||
+ | latitude = lat; | ||
+ | longitude = long; | ||
+ | } | ||
+ | |||
+ | public double getLatitude() { | ||
+ | return latitude; | ||
+ | } | ||
+ | public double getLongitude() { | ||
+ | return longitude; | ||
+ | } | ||
+ | public void setLatitude(double lat) { | ||
+ | latitude = lat; | ||
+ | } | ||
+ | public void setLongitude(double long) { | ||
+ | longitude = long; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Με χρήση της παραπάνω κλάσης η κλάση '' | ||
+ | <code java> | ||
+ | public class Bicycle { | ||
+ | | ||
+ | private int cadence; | ||
+ | private int gear; | ||
+ | private int speed; | ||
+ | public Coordinates coordinates; | ||
+ | | ||
+ | public Bicycle(int startCadence, | ||
+ | coordinates = new Coordinates(lat, | ||
+ | gear = startGear; | ||
+ | cadence = startCadence; | ||
+ | speed = startSpeed; | ||
+ | } | ||
+ | | ||
+ | public int getCadence() { | ||
+ | return cadence; | ||
+ | } | ||
+ | | ||
+ | public void setCadence(int newValue) { | ||
+ | cadence = newValue; | ||
+ | } | ||
+ | | ||
+ | public int getGear() { | ||
+ | return gear; | ||
+ | } | ||
+ | | ||
+ | public void setGear(int newValue) { | ||
+ | gear = newValue; | ||
+ | } | ||
+ | | ||
+ | public int getSpeed() { | ||
+ | return speed; | ||
+ | } | ||
+ | | ||
+ | public void applyBrake(int decrement) { | ||
+ | speed -= decrement; | ||
+ | } | ||
+ | | ||
+ | public void speedUp(int increment) { | ||
+ | speed += increment; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Η κλάση '' | ||
+ | - Στην 1η γραμμή του κατασκευαστή τη κλάσης '' | ||
+ | - Το πεδίο '' | ||
java/data_encapsulation.1421846657.txt.gz · Last modified: 2015/01/21 13:24 (external edit)