الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مساعدة في شرح برنامج

مغلق
بدأه kousai في 16 مايو 2007 · 4 رد · 487 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلامي للجميع

أنا أسف بدأت مشاركاتي بسؤال بس بتمنى انكم تتحملوني

عندي برنامج مؤلف من 2 classes

المطلوب شرح البرنامج وايضاح النتائج التي تضهر بعد Compiler

انا وضعت البرنامج على رابط ورغم ذلك سأقوم بكتابته

public class References {

public static void setToTen(int i) {

i = 10;

System.out.println("inside of the method - the worth of int-date: " + i);

}

public static void setToTen(MyClass mc) {

mc.setValue(10);

System.out.println("inside of the method - the worth of MyClass-Objekt: " + mc.getValue());

}

public static void main(String[] args) {

int i = 5;

setToTen(i);

System.out.println("outside of the method - the worth of int-date : " + i);

MyClass mc = new MyClass();

mc.setValue(5);

setToTen(mc);

System.out.println("outside of the method - the worth of MyClass-Objekt: " + mc.getValue());

}

}

وهنا الclass 2

public class MyClass {

private int value = 0;

public int getValue() {

return this.value;

}

public void setValue(int value) {

this.value = value;

}

}

ارجوا تساعدوني في فهم هدا البرنامج لانه مطلوب مني شرحه يوم الاثنين القادم

فهناك عدة نقاط لم افهمها

1. لمادا جزئنا البرنامج الى 2 كلاسس

2.ولمادا قمنا بتعريف العدد i مرتين في الكلاس References ولماذا اعطيناه قيمة اخرى 5

3.وماذا تعني MyClass mc في الميثود SetToTen

البرنامج هنا

MyClass

References

ارجو منكم عدم كرهي لكترت اسألتي ولكنني مازلت مبتدئ في الجافا بالاضافة الى انني ادرسها بلغة بعيدة كل البعد عن لغتنا

تم تعديل هذه المشاركة بواسطة kousai في 16 مايو 2007 في 23:58

#2

السؤال الأول :

للحصول علي modularity و لتحقيق object encapsulation

السؤال الثاني :

يمكن أن تسمي الأول x و الثاني y فكلاهما ليس نفس الشئ بل هما متغيران مختلفان و ليسا متغير واحد، فالمتغير الأول سميناه i و أعطيناه القيمه ١٠ و الثاني سميناه i أيضا و أعطيناه القيمه ٥ و هذا جائز برمجيا طالما أنهما في دوال مختلفه

الثالث : هي parameter ، أي أن الداله method سوف تستقبل متغير نوعه MyClass و تقوم بمعالجته .

أعتقد أن الاجابه ستكون :

١٠

٥

١٠

١٠

#3

شكرا اخي daziplqa

ومازلت بحاجة الى مساعدة

في كود البرنامج قمنا بمناداة الميثود الخاصة ب Inside مرتين

ومن ثم نادينا الميثود الخاصة ب Outside ايضا مرتين

فلماذا الاجابة تظهر كما يلي

inside

outside

inside

outside

شــــــــــــــــــــــكرا

#4

عندك نسختين من المثد setToTen وحده تستقبل integer

public static void setToTen(int i) {
i = 10;
System.out.println("inside of the method - the worth of int-date: " + i);
}

والأخرى تستقبل reference من الكلاس MyClass

public static void setToTen(MyClass mc) {
mc.setValue(10);
System.out.println("inside of the method - the worth of MyClass-Objekt: " + mc.getValue());
}

الآن اذا ارسلت int سوف يذهب للمثد الأولى ، وإذا ارسلت MyClass reference يذهب للثانيه

الآن ماذا حصل في المين مثد :

public static void main(String[] args) {

int i = 5; // make new variable and assign 5 to it 
setToTen(i); // call setToTen(5) it will goes to setToTen(int i) method since it accept int argument
System.out.println("outside of the method - the worth of int-date : " + i); // will print 5 because its print local variable i 

MyClass mc = new MyClass(); // new instance from MyClass method 
mc.setValue(5);  // make value =5
setToTen(mc); // call setToTen(mc) it will goes to setToTen(MyClass mc) method since it accept MyClass reference
System.out.println("outside of the method - the worth of MyClass-Objekt: " + mc.getValue()); // will print 10 because its print instance member value 
// which is changed from setToTen method 

}

}

اتمنى وضحت ..

#5

تم الوضوح

شـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــكرا

هذا الموضوع مغلق.

مواضيع مشابهة