د.سناء الصايغ الفصل الأول البرمجة الشيئية

Slides:



Advertisements
Similar presentations
Chapter 13 - Inheritance. Goals To learn about inheritance To learn about inheritance To understand how to inherit and override superclass methods To.
Advertisements

CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look 1 Xiang Lian The University of Texas – Pan American Edinburg,
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Inheritance. Class Relationships Composition: A class contains objects of other class(es) (actually, references to such objects) –A “has a” relationship.
Chapter 13 Inheritance. An Introduction to Inheritance Inheritance: extend classes by adding methods and fields (variables) Example: Savings account =
CHAPTER 11 INHERITANCE CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance Part III. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Chapter 10  Inheritance 1 Chapter 10 Inheritance.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance Motivation –Code reuse –Conceptual modeling.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Inheritance.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Object-Oriented Programming in C++
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance CSC 171 FALL 2004 LECTURE 18. READING Read Horstmann, Chapter 11.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Topic 4 Inheritance.
CHAPTER 11 INHERITANCE. CHAPTER GOALS To understand how to inherit and override superclass methods To be able to invoke superclass constructors To learn.
Chapter 10 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
1 Chapter 3 – Object-Based Programming 2 Initializing Class Objects: Constructors Class constructor is a specical method to initialise instance variables.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Encapsulation ◦ Blackbox concept Data and method(s) Hidden details InterfaceEffect(s) methods called class.
Wednesday –POD –I have updated grades in powerschool. If you have a zero for a lab grade, it probably means you didn’t DropItToMe. Please do so. –Slides.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Inheritance INHERITANCE: extend existing classes by adding or redefining methods, and adding instance fields Suppose we have a class Vehicle: public class.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 12.
Chapter 13 Inheritance. Chapter Goals To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
CompSci 230 S Programming Techniques
Chapter 13 - Inheritance.
Lecture 3 John Woodward.
Lecture Notes – Inheritance (Ch 9-10)
Implementing Classes Yonglei Tao.
Computing with C# and the .NET Framework
The University of Texas Rio Grande Valley
CSC 205 Java Programming II
Chapter Three - Implementing Classes
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Anatomy of a Method.
Packages From Deitel & Deitel.
More on Classes and Objects
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look UTPA – Fall 2012 This set of slides is revised from lecture.
Adapted from Java Concepts Companion Slides
CS360 Client/Server Programming Using Java
JAVA CLASSES.
Introduction to Object-Oriented Programming
Java 1/31/2017 Back to Objects.
Presentation transcript:

د.سناء الصايغ الفصل الأول 2010- 2011 البرمجة الشيئية الكلية الجامعية للعلوم التطبيقية لغة برمجة حديثة (1) COMP 2301 د.سناء الصايغ الفصل الأول 2010- 2011 البرمجة الشيئية

البرمجة الشيئية الصنف والكائن (class and object) Control Statements تعتبر الأصناف (classes) وحدة البرنامج في لغة جافا. الصنف عبارة عن قالب (مخطط) يحتوي ويمثل الصفات للكائنات التي تنتمي لهذا الصنف. الكائن هو عبارة عن شيء حقيقي لواقع الحياة وهو ينبثق من كل صنف. بحيث يحتوي هذا الصنف على جميع صفات الكائنات التي تنتمي اليه.  Page 2 2

الصنف والكائن (class and object) Control Statements البرمجة الشيئية الصنف والكائن (class and object) مثال  Page 3 3

تعريف الصنف وتحديد مكوناته (class declaration) Control Statements البرمجة الشيئية تعريف الصنف وتحديد مكوناته (class declaration) يتم تعريف الأصناف عن طريق استخدام الكلمة المحجوزة class حيث يتبعها اسم الصنف. يتبع اسم الصنف قواعد التسمية نفسها التي يتبعها المتغير.  Page 4 4

تعريف الصنف وتحديد مكوناته (class declaration) Control Statements البرمجة الشيئية تعريف الصنف وتحديد مكوناته (class declaration) 3 // Time1 class declaration maintains the time in 24-hour format. 4 public class Time1 5 { 6 private int hour; // 0 - 23 7 private int minute; // 0 - 59 8 private int second; // 0 - 59 9 10 // set a new time value using universal time; ensure that 11 // the data remains consistent by setting invalid values to zero 12 public void setTime( int h, int m, int s ) 13 14 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); // validate hour 15 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); // validate minute 16 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); // validate second 17 } // end method setTime  Page 5 5

تعريف الصنف وتحديد مكوناته (class declaration) Control Statements البرمجة الشيئية تعريف الصنف وتحديد مكوناته (class declaration) 19 // convert to String in universal-time format (HH:MM:SS) 20 public String toUniversalString() 21 { 22 return String.format( "%02d:%02d:%02d", hour, minute, second ); 23 } // end method toUniversalString 24 25 // convert to String in standard-time format (H:MM:SS AM or PM) 26 public String toString() 27 { 28 return String.format( "%d:%02d:%02d %s", 29 ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ), 30 minute, second, ( hour < 12 ? "AM" : "PM" ) ); 31 } // end method toString 32 } // end class Time1  Page 6 6

الكائن (Object) Control Statements البرمجة الشيئية تتم عملية استخدام الأصناف عن طريق انشاء كائنات (objects) تكون على شكل نسخ من هذه الأصناف. تتم عملية انشاء النسخ على النحو التالي: تعريف متغير من نوع الصنف المراد استخدامه والذي تم تعريفه مسبقا انشاء كائن حقيقي من نفس الصنف باستخدام كلمة new. يتم التعامل مع الكائن باستخدام اسم المتغير الذي يشير اليه متبوعا بنقطة ثم احد المتغيرات او الطرق.  Page 7 7

الكائن (Object) – استخدام الصنف (time) Control Statements البرمجة الشيئية الكائن (Object) – استخدام الصنف (time) 2 // Time1 object used in an application. 3 4 public class Time1Test 5 { 6 public static void main( String args[] ) 7 { 8 // create and initialize a Time1 object 9 Time1 time = new Time1(); // invokes Time1 constructor 10 11 // output string representations of the time 12 System.out.print( "The initial universal time is: " ); 13 System.out.println( time.toUniversalString() ); 14 System.out.print( "The initial standard time is: " ); 15 System.out.println( time.toString() ); 16 System.out.println(); // output a blank line  Page 8 8

الكائن (Object) – استخدام الصنف (time) Control Statements البرمجة الشيئية الكائن (Object) – استخدام الصنف (time) 18 // change time and output updated time 19 time.setTime( 13, 27, 6 ); 20 System.out.print( "Universal time after setTime is: " ); 21 System.out.println( time.toUniversalString() ); 22 System.out.print( "Standard time after setTime is: " ); 23 System.out.println( time.toString() ); 24 System.out.println(); // output a blank line 25 26 // set time with invalid values; output updated time 27 time.setTime( 99, 99, 99 ); 28 System.out.println( "After attempting invalid settings:" ); 29 System.out.print( "Universal time: " ); 30 System.out.println( time.toUniversalString() ); 31 } // end main 32 } // end class Time1Test  Page 9 9

الصنف والكائن (class and object) Control Statements البرمجة الشيئية الصنف والكائن (class and object) الصنف هو القالب الذي نستخدمه في انشاء الكائنات Object وكل صنف له خصائصه attributes ومتغيراته Member variables وله سلوكه (الطرق) Member Methods. المنشآت (Constructs): يستخدم لانشاء الكائنات من صنف معين واعطاء قيم اولية لمتغيراته عند انشاءه اول مرة. وهي تشابه الطرق بالشكل ولكن تشترط فيها النقاط التالية: اسم المنشأ يجب انا يطابق اسم الفصيل مع مراعات حالة الأحرف (كبيرة او صغيرة). المنشأ لا يقوم بترجيع اي متغير لذا لا يتم تحديد نوع المتغير المرجع من المنشأ او حتى كتابة void.  Page 10 10

تعريف الصنف وتحديد مكوناته (class declaration) Control Statements البرمجة الشيئية تعريف الصنف وتحديد مكوناته (class declaration) public class BankAccount { // The first constructor is the default constructor sets balance to zero public BankAccount() { balance = 0; } // The second constructor sets balance to initial to initial balance public BankAccount(double initialBalance) { balance = initialBalance; // The deposit method adds an amount to instance variable balance public void deposit(double amount) { balance = balance + amount; // The withdraw method subtracts an amount from instance variable balance  Page 11 11

تعريف الصنف وتحديد مكوناته (class declaration) Control Statements البرمجة الشيئية تعريف الصنف وتحديد مكوناته (class declaration) public void withdraw(double amount) { balance = balance - amount; } // The transfer method withdraw an amount from this object and deposit // to other object balance public void transfer(BankAccount other , double amount) { withdraw(amount); other.deposit(amount); // The getBalance method returns the current balance public double getBalance() { return balance; // The instance variable balance private double balance; }  Page 12 12

الوراثة (inheritance) Control Statements البرمجة الشيئية الوراثة (inheritance) الوراثة (inheritance): الوراثة في لغة البرمجة هي عبارة عن خاصية تقوم فيها الأصناف الجديدة التي نقوم بتعريفها بوراثة متغيرات وطرق صنف آخر. حيث يعتبر الصنف الجديد امتدادا للصنف القديم الذي تمت وراثته. خصائص الوراثة: يتم الاستفادة من الطرق التي تم تعريفها مسبقا في فصائل أخرى مما يؤدي الى توفير الجهد. الفصيلة التي تورث تسمى الفصيلة العليا superclass لأنها تحتوي على الطرق والمتغيرات المشتركة. الفصيلة التي ترث تسمى الفصيلة الفرعية subclass لأنها تحتوي على الطرق والبيانات المضافة. لتحقيق عملية الوراثة نستخدم الكلمة المحجوزة extends ثم نذكر اسم الفصيل الذي نريد وراثته. عند عدم تحديد صنف نرث منه، تفترض لغة جافا انك ترث من الفصيلة الأم Object.  Page 13 13

الكائن (Object) – استخدام الصنف (time) Control Statements البرمجة الشيئية الكائن (Object) – استخدام الصنف (time) public class SavingAccount extends BankAccount // The SavingAccount constructor { public SavingAccount(double rate) { interestRate = rate; } // addInterest method public void addInterest() { double interest = getBalance()*interestRate/100; deposit(interest); // The SavingAccount instance variable private double interestRate;  Page 14 14

تمرين 1 Control Statements البرمجة الشيئية اكتب برنامجا يقوم بتعريف كائنين من الصنف BankAccount حيث يحتوي كل حساب (كائن) على مبلغ وقدره 5500 شيكل ثم قم باستخدام الطرق المتوفرة في الصنف لعمل التالي: ايداع مبلغ وقيمته 1000 شيكل في الحساب الأول. سحب مبلغ وقدره 2000 شيكل من كلا الحسابين. تحويل مبلغ وقدره 500 شيكل من الحساب الأول الى الحساب الثاني. طباعة كشف الحساب في لكلا الحسابين.  Page 15 15