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

كيف يمكنني أستخدام "private Class"

بدأه Zaianb في 28 يناير 2009 · 11 رد · 1,220 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكــم ورحمـة الله وبركاتــه

أخواني وأخواتي

إذا ممكن تعلموني كيف ممكن أحول

public method

to

private method

  1.  
  2. public static double intrest(int n1,int n2,int n3)
  3.  
  4. {
  5. double intrest
  6. intrest= balance(n1,n2,n3)*.12
  7. return intrest
  8. }
  9.  

إذا تعرفوا موقع يعرض مثال على

private method

تحياي لكم جميعا

#2

بدل كلمة Public اكتب Private

#3

أشكرك أخي

لكن برنامجي يحوي أكثر من method

يعني كيف اتمكن من استخدام الناتج من "private method" الى "public method"

هذا ما اعنيه..

#4
   1.

   2.
	  private static double intrest(int n1,int n2,int n3)
   3.

   4.
	  {
   5.
	  double intrest
   6.
	  intrest= balance(n1,n2,n3)*.12
   7.
	  return intrest
   8.
	  }
   9.

تغير الموديفير بس العنوان بيسأل عن شيء آخر

مدونتي

برمجة وما يتبعها

#5

السلام عليكــم ورحمـة الله وبركاتــه

هل تعنبن استعمال ناتج "private method" داخل method أخرى public؟؟

وهل هاتان الmethods توجدان داخل نفس الclass؟؟

#6

أخواني يبدوء أنني لم أشرح ما اريده بطرقة صحيحه

عذراعلى ذلك

في البداي سادرج لكم برنامج حتى تتضح لكم الصورة

  1.  
  2.  
  3. //**** i import a special class for input and output called StaugIO****
  4. //**** the StaugIO calss is countained within the staugIO package****
  5. import staugIO.StaugIO
  6.  
  7. //application class
  8. public class project //class is start now
  9. {
  10.  
  11. public static void main(String[] args) //main method is start now
  12. {
  13. //here i try to define a StaugIO (input and output object) class object
  14. StaugIO io = new StaugIO()
  15.  
  16. //define and initialization string to store name
  17. String name =""
  18.  
  19. //use io to call method
  20. //return the string value entared by the user
  21. //assign user entar his name
  22. name=io.readString("Please Enter yor Full name")
  23.  
  24. //define and initialization integer to store account
  25. int account
  26.  
  27. //return the integer value entared by the user
  28. //assign user entar his account
  29. account=io.readInt("Please Enter your current balance")
  30.  
  31. //define and initialization integer to store deposit
  32. int deposit
  33.  
  34. //define and initialization integer to store totaldeposit
  35. int totaldeposit=0
  36.  
  37. //use io to call method
  38. //return the integer value entared by the user
  39. //assign user entar his deposit
  40. deposit=io.readInt("Please Enter the amount you want to deposit"+ "nIn case you do NOT
    want to depsit..please Enter 0")
  41.  
  42.  
  43. //****i add a while loop to make the programe aske the user more than one time ****
  44. //****the loop will keep asking the user if the number must be grater than 0****
  45. while(deposit>0)
  46.  
  47. //****I frame the body of the while loop with curly braces because it has more than one
    statement****
  48. //**** while loop is starting now*****
  49. {
  50.  
  51. //i used this statement to add the deposit
  52. totaldeposit += deposit
  53.  
  54. //use io to call method
  55. //return the integer value entared by the user if the useer entar number grater than zero
  56. //assign user entar his deposit
  57. deposit=io.readInt("Please Enter the amount you want to deposit"+ "nIn case you do NOT
    want to depsit..please Enter 0")
  58.  
  59. } //while loop end
  60.  
  61. //define and initialization integer to store withdraw
  62. int withdraw
  63.  
  64. //define and initialization integer to store totalwithdraw
  65. int totalwithdraw=0
  66.  
  67. //use io to call method
  68. //return the integer value entared by the user if the useer entar number grater than zero
  69. //assign user entar his withdraw
  70. withdraw=io.readInt("Please Enter the amount you want to withdraw"+ "nIn case you do N
    OT wnat to withdraw..please Enter 0")
  71.  
  72. //****i add a while loop to make the programe aske the user more than one time ****
  73. //****the loop will keep asking the user if the number is grater than 0****
  74. while(withdraw>0)
  75.  
  76. //the loop will start if the withdraw is less or equal to the balance
  77. if(withdraw<=balance(account, totaldeposit, totalwithdraw))
  78.  
  79. //****I frame the body of the "if" with curly braces because it has more than one statem
    ent****
  80. //while if start
  81. {
  82.  
  83. //i used this statement to add the deposit
  84. totalwithdraw +=withdraw
  85.  
  86. //use io to call method
  87. //return the integer value entared by the user if the useer entar number is less or equal
    to his balance
  88. //assign user entar his withdraw
  89. withdraw=io.readInt("Please Enter the amount you want to withdraw"+ "nIf you do NOT wn
    at to withdraw..please Enter 0")
  90.  
  91. } //if is end
  92.  
  93. //i use else in case the user entar number grater than his balance
  94. else
  95.  
  96. //****I frame the body of the "else" with curly braces because it has more than one sta
    tement****
  97. { //else start
  98.  
  99. //use io to call method
  100. //i add this becuse i try to write a string to an information dialog box
  101. io.writeInfo("withdraw failed"+"nSorry you can NOT withdraw "+ withdraw + " becuse "+w
    ithdraw+ " is grater than your balance" + balance(account, totaldeposit, totalwithdraw))
  102.  
  103. //i try to break the calcluation of the withdraw, when the user entar withdraw less than
    his balance
  104. break
  105.  
  106. } //else end
  107.  
  108. //use io to call method
  109. //i add this becuse i try to write a string "report" to an information dialog box
  110. io.writeInfo("User Name is " + name + "nYour current balance is SR " + account + "nTh
    e amount you want to deposit to your account is SR " + totaldeposit + "nYou withdraw from You
    r current balance is SR " + totalwithdraw + "nYour balance after adding deposit and taking wi
    thdraw is SR " + balance(account, totaldeposit, totalwithdraw)
  111. +"nThe intrest rate for your final balance is SR " + intrest(account, totaldeposit, to
    talwithdraw)+"n Your Final amount with the itrest rate is SR " + finalamount(account, totaldep
    osit, totalwithdraw))
  112.  
  113.  
  114.  
  115. }//main end
  116.  
  117. //balance method
  118. public static int balance(int n1,int n2,int n3)
  119. { //balance method start
  120. int calc
  121. calc=(n1+n2)-n3
  122. return calc //return calc to its calling function
  123. } //balance end
  124.  
  125. //interst method
  126. public static double intrest(int n1,int n2,int n3)
  127.  
  128. { //interst start
  129. double intrest
  130. intrest= balance(n1,n2,n3)*.12
  131. return intrest //return intrest to its calling function
  132. }//intrest end
  133.  
  134. //finalamount method
  135. public static double finalamount(int n1,int n2,int n3)
  136. { //final amount start
  137. double add
  138. add = balance(n1,n2,n3) + intrest(n1,n2,n3)
  139. return add //return add to its calling function
  140. } //final amount end
  141.  
  142. } //class end
  143.  
  144.  
  145.  

في هذا البرنامج يقوم بالتالي:

سؤال المستخدم عن

اسمه

حسابه

هل يرغب في اضافة مال

هل يرغب في سحب مال

ثم يقوم البرنامج بادراج تقرير للمستخدم بالعملييات التي قام باجرائها

أخوان انا ارغب في تحويل

public static double intrest(int n1,int n2,int n3)

الى

private

انا اعلم انه يجب علي تحويل الكلمة

لكن لاتنسوى بان الناتج في

private

سوف يستخدم في

public method

  1. public static double finalamount(int n1,int n2,int n3)
  2. { //final amount start
  3. double add
  4. add = balance(n1,n2,n3) + intrest(n1,n2,n3)
  5. return add //return add to its calling function
  6. } //final amount end
  7.  

فمذا علي ان أفعل في هذه الحاله

أتمنى انني وفقت في شرح ما اريده

السلام عليكــم ورحمـة الله وبركاتــه

هل تعنبن استعمال ناتج "private method" داخل Method أخرى public؟؟

وهل هاتان الmethods توجدان داخل نفس الclass؟؟

نعم أخي هذا ما اعنيه

نعم هو في نفس class

#7

السلام عليكــم ورحمـة الله وبركاتــه

يازمك فقط تغيير public إلى private

الmethod "finalamount" يمكنها استعمال intrest بالرغم من أنها private لأنه كلتاهما توجدان في نفس الclass

المشكل سيطرح إذا أردت استعمال intrest من داخل class اخرى

#8

صح كلامك

طيب

عندي سؤال اذا سويتهم من كلاسين مختلفين

ايش لازم اغير..؟؟

معليش بس حابة اتعلم

#9

Zaianb كتب:
عندي سؤال اذا سويتهم من كلاسين مختلفين

ايش لازم اغير..؟؟

في هذه الحالة لازم public بالنسبة للmethod التي ستستعمليها

مثال


{
public String method1()
{
return "I am method1";
}
}

class class2
{
classe1 c1 = new class1();

public void method2()
{
System.out.println("I am using " + c1.method1());
}
private void method3()
{
System.out.println("I am using " + c1.method1());
}
}
class class1

في المثال، method1 هي public، يعني ممكن أن نستعملها داخل class أخرى

method2 و method3 تستعملان كلتاهما method1، أردت فقط أن أبين أنه يمكن استعمال public method داخل public or private method

#10

السلام عليكم و رحمة الله

الأخت الفاضلة قد تفيدك هذه الصفحة:

http://java.sun.com/docs/books/tutorial/ja...esscontrol.html

سبحانك اللهم و بحمدك ، أشهد أن لا إله إلا أنت ، أستغفرك ربي و أتوب إليك

مدونتي

http://www.facebook.com/brmjt

#11

الدالة من نوع private لا يمكن إستدعاؤها من خارج الصنف.

و هي تستعمل عادة لإعطاء قيمة متغيرة لا تدخل في constructor مباشرة بل تاخذ قيمتها إعتمادا على قيم متغيرات أخرى.

يجب الإنتباه أنه حتى الدوال الوارثة للصنف الذي يحتوي دالة private لا يمكنها إستعمال تلك الدالة.

و في هذه الحالة، أي إذا أردنا إنشاء دالة لا يمكن إستدعاؤها من خارج الصنف بينما نستطيع إستدعاءها من الأصناف الوارثة، في هذه الحالة نستعمل الكلمة المفتاحية protected.

بالتوفيق.

#12

سنعود قليلا إلى المفاهيم الأساسية للبرمجة الكائنية

كما نعلم جميعا ان البرمجة الكائنية اتت بعدة مفاهيم جديدة ومن بين هذه المفاهيم مفهموم public و private

السؤال الأهم هو لماذا هذه المفاهيم ولماذا نحتاجها في برامجنا

للإجابة عن هذه الاسئلة يلزمنا معرفة ما هو الهدف من البرمجة الكائنية

فالبرمجة الكائنية أتت تقريبا لتحاكي الواقع بمعنى أننا نبرمج تحت ما يسمى الكائن

طيب إذن ما هو الهدف لكل هذه الخصوصية

الهدف منها هو ان لسفة البرمجة الكائنية أصلا أتت لتكون البرمجة بين الفريق سهلة

مثلا تخيل أنك في شركة وتبرمج مع عدة مبرمجين وكل مبرمج لديه فئة يبرمج عليها

بما انها فئة يعني لديها دوال ومتغيرات طيب هو يريد من تلك المتغريات أن تكون خاصة بتلك الفئة بصيغة أخرى انه لا يريد من مبرمج الفئة الأخرى ان يصل إلى متغرات فئته إلا بشروط معينة ماهي تلك الشروط

بإفتراض أنك لديك الأساسيات في البرمجة الكائنية

فنضع دالتين إصطلح عالميا على تسميتهما set و get وهاتين الدالتين هما اللذان نضع فيهما الشروط اللازمة للتعامل مع المتغيرات الخاصة

هذا بالنسبة للمتغيرات أما بالنسبة للدوال الخاصة فهي لا يمكنك إستدعائها من خارج الصنف أبدا

ولإصلاح المشكل لديك حلين إما ان تبدل private ب public وهذا الحل سيؤثر على برنامجك

وإما ان تعيد هندسة برنامجك من جديد

للأسف نحن نقع في مشكل أننا ندخل إلى البرمجة مباشرة ولا نهتم بتحليل ما نريد برمجته أولا على الورق وإلا لماذا يوجد هناك علم هندسة البرامج ومخططات uml

والموريس فماذا سنخسر لو حللنا البرنامج الذي نريده على الورق أولا وعملنا له مخططاته كلها ومن تم تاتي البرمجة على الحاسوب بهذه الطريقة سنوفر الوقت والجهد وحتى الأخطاء القاتلة لن تكون

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