introduction to object oriented programming introduction

Introduction to Object Oriented Programming Introduction to Object - PowerPoint PPT Presentation

Introduction to Object Oriented Programming Introduction to Object Oriented Programming plw@ku.ac.th


  1. Introduction to Object Oriented Programming Introduction to Object Oriented Programming ปรีดา เลิศพงศวิภูษณะ plw@ku.ac.th ภาควิชาวิศวกรรมคอมพิวเตอร คณะวิศวกรรมศาสตร มหาวิทยาลัยเกษตรศาสตร 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 1

  2. Constraints void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 2

  3. Constraints 2 Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 3

  4. Access Protection Car c = new Car("New York A234 567", 100.0); c.speed = 150.0; 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 4

  5. Examples of Access Protection public class Car { private private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private private private double maxSpeed; // kilometers per hour public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public double getSpeed() { return this.speed; } public double getMaxSpeed() { return this.maxSpeed; } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 5

  6. Examples of Access Protection 2 // setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 6

  7. Examples of Access Protection class CarTest8 { public static void main(String args[]) { Car c = new Car("New York A45 636", 100.0); c.licensePlate = "New York A45 636"; c.speed = 0.0; c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c. speed + " kilometers per hour."); c.floorIt(); System.out.println(c.licensePlate + " is moving at " + c. speed + " kilometers per hour."); } } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 7

  8. Output c:\> javac Car.java c:\> javac CarTest8.java CarTest8.java:7: Variable licensePlate in class Car not accessible from class CarTest8. c.licensePlate = "New York A45 636"; ^ CarTest8.java:8: Variable speed in class Car not accessible from class CarTest8. c.speed = 0.0; ^ CarTest8.java:9: Variable maxSpeed in class Car not accessible from class CarTest8. c.maxSpeed = 123.45; Examples of Access Protection ^ CarTest8.java:11: Variable licensePlate in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:11: Variable speed in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:16: Variable licensePlate in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:16: Variable speed in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ 7 errors c:\> 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 8

  9. The Four Levels of Access Protection � If you want any object at all to be able to call a method or change a field, declare it public public . � If you want only objects in the same class to be able to get or set the value of a field or invoke a method, declare it private private . � If you want access restricted to subclasses and members of the same package, declare it protected protected . � Finally, to restrict access only to objects in the same package, use no access no access declaration at all. This is called "package" or "default" access, but it has no declaration keyword. 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 9

  10. The Three Benefits of Access Protection The Three Benefits of Access Protection � It allows you to enforce constraints on an object's state. � It provides a simpler client interface. Client programmers don't need to know everything that's in the class, only the public parts. � It separates interface from implementation, allowing them to vary independently. For instance consider making the licensePlate field of Car an instance of a new LicensePlate class instead of a String. 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 10

  11. Changing the Implementation public class Car { private String licensePlate; // e.g. "New York A456 324" private float speed; // kilometers per hour private float maxSpeed; // kilometers per hour public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0F; if (maxSpeed >= 0.0) { this.maxSpeed = (float) maxSpeed; } else { maxSpeed = 0.0F; } } // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public double getSpeed() { return this.speed; } public double getMaxSpeed() { return this.maxSpeed; } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 11

  12. Changing the Implementation 2 // setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + (float) deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0F; } } } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 12

  13. What should be public? ? What should be private What should be private? ? What should be public � Classes are public. � Fields are private. � Constructors are public. � Getter and Setter methods are public. � Other methods must be decided on a case-by-case basis. 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 13

  14. Overloading public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour // constructors public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } public Car(String licensePlate, double speed, double maxSpeed) { this.licensePlate = licensePlate; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } } // other methods... } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 14

  15. this in constructors public Car(String licensePlate, double maxSpeed) { Car Car(licensePlate, 0.0, maxSpeed); } public Car(String licensePlate, double maxSpeed) { this this(licensePlate, 0.0, maxSpeed); } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 15

  16. this in constructors 2 public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour // constructors public Car(String licensePlate, double maxSpeed) { this(licensePlate, 0.0, maxSpeed); } public Car(String licensePlate, double speed, double maxSpeed) { this.licensePlate = licensePlate; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } } // other methods... } 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 16

  17. Operator Overloading � Therefore Java does not support operator overloading. 7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 17

Recommend


More recommend