המצגת נטענת. אנא המתן

המצגת נטענת. אנא המתן

עקרונות תכנות מונחה עצמים

מצגות קשורות


מצגת בנושא: "עקרונות תכנות מונחה עצמים"— תמליל מצגת:

1 עקרונות תכנות מונחה עצמים
תרגול 1

2 Administration 70% - מבחן. 30% - 4 עבודות ב-JAVA.
70% - מבחן. 30% - 4 עבודות ב-JAVA. הגשה בזוגות דרך ה-Submission System 60

3 Our Plan UML class diagrams Packages, Classpath, Jar Files,
Testing & 8-Queens Problem Collections & Wrappers Messages, Instances and Initialization C++ - Subclasses, static & dynamic behavior, multiple inheritance and overloading. Design Patterns Generics

4 Today First part Packages Class Path JAR Files Second part
UML Class Diagrams

5 java.util.List ≠ java.awt.List!
Packages Packages are the Java mechanism to prevent name collisions between classes. java.util.List ≠ java.awt.List! Package rules: Each class belongs to a package (which will hold related classes) Package may have sub-packages A package path is dot-separated foo.goo.Boo Class Boo, sub-package goo, package foo Sub-packages has no special properties or privileges

6 Packages Importing classes from other packages
Package declaration (In the beginning of the class) package foo.goo; class Boo {...} Importing classes from other packages import java.util.Array // Import only the Array class import java.util.*; // All classes in java.util

7 Finding a class When looking for a class, the JRE looks for a .class file in directories that match the package path. Intuitively, Sub-package = Sub-directory. The root directory is the classpath. If, for example, we want to find the class foo.goo.Boo in the directories tree +-classpath |-... +-foo +-goo |-X.class |-Y.class |-Boo.class

8 Classpath Classpath is a parameter—set either on the command-line, or through an environment variable—that tells the Java Virtual Machine or the Java compiler where to look for user-defined classes and packages. A classpath can contain several roots. Finding the previous class in the Classpath= “A:B:C” Root A +-A +-fi +-fy |-Fum.class |-... Root B +-B |-... +-foo +-goo |-X.class |-Y.class Root C +-C |-... +-foo +-goo |-Z.class |-Boo.class

9 Classpath (Intellij) In you Intellij IDE: Click on “File”
Select “Project structure” Select “Modules” Switch to tab “Dependencies” Click the “+” button on the right and select “Jars or directories” Find your path and click OK If you get a dialog with "Choose Categories of Selected File", choose Classes (even if it's properties), press OK and OK again

10 Classpath (Eclipse)

11 JAR Files Jar files are the Java equivalent to libraries and executables. A jar file is a zip archive that contains: .class files Other needed resources (images, config files) A manifest file (optional) “MANIFEST.MF” Source files (optional)

12 The Manifest File The manifest file is a text file with key:value properties. The JRE reads these properties when it loads the .jar file. Important properties in a jar manifest: Manifest-Version: For backward compatibility. (e.g Manifest-Version: 1.0) Main-Class: The class containing the main method to run. (e.g Main-Class: a.b.c.Foo) Class-Path: Space-separated list of other class paths to use. (e.g Class-path: mylib1.jar mylib2.jar) See for details on jar and manifest specifications.

13 Using JAR Files Jar files as Java shared libraries
Including a jar file in a classpath will cause the JRE to look in the archive for classes and resources, as if it was another directory. Jar files as Java executables You can run a jar file by running: java -jar hello-world.jar (Or double-click on it, in some systems) The Java Runtime will load all the content of the jar, including its classpath, and will run the main class that is written in the manifest.

14 Creating JAR Files Creating a jar file using the command-line tool: jar cf <jar-file> <manifest-file> files . . .

15 Or do it with Eclipse

16 Or Intellij (Better!) After adding the module (and possibly the main class), select OK.

17 Or Intellij (Better!) - If you want to add your source code to the jar file, click on the ‘+’ icon and choose “module Sources”. Once done, click OK. - Select the “Build” tab from the intellij menu and choose “Build Artifacts”, then select “Build” - You can find your jar file in the “out” folder. If you made changes to your code, simply choose “Rebuild”.

18 Or Intellij (Better!) Once your jar is created, you can verify the content of this by right-clicking and opening it with 7-Zip or Winrar, just like any other compressed file. You can also run the jar file from your command line, simply type: cd *your-jar’s-directory* java –jar *your-jar’s-name*

19 Unified Modeling Language (UML)

20 What is a model? An abstraction of software features
A structure that singles out selected features Independent from other software elements Usually visual (diagrammatic) רכיב המייצג תכונות שונות של התוכנה שלנו, לבחירתנו )על פי דרישות התוכנה/לקוח(.  לרוב, אובייקט = מחלקה, מייצג עבורנו מודל. אך יתכן שמס' מחלקות ביחד ייצגו מודל  אחד. לדוגמא: אובייקט מחשבון , מכיל פונקציות שונות של חישוב.  בדרך כלל המודל שלנו ניתן לייצוג בצורה דיאגרמות.

21 UML The Unified Modeling Language (UML) is a widely accepted standard for software specification and design. It’s a modeling Language. Why do engineers build models? To understand To communicate To predict To specify (Blueprints) Building models is realized by selecting statements through abstraction, i.e., reduction of information preserving properties relative to a given set of concerns. שפה סטנדרטית לייצוג המודלים השונים שלנו בתוכנה )והיחסים ביניהם( על ידי דיאגרמות -UML שונות. בשלב ניתוח הדרישות של התוכנה ועיצובה, נעשה שימוש בשפה זו כדי לאפיין את המערכת שלנו. .blueprints לדוגמא: שקול לבנייה של בניין, ותכנונו על ידי שרטוטים שונים ו  מוטיבציה לבנייה של מודלים: הבנה: להבין את המערכת שלנו בצורה טובה יותר.  תקשורת: לתקשר בין המפתחים של התוכנה )להציג את המערכת לאנשים מבחוץ(.  לחזות: לחזות דברים שונים על תהליך הפיתוח ועל המערכת עצמה  כמה משאבים תהליך הפיתוח ידרוש )זמן, כוח אדם, מערכות שונות( o פונקציונליות המערכת. o .(blueprints( לפרט: את אופן המימוש של התוכנה From: Bran Selic, UML2 MoDELS 2012

22 UML Class Diagram

23 UML + OCL (Object Constraint Language)

24 Class Class – An entity that defines: Properties. Behavior.
Relationships to other classes. Semantics. Object is an instance of a class. Some classes may not have instances (static classes). For example, Math. For Example: Information

25 Class Visibility Circle - centreX:Int - centreY:Int + draw()
Access Level: Public – ‘+’, Accessed by everyone Protected – ‘#’, Accessed by self, subclasses and members of the same package Private – ‘-’, Accessed by self only For Example: - centreX:Int - centreY:Int Circle + draw() # move(Int X, Int Y)

26 Abstract Class Name* Car * Fields Non Abstract Methods
- engineSize : int - maxSpeed : int # getMaxSpeed() : int + drive() *

27 UML Class Diagrams Shows the classes of a system:
Their interrelationships: Association Generalization (Inheritance, Subtyping, Extends, is a) Realization (Interface, Implements) Their Constraints

28 Association Association is a relationship that describes a logical link between two classes. For example, a person works for a company: This type of association is a plain association. Role employee employer Person Company works for Association Name

29 Association - Multiplicity
A Student can take up to five Courses. Student has to be enrolled in at least one course. Up to 300 students can enroll in a course. A class should have at least 10 students. Student Course takes 1..5

30 Association Common mistakes Association VS attribute
Associations are the essence of object-oriented modeling. Connections between objects are always modeled as links. Attributes provide additional (secondary) information about identified objects. Attribute heuristic rule: If it’s important it’s not an attribute!

31 Association Common mistakes
Roles VS Objects A Person has exactly two Parents and any number of children

32 Generalization Inheritance relationship.
With this relationship, we can collect the properties and services which are shared between some classes and extract them to a class called a SuperType Every class that was a part of this process becomes a SubType and inherits the properties, services and relationships from the SuperType class (and of course can extend them).

33 Generalization - Example
חברה מעסיקה טכנאים – חלקם מומחים וחלקם מתמחים. לכל מתמחה יש לפחות מנחה אחד. מתמחה יכול להיות מנחה של מתמחה אחר כל עוד אין מעגליות בתחום ההתמחות. כללים: לכל מתמחה יש רק מנחה אחד מנחה יכול להנחות מתמחים אחרים שיכולים להנחות אותו. יתכנו מומחה ומתמחה באותה הדרגה.

34 <<interface>> List
Realization A realization relationship indicates that one class implements a behavior specified by some interface An interface can be realized by many classes A class may realize many interfaces LinkedList <<interface>> List LinkedList List ArrayList

35 Static modeling – Advanced associations
UML allows us to model relationships between classes. Allows us to statically model a realistic state of the system and makes it simpler to understand the model. Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist. Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House.

36 Tools Web based UML tool

37 Example לחברה יש בין 20 עד 120 עובדים. כל עובד מועסק בחברה אחת בלבד.
ישנם 2 סוגים של עובדים: פשוט ובכיר. לכל עובד פשוט יש מנהל אחראי שהוא עובד בכיר. כל עובד בכיר יכול להיות מנהל ל0 עד 5 עובדים פשוטים.

38 Solution


הורד את "ppt "עקרונות תכנות מונחה עצמים

מצגות קשורות


מודעות Google