مظفر بگ محمدی دانشگاه ایلام

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Polymorphism. Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[]) { double.
Comparable and Comparator Nuts and Bolts. 2 Nuts and bolts Four methods underlie many of Java’s important Collection types: equals, compare and compareTo,
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
26-Jun-15 Polymorphism. 2 Legal assignments Widening is legal Narrowing is illegal (unless you cast) class Test { public static void main(String args[])
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Comparable and Comparator. 2 Comparing our own objects The Object class provides public boolean equals(Object obj) and public int hashCode() methods –For.
CSE 11 February 11, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Object Oriented Design and UML
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
I NTERFACES Prepared by Miss Simab Shahid Lecturer computer Science and Software Engineering department, University of Hail Chapter.
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance, Abstract & Interface Pepper With help from and
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
PHY281 Scientific Java Programming ObjectsSlide 1 Classes & Objects In this section we will learn about Classes and Objects in Java :  What are Objects?
Interfaces and Inner Classes
1 Inheritance and Subclasses. 2 Inheritance Often we create very similar classes –Different types of triangles: equilateral, isosceles, etc. –Different.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Inheritance & Polymorphism Android Club Agenda Inheritance Polymorphism.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Polymorphism 2nd Lecture
Lecture 12 Inheritance.
Examples of Classes & Objects
Interface.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Abstract Class, Interface, Package
Inheritance.
Polymorphism 11-Nov-18.
More on Classes & Arrays
Comparable and Comparator
Polymorphism 2nd Lecture
مظفر بگ محمدی دانشگاه ایلام
CMSC 202 Interfaces.
Polymorphism 28-Nov-18.
Classes & Objects: Examples
CMSC 202 Polymorphism.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Comparable and Comparator
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Grouped Data Arrays, and Array Lists.
مظفر بگ محمدی دانشگاه ایلام
CMSC 202 Interfaces.
Polymorphism 15-Apr-19.
CIS 199 Final Review.
Polymorphism 21-Apr-19.
Sampath Kumar S Assistant Professor, SECE
Creating and Modifying Text part 3
TCSS 143, Autumn 2004 Lecture Notes
Topics OOP Review Inheritance Review Abstract Classes
CMSC 202 Interfaces.
Presentation transcript:

مظفر بگ محمدی دانشگاه ایلام واسط ها مظفر بگ محمدی دانشگاه ایلام

کلاسها و متدها وقتی که کلاسی متدهایش را بصورت عمومی تعریف می کند، نحوه ی تعامل کاربران کلاس را با متد توصیف می کند. متدهای عمومی واسط کلاس را تشکیل می دهند. کلاس انتزاعی یک یا چند متد دارد که فقط واسط دارند و دارای بدنه نیستند. جاوا اجازه می دهد این مفهوم را یک قدم بیشتر گسترش دهیم.

واسطها واسط چیزی شبیه کلاس انتزاعی است: تمام متدهای واسط انتزاعی هستند. واسط متغییر نمونه ندارد. واسط کلاس نیست. کلاسها می توانند یک یا چند واسط را پیاده کنند. نحوه ی تعریف واسط شبیه تعریف کلاس است. فقط بجای کلمه ی class از کلمه ی interface استفاده می کنیم.

واسطها واسط و تمام متدهای آن باید بصورت public تعریف شوند. چون واسط نیز یک نوع است، متد می تواند یک پارامتر از نوع واسط داشته باشد. این پارامتر آرگومانی را می پذیرد که از نوع کلاسی باشد که واسط را پیاده کرده است. 4

پیاده سازی واسط public interface Animal { public void eat( ); } برای ایجاد یک کلاس که تمام متدهای تعریف شده توسط واسط را پیاده می کند از کلمه ی کلیدی implements استفاده کنید. کلاس باید تمام متدهای واسط را پیاده کند. واسط سرآیند متدها را تعریف می کند و کلاسی که واسط را پیاده می کند بدنه ی متدها را تعریف می نماید. مثال واسط Animal: public interface Animal { public void eat( ); }

پیاده سازی واسط Animal public class Snake implements Animal // Lion and Snake implement the required eat( ) method public class Lion implements Animal { public void eat() { System.out.println("Lions Devour"); } } public class Snake implements Animal { System.out.println( "Snakes swallow whole"); }

پیاده سازی واسط Animal // Dog implements the required eat( ) method and has // some of its own methods and instance variables public class Dog implements Animal { private String name; public Dog(String newName) {name = newName;} public void eat() {System.out.println("Dog chews a bone");} } // Poodle is derived from Dog, so it inherits eat( ) // Adds a method of its own public class Poodle extends Dog { public Poodle( String name ) { super(name); } // call Dog constructor public String toString( ) { return "Poodle"; }

پیاده سازی واسط Animal // --- Output Lions Devour Dog chews a bone // Using classes that implement Animal public class Jungle { public static void feed( Animal a ) { a.eat(); } public static void main( String[] args ){ Animal[ ] animals = { new Lion( ), new Poodle( "Fluffy“ ), new Dog( "Max“ ), new Snake( ) }; for (int i = 0; i < animals.length; i++) feed( animals[ i ] ); } // --- Output Lions Devour Dog chews a bone Snakes swallow whole

گسترش یک واسط می توان با اضافه کردن متد به یک واسط موجود یک واسط جدید درست کرد که واسط قدیمی را گسترش می دهد. interface TiredAnimal extends Animal { public void sleep( ); } واسط TiredAnimal هم دارای متد eat( ) و هم متد sleep( ) است.

واسط Comparable واسط Comparable جزیی از بسته ی java.lang است و بصورت خودکار به برنامه اضافه می شود. این واسط فقط یک متد دارد. به پارامتر Object دقت کنید: public int compareTo(Object other); رعایت ملزومات واسط اجباری نیست و این وظیفه ی برنامه نویس است که واسط Comparable را درست پیاده کند. هنگام پیاده سازی compareTo باید آنرا بصورت مناسب بارگذاری کنید. 10

واسط Comparable مقدار بازگشتی متد compareTo باید: یک عدد منفی باشد، اگر شی فراخوانی کننده قبل از پارامتر other باشد. صفر باشد، اگر شی فراخوانی کننده مساوی پارامتر other باشد. یک عدد مثبت باشد، اگر شی فراخوانی کننده بعد از پارامتر other باشد. اگر پارامتر other و شی فراخوانی کننده هم نوع نباشند، خطای ClassCastException ارسال خواهد شد. 11

متد compareTo برای Person public class Person implements Comparable { private String name; ... public int compareTo( Object obj ) Person p = (Person)obj; return name.compareTo(p.name); } .... If obj is not a Person object a ClassCastException will be thrown Comparing the names using String’s compareTo method

استفاده از Comparable // prints the index of the smallest Integer in an array // Note use of Integer, not int public class FindSmallest { public static void main( String[ ] args) { // find the smallest Integer in an array // Integer (implements Comparable ) int index = 0; // index of smallest value Integer[ ] values = { new Integer(144), new Integer(200), new Integer(99), new Integer(42), new Integer(132) }; for (int i = 1; i < values.length; i++) if ( values[ i ].compareTo( values[ index ] ) < 0 ) index = i; } System.out.println("Index of smallest value is “ + index);

پیاده کردن چندین Interfaces public interface Animal { public void eat( ); } public interface Cat void purr( ); // public by default; // since a Lion is an Animal and a Cat, Lion may wish // to implement both interfaces public class Lion implements Animal, Cat public void eat( ) {System.out.println(“Big Gulps”);} public void purr( ) {System.out.println(“ROOOAAAR!”);} Just separate the Interface names with a comma

واسطهای ناسازگار در جاوا هر کلاس فقط می تواند یک کلاس پایه داشته باشد. تا دو متد با سرآیند یکسان، تعاریف متعدد نداشته باشند. اما هر کلاس می تواند چندین واسط را پیاده کند. چون که متدهای واسط بدنه ندارند، این موضوع ایرادی ندارد. اما مشکلات دیگری وجود دارد. اگر واسطها دو متد با اسم یکسان، پارامترهای یکسان و مقدار برگشتی متفاوت داشته باشند، مشکل پیش می آید. اگر کلاسی دو واسط ناسازگار را پیاده کند، خطا تولید شده و تعریف کلاس غیرقانونی است. 15