Перейти к содержимому

ג'אוה - המרה כלפי מעלה/מטה (Upcasting, Downcasting) בירושה, אפשר לשחק כדורגל אבל רק מה שההורה מרשה!!

חן רון – אסטרטגיה של תלמידים מצטיינים

0:00 / 0:00

ג'אוה - המרה כלפי מעלה/מטה (Upcasting, Downcasting) בירושה, אפשר לשחק כדורגל אבל רק מה שההורה מרשה!!

230 просмотров · 3 г. назад
חן רון – אסטרטגיה של תלמידים מצטיינים
411 подписчиков
230 просмотров · 3 г. назад
לא תמיד קל לתפוס ברגע הראשון למה הכוונה ומה המשמעות ב-upcasting (המרה כלפי מעלה) וב-Downcasting (המרה כלפי מטה) בשפת תכנות ג'אוה. דבר ראשון צריך להבין שמה שלמעלה זה ההורה ומה שלמטה זה מחלקות הילד. בדרך כלל אם אנו בונים מחלקת ילד אז מה שמפנה אליה, הרפרנס (המצביע) הוא מאותו סוג של קלאס של ילד, אבל אם אנחנו נעשה משהו שונה ונגיד שדווקא המצביע הפעם הוא מהקלאס של ההורה, אז מה עשינו בזה? נראה שיצרנו לעצמנו חיסרון, מפני שבצורה כזאת עכשיו אי יהיה אפשר להפעיל על האובייקט הזה את המתודות שלו שיחודיות אליו, למחלקה הילד שלו. ואנחנו נאלץ להסתפק ולהשתמש רק במתודות של מחלקת ההורה, והכל בגלל שמצביע של מחלקת ההורה בוא מה שמצביע אליו. בכל זאת יש בכך יתרונות, וזה שאם ניצור מערך שכולו פריטים לכאורה של מחלקת ההורה, אז נוכל לשים שם גם אובייקטים של מחלקת הילד ולהריץ על הכל עם לולאה איזשהו קוד שמיישם מתודות שקיימות אצל הורה, וזה נראה יעיל, אם באמת אנו רק מתעניינים במתודות שקיימות אצל ההורה. אבל מה אם עכשיו אנו מתחרטים ורוצים דווקא כן להפעיל מתודות של האובייקט הזה שקיימות אצלו ספציפית בגלל שהוא ממחלקת הילד הכוללת מתודות מסויימות יחודיות שאנו רוצים להפעיל? עכשיו נכנס לפעולה הנושא של Downcasting, המרה למטה, ועכשיו אם נשתמש בתחביר מיוחד שכולל שני זוגות של סוגריים, ונרשום בסוגריים שם את השם של מחלקת הילד, המחשב יאפשר לנו לשוב ולקרוא למתודות של מחלקת הילד, למרות שה-reference שמצביע אליו הוא רפרנס של מחלקת ההורה. אין על שפת תכנות Java! Upcasting and downcasting in Java is when, as creating child classes (subclasses) using the word "new", you decide to do something different with the reference that will point to this new object. You decide to have the reference be of the type of the parent class, instead of the type of the child class. This is upcasting! However it entails some disadvantages. Most prominently, now you will not be able to use the methods and parameters of the child class that was not inherited from the parent. This limits your ability to do things with the object as you could have done if the reference to it was of the kind of the subclass itself. However, there are advantages to this upcasting coding style. Most notably, you can lump together in an array many objects of different subtypes, declare this array to be one of the type of the parent class, and if you are interested in running some code on these objects that will do things that are related only to the methods that were inherited from the parent class, then you'll be fine and actually you prove yourself to be efficient. Downcasting: now, if you changed your mind, and after creating the objects using upcasting, you now want to go back and actually reach and use the methods that are unique to the subclass, here a certain esoteric syntax will be needed and using this weird formula, which makes use of two sets of parentheses, will make the magic, and you could finally call and use the methods and parameters that are emergent from the subclass, too.