Ladda ner presentationen
Presentation laddar. Vänta.
1
Objektorienterad programmering d2
3 Object interaction Creating cooperating objects Collections of objects BK chap Sammankopling av objekt med pekare Pekarna i Java kallas referenser Abstraktion, modularisering med klasser DAT050, 17/18, lp 1
2
Objektorienterad programmering d2
Concepts abstraction modularization classes define types class diagram object diagram object references primitive types object types object creation internal/external method call object collections lists class libraries Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
3
Objektorienterad programmering d2
A digital clock Datarepresentation för hantering av klockslag Inget GUI här, men designen gör det lätt att införa senare Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
4
Abstraction and modularization
Objektorienterad programmering d2 Abstraction and modularization Abstraction is the ability to ignore details of parts to focus attention on a higher level of a problem. Modularization is the process of dividing a whole into well-defined parts, which can be built and examined separately, and which interact in well-defined ways. Modularisera och abstrahera från detaljerna i modulerna Abstraktionsnivå Användare kontra implementatör Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
5
Modularizing the clock display
Objektorienterad programmering d2 Modularizing the clock display One four-digit display? Or two two-digit displays? Försök hitta delsystem Undvik kodduplicering Generalisera! Undvik ad-hoc-lösningar Återanvändning, korrekthet, underhållsvänlighet. Abstrahera bort från timmar/minuter: Presentera två siffror i intervallet 0..limit-1 med wrap-around. (limit kan vara 12, 24, 60, …) Skilj på den numeriska hanteringen av talen och talens presentation som siffersträngar (numeraler). Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
6
Objektorienterad programmering d2
Object diagram Dynamisk vy Relationer på run-time-nivå mellan objekt Egentligen inte objekt utan referenser (pekare) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
7
Objektorienterad programmering d2
Class diagram Statisk vy Relationer på typnivå mellan klasser Kan instansieras med många olika objektdiagram Multiplicitet kan anges om man vill precisera Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
8
Implementation - NumberDisplay
Objektorienterad programmering d2 Implementation - NumberDisplay public class NumberDisplay { private int limit; private int value; Constructor and methods omitted. } Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
9
Implementation - ClockDisplay
Objektorienterad programmering d2 Implementation - ClockDisplay public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; Constructor and methods omitted. } objects Ett objekt kan använda andra objekt En klass definierar en datatyp En variabel av en klass kan innehålla objekt av klassen. classes define data types Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
10
Primitive types vs. object types
Objektorienterad programmering d2 & * Pekare i Java 00:00-24:00 Primitive types vs. object types object types reference variable ObjectType obj; object reference primitive types Jfr C En pekarvariabel innehåller en adress till ett objekt Javas referenser är egentligen pekare med restriktioner. ANIMERA object simple variable int i; 32 value Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
11
Objektorienterad programmering d2
What is the output? int a; int b; a = 32; b = a; a = a + 1; System.out.println(b); Person a; Person b; a = new Person("Nisse"); b = a; a.changeName(“Lisa"); System.out.println(b.getName()); Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
12
Primitive types vs. object types
Objektorienterad programmering d2 Primitive types vs. object types ObjectType a; ObjectType b; object b = a; A och b är alias för samma objekt int a; int b; 32 32 Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
13
Shallow copy vs. Deep copy
Objektorienterad programmering d2 Shallow copy vs. Deep copy Shallow copy ObjectType a; ObjectType b; object Deep copy ObjectType a; ObjectType b; Djup kopiering är lite mer komplicerat. Kloning… object object Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
14
Source code: NumberDisplay
Objektorienterad programmering d2 Source code: NumberDisplay public NumberDisplay(int rollOverLimit) { limit = rollOverLimit; value = 0; } public void increment() value = (value + 1) % limit; Konstruktor Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
15
Objektorienterad programmering d2
The modulo operator The 'division' operator (/), when applied to int operands, returns the result of an integer division. The 'modulo' operator (%) returns the remainder of an integer division. E.g., generally: / 5 gives result 3, remainder 2 In Java: / 5 == % 5 == 2 Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
16
Source code: NumberDisplay
Objektorienterad programmering d2 Source code: NumberDisplay public String getDisplayValue() { if(value < 10) { return "0" + value; } else { return "" + value; String är en standardklass i paketet java.lang Typomvandling till String Läs om String i JavaDoc! StringBuffer Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
17
Objects creating objects
Objektorienterad programmering d2 Objects creating objects public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; public ClockDisplay() hours = new NumberDisplay(24); minutes = new NumberDisplay(60); ... } new skapar ett nytt objekt och anropar klassens konstruktor ClockDisplay äger två NumberDisplay-objekt Separera I/O och interna beräkningar displayString lagrar utskriftsversionen av tiden Blanda inte ihop VAD något är internt med HUR det skall se ut! I det här fallet: Räkna med heltal – presentera som sträng Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
18
Objects creating objects
Objektorienterad programmering d2 Objects creating objects in class NumberDisplay: public NumberDisplay(int rollOverLimit); formal parameter in class ClockDisplay: Kör debuggern Öppna klockexemplet Öppna debugfönstret (ctrl-D) Sätt en brytpunkt vid den första måsvingen i konstruktorn i ClockDisplay som tar två parametrar Skapa ett objekt på objektbänken. Ange 8:45. Visa de olika panelerna i debugfönstret. Singelsteppa in i NumberDisplays konstruktor. visa anropsstacken. Steppa det andra konstruktoranropet utan att gå in i det. Continue Kolla objektet med Object inspector Kör setTime utan debugging. Sätt 9:59 Sätt en brytpunkt i timeTick och steppa igenom (det verkar som Object inspector inte uppdaterar fälten med viss fördröjning) hours = new NumberDisplay(24); actual parameter Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
19
ClockDisplay object diagram
Objektorienterad programmering d2 ClockDisplay object diagram TAVLAN ClockDisplay äger två NumberDisplay-objekt UML = Unified Modelling Language Rita klassdiagrammet på tavlan med aggregation och multiplicitet Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
20
Objektorienterad programmering d2
Method calling external method calls (objects of class NumberDisplay) public void timeTick() { minutes.increment(); if (minutes.getValue() == 0) { // it just rolled over! hours.increment(); } updateDisplay(); - i vilken klass finns denna metod? internal method call (timeTick and updateDisplay are methods in class ClockDisplay) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
21
Objektorienterad programmering d2
External method call external method calls minutes.increment(); object . methodName ( actual parameters ); Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
22
Objektorienterad programmering d2
Internal method call internal method calls updateDisplay(); methodName ( actual parameters ); No variable name is required this can be used as a reference to the invoking object, but not used for method calls. Anropande objekt Man kan också skriva this->updateDisplay(); OBS detta har inget att göra med att metoden är privat! Jfr C NumberDisplay *minutes; minutes = malloc(sizeof(NumberDisplay)); increment(minutes); Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
23
Objektorienterad programmering d2
Method calls NB: A method call on another object of the same type would be an external call. ‘Internal’ means ‘this object’. ‘External’ means ‘any other object’, regardless of its type. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
24
Private methods “help functions”
Objektorienterad programmering d2 Private methods “help functions” /** * Update the internal string that * represents the display. */ private void updateDisplay() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } Typisk intern hjälpfunktion Kan vara viktigt att den är privat. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
25
Objektorienterad programmering d2
Concept summary object creation overloading internal/external method calls debugger Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
26
Objektorienterad programmering d2
Grouping objects Class libraries - packages Collections of objects - Array lists Generic types Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
27
The requirement to group objects
Objektorienterad programmering d2 The requirement to group objects Many applications involve collections of objects: Personal organizers. Library catalogs. Student-record systems. The number of items to be stored varies. Items added. Items deleted. Automatisk minneshantering önskvärd Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
28
An organizer for music files
Objektorienterad programmering d2 An organizer for music files Track files may be added. There is no pre-defined limit to the number of files. It will tell how many file names are stored in the collection. It will list individual file names. Explore the music-organizer-v1 project. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
29
Objektorienterad programmering d2
Class libraries Collections of useful classes. We don’t have to write everything from scratch. Java calls its libraries, packages. Grouping objects is a recurring requirement The java.util package contains classes for various kinds of object collections. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
30
Objektorienterad programmering d2
import java.util.ArrayList; public class MusicOrganizer { // Storage for an arbitrary number of file names. private ArrayList<String> files; /** * Perform any initialization required for the * organizer. */ public MusicOrganizer() files = new ArrayList<String>(); } ... ArrayList är fältbaserad LinkedList baseras på dubbellänkad lista (Visa ArrayList I API-spec. senare) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
31
Collections of Objects
Objektorienterad programmering d2 Collections of Objects We specify: the kind of collection: ArrayList the type of objects it will contain: String private ArrayList<String> files; We say, “ArrayList of String”. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
32
Objektorienterad programmering d2
Generic classes Collection types are often parameterized or generic to make them as general as possible. ArrayList implements list functionality: add, get, size, etc. The type parameter says what we want a list of: ArrayList<Person> ArrayList<TicketMachine> etc. So if T is a type, then ArrayList<T> is a type as well. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
33
Creating an ArrayList object
Objektorienterad programmering d2 Creating an ArrayList object In versions of Java prior to version 7: files = new ArrayList<String>(); Java 7 introduced ‘diamond notation’ files = new ArrayList<>(); The type parameter can be inferred from the variable being assigned to. A convenience. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
34
Object structures with collections
Objektorienterad programmering d2 Object structures with collections Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
35
Objektorienterad programmering d2
Adding a third file Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
36
Features of the collection
Objektorienterad programmering d2 Features of the collection It increases its capacity as necessary. It keeps a private count: size() accessor. It keeps the objects in order. Details of how all this is done are hidden. Does that matter? Does not knowing how prevent us from using it? Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
37
Objektorienterad programmering d2
Using the collection public class MusicOrganizer { private ArrayList<String> files; ... public void addFile(String filename) files.add(filename); } public int getNumberOfFiles() return files.size(); Adding a new file Returning the number of files (delegation) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
38
Objektorienterad programmering d2
Index numbering Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
39
Objektorienterad programmering d2
Retrieving an object public void listFile(int index) { if(index >= 0 && index < files.size()) { String filename = files.get(index); System.out.println(filename); } else { // This is not a valid index. Index validity checks Retrieve and print the file name Needed? (Error message?) Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
40
Removal may affect numbering
Objektorienterad programmering d2 Removal may affect numbering Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
41
Objektorienterad programmering d2
Review Collections allow an arbitrary number of objects to be stored. Class libraries usually contain tried-and-tested collection classes. Java’s class libraries are called packages. We have used the ArrayList class from the java.util package. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
42
Objektorienterad programmering d2
Review Items may be added and removed. Each item has an index. Index values may change if items are removed (or further items added). The main ArrayList methods are add, get, remove and size. ArrayList is a parameterized or generic type. Uppgift Leta upp ArrayList i API-spec. Jämför med LinkedList. Objektorienterad programmering, DAT050, DAI2, 17/18, lp 1 DAT050, 17/18, lp 1
Liknande presentationer
© 2024 SlidePlayer.se Inc.
All rights reserved.