انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة
الكلية كلية تكنولوجيا المعلومات
القسم قسم البرامجيات
المرحلة 2
أستاذ المادة احمد مهدي محمد سعيد الصالح
31/01/2016 20:40:47
Conversions and Overloading
First. Java allows certain implicit conversations of a value of one type to a value of another type. Implicit conversations involve only the primitive types. For example, Java allows chars to be widened to the numeric types. Thus, the assignment to n in the following is legal:
char c=’A’;
int n=c;
Second, conversations involve computation, that is, they cause a production of a new type (of the variable types) that is then assigned to the variable.
After the compiler determines the conversation needed to make the assignment legal, it generates the code needed to produce the new value.
Overloading In some circumstances, you might want to write several method in the same class that do the same job with different arguments. If we want method to return different data types such as int, float and String types. public void println(int i) public void println ( float f) public void println ( String str) The determination of type correctness is actually not as simple as described previously, for two reasons. In addition, Java allows overloading. This means that there can be several methods with the same name. For example, consider a class C with the following methods: Public class c { static int comp( int i, long j) // def1. static float comp( long i, int j) // def 2 static int comp (long i, long j) // def3 }
This class provides three overloaded definitions of comp. when there are overloaded definitions, several of them might work for a particular call. For example, suppose you have the declarations: int x; long y; float z;
In Java, an int (32 bits ) can be widened to a long (64 bits), and also a float (32 bits ) can be widened to a long (64 bits), Therefore a call C.comp(x, y) could go to either the first definition of comp (since here the types match exactly) or the third definition of comp (by widening x to a long). The second definition is not possible since it isn’t possible to widen a long to an int.
The rule used to determine which method to call when there are several choices, as in this example, is “most specific”. A method m1 is more specific than another method m2 if any legal call of m1 would also be a legal call of m2 if more conversations were done.
For example, the first definition of comp would be selected for the call C.comp(x,y) since it is more specific than the third definition. Note: If there is no most specific method, a compile time error occurs for example, all three definitions matches for the call C.comp(x,x). however, none of these is most specific and therefore the call is illegal . The programmer can solve the ambiguity in case like this by making the conversation explicitly, for example C.comp ((long) x,x) select the second definition.
Generally, two rules apply to overloaded methods: a- Arguments list must differ b- Return types can be different. Suppose we have the following example: Constructor Overloading As with methods, constructors can be overloaded. • Argument lists must differ. • You can use the this reference at the first line of a constructor to call another constructor.
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
|