Baixe o app para aproveitar ainda mais
Prévia do material em texto
Projeto de Software / P3 Orientação a Objetos: Conceitos Básicos Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro Procedural Programming 3 Func0on-‐based § Programming unit: func0on § We group func0ons to form a program 4 What is the size of an array? 23 12 25 32 33 53 21 2 … 89 function doSomething() {! $array = array(23,12,25,32,33,53,21,2,...,89);! for ($i = 0; $i < count($array); $i++) {! ...! }! }! The array does NOT know! 5 Use this idea for all objects in the world… What? You don’t know your age? You also don’t know if you are romantic? 6 The array could “know” its own size! 23 12 25 32 33 53 21 2 … 89 count(array);! ! versus ! array.size();! Object-‐Oriented Programming 8 Collec0on of interac0ng objects Object 1 Object 5 Object 2 Object 3 Object 4 9 What can we model with objects? § Tangible things § Airplane § Seat § Conceptual things § Posi0on job (pilot) § Date § Processes § Sor0ng passengers § Finding a passenger From http://www.cs.brown.edu/courses/cs015/lectures.html 10 Objects have § Proper0es § Features that describe them § Capabili0es § What they can do, how they behave 11 Proper0es § Features that describe the object § Constant or variable § Proper0es may themselves be objects engines speed capacity ______________ ______________ ______________ 12 State Collec0on of all of an object’s proper0es and their values; it changes if any property value changes From http://www.cs.brown.edu/courses/cs015/lectures.html 13 Capabili0es § Allow objects to perform ac0ons § Ini0al state § Change state § Responses based on proper0es § Objects “know” how to do things accelerate decelerate ______________ ______________ ______________ 14 Modeling objects § How to model every object in earth? § Commonali0es § Variabili0es § We need a way to create objects sets taking the advantage of their commonali0es 15 Classifica0on § Group of objects that has the same set of proper0es and capabili0es § Define an object type: AircraX Airline company: GOL Manufacturer: Boeing Model: 737 N. of engines: 2 Capacity: 174 Airline company: Lufthansa Manufacturer: Boeing Model: 747 N. of engines: 4 Capacity: 540 accelerate decelerate accelerate decelerate How to create an object? 17 Think about it § An object has: § Proper0es § Capabili0es § An object know how to do things… § Proper0es and capabili0es should be “inside” the object § So, we can use a box to throw everything in it 18 Let’s create a snack object… § … using a box We need a TEMPLATE! 20 Template to organize our snack 21 Class § Defines a template for making objects instances § Defines proper0es as a\ributes § Defines capabili0es as methods From http://www.cs.brown.edu/courses/cs015/lectures.html 22 Objects instances: individual objects § Made from class § Many instances of the same class § Same a\ributes, poten0ally with different values From http://www.cs.brown.edu/courses/cs015/lectures.html 23 Implemen0ng a Class in Java public class Aircraft { String manufacturer; int speed; int numberOfEngines; public Aircraft(String manufacturer, int speed, int numberOfEngines) { this.manufacturer = manufacturer; this.speed = speed; this.numberOfEngines = numberOfEngines; } public void accelerate(int speed) { … } }! Properties (attributes) Capabilities (methods) 24 No0ce that… § There is a special method named “constructor” § Code conven0ons! § numberOfEngines § AircraX (class); AircraX (constructor) 25 How to instan0ate an object of AircraX?! § New operator: § Used to create instances § We call the constructor capability public class Main { public static void main (String[] args) { Aircraft boeing737 = new Aircraft(“Boeing”, 0, 2); boeing737.accelerate(840); } }! 26 Changing the object state § Changing the speed § We do it by calling accelerate… § … or by assigning a new value to speed directly § Can you see any problem?! public class Main { public static void main (String[] args) { Aircraft boeing737 = new Aircraft(“Boeing”, 0, 2); boeing737.accelerate(840); boeing737.speed = -840; } }! 27 Encapsula0on § Access to a\ributes only through the methods! § Protects the object state public class Aircraft { private String manufacturer; private int speed; private int numberOfEngines; public void accelerate(int speed) { if ((speed >= 0) && (speed > this.speed)) { this.speed = speed; } } }! Protect your attributes! 28 Encapsula0on Private Properties Public Capacity Public Capacity Public Capacity Public Capacity Public Capacity Public Capacity Public Capacity Public Capacity From http://www.cs.brown.edu/courses/cs015/lectures.html 29 Now… § … nega0ve speeds are not allowed! public class Main { public static void main (String[] args) { Aircraft boeing737 = new Aircraft(“Boeing”, 0, 2); boeing737.accelerate(-840); boeing737.speed = -840; } }! 30 Abstrac0on § We don’t care about how the methods are implemented! § How does “accelerate” work? WE DON’T CARE! § How does “decelerate” work? Again, WE DON’T CARE! § So, we hide the strategy used in the implementa0on § Focus on “what” § Forget about “how” How What Procedural Programming versus Object-‐Oriented Programming 32 Array size example function doSomething() {! $array = array(23,12,25,32,33,53,21,2,...,89);! for ($i = 0; $i < count($array); $i++) {! ...! }! }! public class Vector { private Object[] elementData; public int size() { … } }! Exercises 34 Exercises § Think about an object § Define its proper0es § Define its capabili0es § Are the proper0es other objects? § If so, define its proper0es and capabili0es as well
Compartilhar