انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية تكنولوجيا المعلومات
القسم قسم البرامجيات
المرحلة 2
أستاذ المادة احمد خلفة عبيد العجيلي
09/03/2019 10:11:26
Object-oriented programming with Java Ahmed Al-Ajeli 1 Understanding class definitions Exploring source code Ahmed Al-Ajeli Lecture 2 2 Main concepts to be covered •fields •constructors •methods •parameters •assignment statements Object-oriented programming with Java Ahmed Al-Ajeli 2 3 Ticket machines – an external view •Exploring the behaviour of a typical ticket machine. –Use the naive-ticket-machine project (We will assume the customer is honest) –Machines supply tickets of a fixed price. •How is that price determined? –How is ‘money’ entered into a machine? –How does a machine keep track of the money that is entered? 4 public class TicketMachine { // The price of a ticket from this machine. private int price; // The amount of money entered by a customer so far. private int balance; // The total amount of money collected by this machine. private int total; public TicketMachine(int cost) { price = cost; balance = 0; total = 0; } Demo of naïve-ticket-machine Object-oriented programming with Java Ahmed Al-Ajeli 3 5 public int getPrice() { return price; } public int getBalance() { return balance; } public void insertMoney(int amount) { balance = balance + amount; } public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The Eclipse Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); total = total + balance; balance = 0; } } 6 Ticket machines – an internal view •Interacting with an object gives us clues about its behaviour. •Looking inside allows us to determine how that behaviour is provided or implemented. •All Java classes have a similar-looking internal view. Object-oriented programming with Java Ahmed Al-Ajeli 4 7 Basic class structure public class TicketMachine { Inner part omitted. } public class ClassName { Fields Constructors Methods } The outer wrapper of TicketMachine The inner contents of a class 8 Keywords •Words with a special meaning in the language: –public –class –private –int •Also known as reserved words. •Always entirely lower-case. Object-oriented programming with Java Ahmed Al-Ajeli 5 9 Fields •Fields store values for an object. •They are also known as instance variables. •Fields define the state of an object. •Some values change often. •Some change rarely (or not at all). public class TicketMachine { private int price; private int balance; private int total; Further details omitted. } private int price; visibility modifier type variable name 10 Constructors •Initialize an object. •Have the same name as their class. •Public visibility (since called by the user) •Close association with the fields: –Initial values stored into the fields. –Parameter values often used for these. public TicketMachine(int cost) { price = cost; balance = 0; total = 0; } Object-oriented programming with Java Ahmed Al-Ajeli 6 11 Constructing objects -Classname objectname = new Classname (parameters list); -TicketMachine ticketMachine1= new TicketMachine (500); •What will happen? - The new operator makes a TicketMachine object. - It invokes (calls) the constructor - It returns the object. pattern example 12 Assignment •Values are stored into fields (and other variables) via assignment statements: –variable = expression; –balance = balance + amount; •A variable can store just one value, so any previous value is lost. pattern example Object-oriented programming with Java Ahmed Al-Ajeli 7 13 Choosing variable names •There is a lot of freedom over choice of names. Use it wisely! •Choose expressive names to make code easier to understand: –price, amount, name, age, etc. •Avoid single-letter or cryptic names: –w, t5, xyz123 14 Methods •Methods implement the behaviour of objects. •Methods have a consistent structure comprised of a header (signature) and a body. •Accessor methods provide information about an object. •Mutator methods alter the state of an object. •Other sorts of methods accomplish a variety of tasks. Object-oriented programming with Java Ahmed Al-Ajeli 8 15 Method structure •The header: –public int getPrice() •The header tells us: –the visibility to objects of other classes; –whether the method returns a result; –the name of the method; –whether the method takes parameters. •The body encloses the method’s statements. 16 Calling (activating) methods •Calling a method within an object is similar to sending a message to that object. - Objectname.methodname (parameter list) - ticketmachine1.getPrice() pattern example Actual parameters Object-oriented programming with Java Ahmed Al-Ajeli 9 17 Accessor (get) methods public int getPrice() { return price; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier 18 Accessor methods •An accessor method always has a return type that is not void. •An accessor method returns a value (result) of the type given in the header. •The method will contain a return statement to return the value. •NB: Returning is not printing! Object-oriented programming with Java Ahmed Al-Ajeli 10 19 Test •What is wrong here? public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } (there are five errors!) 20 Test public class CokeMachine { private price; public CokeMachine() { price = 300 } public int getPrice { return Price; } } ; () int - •What is wrong here? (there are five errors!) Object-oriented programming with Java Ahmed Al-Ajeli 11 21 Mutator methods •Have a similar method structure: header and body. •Used to mutate (i.e., change) an object’s state. •Achieved through changing the value of one or more fields. –They typically contain one or more assignment statements. –Often receive parameters. 22 Mutator methods public void insertMoney(int amount) { balance = balance + amount; } return type method name formal parameter visibility modifier assignment statement field being mutated Object-oriented programming with Java Ahmed Al-Ajeli 12 23 set mutator methods •Fields often have dedicated set mutator methods. •These have a simple, distinctive form: –void return type –method name related to the field name –single formal parameter, with the same type as the type of the field –a single assignment statement 24 A typical set method public void setDiscount(int amount) { discount = amount; } We can easily infer that discount is a field of type int, i.e: private int discount; Object-oriented programming with Java Ahmed Al-Ajeli 13 25 Protective mutators •A set method does not have to always assign unconditionally to the field. •The parameter may be checked for validity and rejected if inappropriate. •Mutators thereby protect fields. •Mutators support encapsulation. 26 String concatenation •4 + 5 9 •"wind" + "ow" "window" •"Result: " + 6 "Result: 6" •"# " + price + " cents" "# 500 cents" overloading Object-oriented programming with Java Ahmed Al-Ajeli 14 27 Quiz •System.out.println(5 + 6 + "hello"); •System.out.println("hello" + 5 + 6); 11hello hello56
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|