[at]السلام عليكم
دي فيديوا يشرح موضوع مهم جدااا وهوه Generic ودي احد المزيا الي انضافت في الجافا من اول اصدار 1.5
[at]السلام عليكم
دي فيديوا يشرح موضوع مهم جدااا وهوه Generic ودي احد المزيا الي انضافت في الجافا من اول اصدار 1.5
SCJP
SCBCD Soon
جزاكم الله خيرا
انا كنت قرات عن الموضوع دا بس مش كنت فاهم اوى
لكن بعد الشرح الحمدلله بدات الى حد ما افهمها كويس
عاوزه بس شويه تطبيق
بس سوالى
امتى اعمل Generic خاص بى ؟؟
فى حالات ايه ؟
لو مثال صغير ان امكن
وفققك الله الى ما يحبه ويرضاه
اقتباسامتى اعمل Generic خاص بى ؟؟
في معظم الوقت كل ماعليك هو أن تعرف كيف تتعامل مع ال Generics ...
لكن أحيانا , مثلا لو طلب منك عمل container مثلا stack مثلا,,, أو عمل generic method ... فستطر لكتابة generics نفسك, و كما تعلم, الموضوع ميكانيكيا (plumbing code)
مثال مثلا على generic method :
<T> T getObject(ObjectMapping objectMapping, String clazzName)
{
List<T> list = new ArrayList<T>();
// .... other code here..
//.........
return list.get(0);
}أنت ستقوم بإرجاع أي كائن من النوع الذي سوف يستخدم في ال ArrayList المسمي list
تم تعديل هذه المشاركة بواسطة هويدي في 16 أغسطس 2010 في 01:37
ماشى يا عمووو هويدى
شكرا يا غالى
جزاكم الله خيرا ع الافاده
يعنى اللى انا فهمته انه لم احتاج اعمل حاجه خاصه بيا
يعنى مثلا عاوز اعمل ArrayList من class انا اللى عاملها
وعاوز اعمل ليها الدوال الخاصه بيها وكدا
صح و لا انا كدا غلطان ؟؟
تمام كلامك ياباشمهندس محمود
وزي مقال مشرف القسم تماما واحب اضيف
ان الاستخدام الي قبل كده لل collections اني بحط فيها مجموعه غير متجانسه من elements لان methods المسؤله علي اضافه هذه elements كانت بتضيف حاجه من نوع Object ودي الي اطلقنا عليها بعده كده Raw Type
لما بستخدم generic قدرت اعمل container يحتوي علي elements من نفس النوع او متجانسه مش بس كده ممكن باستخدام wildcard احط شروطي في الاضافه لهذا container ان مثلا elements الي هتنضاف دي تكون من نوع كلاس معين او تكون من نوع Class بيرث كلاس معين اي كلاس هوه superclass بالنسبه للكلاس الي محدده
There are three types of wildcards: 1. "? extends Type": Denotes a family of subtypes of type Type. This is the most useful wildcard 2. "? super Type": Denotes a family of supertypes of type Type 3. "?": Denotes the set of all types or any
As an example of using wildcards, consider a draw() method that should be capable of drawing any shape such as circle, rectangle, and triangle. The implementation may look something like this. Here Shape is an abstract class with three subclasses: Circle, Rectangle, and Triangle.
public void draw(List<Shape> shape) { for(Shape s: shape) { s.draw(this); } }It is worth noting that the draw() method can only be called on lists of Shape and cannot be called on a list of Circle, Rectangle, and Triangle for example. In order to have the method accept any kind of shape, it should be written as follows:
public void draw(List<? extends Shape> shape) { // rest of the code is the same }Here is another example of a generics method that uses wildcards to sort a list into ascending order. Basically, all elements in the list must implement the Comparable interface.
public static <T extends Comparable<? super T>> void sort(List<T> list) { Object a[] = list.toArray(); Arrays.sort(a); ListIterator<T> i = list.listIterator(); for(int j=0; j<a.length; j++) { i.index(); i.set((T)a[j]); } }SCJP
SCBCD Soon
ممكن سؤال
عندي المثال
List<? extends Number> foo3 = new ArrayList<Integer>(); foo3.add(new Integer(5) );
لماذا لا اقدر اضافة new Integer