السلام عليكم
ما فائدة من انشاء اوبجكت او باراميتر من نوع abstract class ؟
و ما الفرق بين انشاء اوبجكت من نوع كلاس abstract
او انشاؤه من نوع الكلاس الابن subclass ؟؟
السلام عليكم
ما فائدة من انشاء اوبجكت او باراميتر من نوع abstract class ؟
و ما الفرق بين انشاء اوبجكت من نوع كلاس abstract
او انشاؤه من نوع الكلاس الابن subclass ؟؟
تم تعديل هذه المشاركة بواسطة ηaseemЈava في 9 نوفمبر 2013 في 14:02
هل تعرفين الواجهة interface وفائدتها؟ الـabstract class هو interface + وظيفة.
لو كان عندك برنامج للتعامل مع الصور وفيه فئتين Png و Gif (صيغ صور) كل منهم يستخدم الواجهة Image ويوفران وظيفة اسمها getPixels لعدد البكسلات في الصورة (العرض × الإرتفاع):
interface Image {
public int getPixels();
}
class Png implements Image {
private int width;
private int height;
Png(int width, int height) {
this.width = width;
this.height = height;
}
public int getPixels() {
return this.width * this.height;
}
}
class Gif implements Image {
private int width;
private int height;
Gif(int width, int height) {
this.width = width;
this.height = height;
}
public int getPixels() {
return this.width * this.height;
}
}
public class Test {
public static void main(String[] args) {
Png png = new Png(10, 20);
Gif gif = new Gif(20, 20);
printNumberOfPixels(png);
printNumberOfPixels(gif);
}
public static void printNumberOfPixels(Image image) {
System.out.printf("number of pixels = %d\n", image.getPixels());
}
}الوظيفة printNumberOfPixels تفبل أي فئة تستخدم الواجهة Image، لايهما ماهي الفئة ومانوعها المهم أنها توفر وظيفة اسمها getPixels.
في كل الفئتين، الوظيفة getPixels نفس الشيء، مضيعة للوقت أن نعيد كتابة نفس الكود في كل فئة، الحل الوسط بين الفئة والواجهة هو الـabstract class، توفر ميزة الواجهة مع الفئة العادية، يمكن إعادة كتابة الكود باستخدام الـabstract class هكذا:
abstract class Image {
protected int width;
protected int height;
Image(int width, int height) {
this.width = width;
this.height = height;
}
public int getPixels() {
return this.width * this.height;
}
}
class Png extends Image {
Png(int width, int height) {
super(width, height);
}
}
class Gif extends Image {
Gif(int width, int height) {
super(width, height);
}
}
public class Test {
public static void main(String[] args) {
Png png = new Png(10, 20);
Gif gif = new Gif(20, 20);
printNumberOfPixels(png);
printNumberOfPixels(gif);
}
public static void printNumberOfPixels(Image image) {
System.out.printf("number of pixels = %d\n", image.getPixels());
}
}تم تعديل هذه المشاركة بواسطة Mr.B في 9 نوفمبر 2013 في 21:50
هل بحثت في المنتدى قبل طرح السؤال ؟
انظر :
ما هو الفرق بين Abstract Class و Interface Class
ماهو ال abstract و ال interface
الموضوع مُكرر, يُرجى عدم تكرار المخالفة !
C++ and Java, say, are presumably growing faster than plain C, but I bet C will still be around. ― Dennis Ritchie
~OoO~________--------------------------------------------________~OoO~
من مواضيعي :
Mr.B كتب:
هل تعرفين الواجهة interface وفائدتها؟ الـabstract class هو interface + وظيفة.
لو كان عندك برنامج للتعامل مع الصور وفيه فئتين Png و Gif (صيغ صور) كل منهم يستخدم الواجهة Image ويوفران وظيفة اسمها getPixels لعدد البكسلات في الصورة (العرض × الإرتفاع):
interface Image { public int getPixels(); } class Png implements Image { private int width; private int height; Png(int width, int height) { this.width = width; this.height = height; } public int getPixels() { return this.width * this.height; } } class Gif implements Image { private int width; private int height; Gif(int width, int height) { this.width = width; this.height = height; } public int getPixels() { return this.width * this.height; } } public class Test { public static void main(String[] args) { Png png = new Png(10, 20); Gif gif = new Gif(20, 20); printNumberOfPixels(png); printNumberOfPixels(gif); } public static void printNumberOfPixels(Image image) { System.out.printf("number of pixels = %d\n", image.getPixels()); } }الوظيفة printNumberOfPixels تفبل أي فئة تستخدم الواجهة Image، لايهما ماهي الفئة ومانوعها المهم أنها توفر وظيفة اسمها getPixels.
في كل الفئتين، الوظيفة getPixels نفس الشيء، مضيعة للوقت أن نعيد كتابة نفس الكود في كل فئة، الحل الوسط بين الفئة والواجهة هو الـabstract class، توفر ميزة الواجهة مع الفئة العادية، يمكن إعادة كتابة الكود باستخدام الـabstract class هكذا:
abstract class Image { protected int width; protected int height; Image(int width, int height) { this.width = width; this.height = height; } public int getPixels() { return this.width * this.height; } } class Png extends Image { Png(int width, int height) { super(width, height); } } class Gif extends Image { Gif(int width, int height) { super(width, height); } } public class Test { public static void main(String[] args) { Png png = new Png(10, 20); Gif gif = new Gif(20, 20); printNumberOfPixels(png); printNumberOfPixels(gif); } public static void printNumberOfPixels(Image image) { System.out.printf("number of pixels = %d\n", image.getPixels()); } }
أستاذي الفاضل Mr.B
ألف شكر على شرحك المفصل والمتسلسل
واعتذر لتأخري في التعقيب على ردكم
الكود الذي استخدمت في ال abstract class
لم ألحظ فيه دالة فارغة واحدة على الاقل كما هو معلوم عن مفهوم ال abstract classes
فعندما ترث الكلاسات المشتقة من الكلاس الاب يتم كتابة اكواد الدوال الفارغة بما يناسب وظيفة وعمل تلك لدوال في الكلاسات الابناء
هل ممكن توضيح هذه النقطة
اكرر شكري
Snack3r كتب:هل بحثت في المنتدى قبل طرح السؤال ؟
انظر :
ما هو الفرق بين Abstract Class و Interface Class
ماهو ال abstract و ال interface
الموضوع مُكرر, يُرجى عدم تكرار المخالفة !
شكرا لتنبيهنا
ηaseemЈava كتب:الكود الذي استخدمت في ال abstract classلم ألحظ فيه دالة فارغة واحدة على الاقل كما هو معلوم عن مفهوم ال abstract classes
فعندما ترث الكلاسات المشتقة من الكلاس الاب يتم كتابة اكواد الدوال الفارغة بما يناسب وظيفة وعمل تلك لدوال في الكلاسات الابناء
لايلزم أن تعرف دالة على الأقل، يمكنك حتى إنشاء abstract class و interface فارغة ولاتوجد مشكلة مثل:
interface AEmpty {} // فارغة
abstract class BEmpty {} // فارغة
class A implements AEmpty {}
class B extends BEmpty {}
public class Test {
public static void main(String[] args) {
new A();
new B();
}
}
الوظاف الفارغة المقصود بها وظائف يلزم أن يضيفها المستخدم بنفسه، مثلاً في المثال السابق يمكن إضافة وظيفة للحصول على إمتداد الملف ويلزم المستخدم بإضافتها بنفسه:
abstract class Image {
protected int width;
protected int height;
Image(int width, int height) {
this.width = width;
this.height = height;
}
// overriding وظيفة جاهزة، يمكن أن يعيد المستخدم كتابتها
public int getPixels() {
return this.width * this.height;
}
// وظيفة للحصول على إمتداد الملف، يجب أن يضفها المستخدم بنفسه
abstract public String getExtension();
}
class Png extends Image {
Png(int width, int height) {
super(width, height);
}
public String getExtension() {
return "png";
}
}
public class Test {
public static void main(String[] args) {
Png png = new Png(10, 20);
printNumberOfPixels(png);
}
public static void printNumberOfPixels(Image image) {
System.out.printf("number of pixels = %d | extension = %s\n",
image.getPixels(), image.getExtension());
}
}اتضحت الصورة
جزاك الله خيراً