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

ماهي وظيفة Return وشرح الكود اى فيها

بدأه ahaaat في 9 أغسطس 2009 · 5 رد · 4,678 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلام ياخوان اتمنى شرح هذه الكود

First file

public class GradeBook
 6  {
 7	 private String courseName; // course name for this GradeBook
 8
 9	 // method to set the course name			  
10	 public void setCourseName( String name )	  
11	 {											 
12		courseName = name; // store the course name
13	 } // end method setCourseName				 
14
15	 // method to retrieve the course name
16	 public String getCourseName()		
17	 {									
18		return courseName;				
19	 } // end method getCourseName		
20
21	 // display a welcome message to the GradeBook user
22	 public void displayMessage()
23	 {
24		// this statement calls getCourseName to get the
25		// name of the course this GradeBook represents
26		System.out.printf( "Welcome to the grade book for\n%s!\n",
27		   getCourseName() );
28	 } // end method displayMessage
29
30  } // end class GradeBook

Second

import java.util.Scanner; // program uses Scanner
 4
 5  public class GradeBookTest
 6  {
 7	 // main method begins program execution
 8	 public static void main( String args[] )
 9	 {
10		// create Scanner to obtain input from command window
11		Scanner input = new Scanner( System.in );
12
13		// create a GradeBook object and assign it to myGradeBook
14		GradeBook myGradeBook = new GradeBook();
15
16		// display initial value of courseName
17		System.out.printf( "Initial course name is: %s\n\n",
18		   myGradeBook.getCourseName() );
19
20		// prompt for and read course name
21		System.out.println( "Please enter the course name:" );
22		String theName = input.nextLine(); // read a line of text
23		myGradeBook.setCourseName( theName ); // set the course name
24		System.out.println(); // outputs a blank line
25
26		// display welcome message after specifying course name
27		myGradeBook.displayMessage();
28	 } // end main
29
30  } // end class GradeBookTest

وبالاخص هذه اش معنى return اش وضيفتها

public String getCourseName()		
17	 {									
18		return courseName;				
19	 } // end method getCourseName

والف شكر لكم ياخوان والله مالي الى اللله ثم انتم

وشكراً لكم ثاني مرة

#2

سلام ..

-------

ال keyWord return

دائما يأتى وراها متغير له type معين

ودائماً تكتب فى نهاية ال method

و القيمة الراجعة من هذه الدالة تذهب الى نفس الدالة التى تعمل calling

-------------------------------------------------------------------

الكود المكتوب عند حضرتك :

ماشى بطريقة ال setter & getter

الدالة set مسئولة عن تخزين القيم

الدالة get مسئول عن إرجاع القيم >>> أوتوماتيكياً

-----------------------------------------------------------------

مثال يوضح أكثر استعمال ال setter& getter و ال return keyword

/*Here we use setter and getter
 * we store value in set method 
 * and return automatically this value from the get method
 * 
 * Beshoy atef william.....Thanks
 */
import java.util.Scanner;

public class TestSetGet {

	String name, country;
	int age;
	float salary;

	public TestSetGet() {
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public void displayData() {
		System.out.println("The name is :" + getName() + "\n" + "The country is: " +  getCountry() + "\n " + "The age is: " + getAge() + " \n " + " The salary is: " + getSalary());
	}

	public static void main(String[] args) {
		TestSetGet testSetGet = new TestSetGet();

		Scanner scanner = new Scanner(System.in);

		System.out.println(" Enter name:");
		String theName = scanner.nextLine();
		testSetGet.setName(theName);	// Here we call the setName and store the value we enter in the variable name

		System.out.println(" Enter country:");
		String theCountry = scanner.nextLine();
		testSetGet.setCountry(theCountry);			  // as the same 

		System.out.println(" Enter age:");
		int theAge = scanner.nextInt();
		testSetGet.setAge(theAge);					 // as the same

		System.out.println(" Enter salary:");
		float theSalary = scanner.nextFloat();
		testSetGet.setSalary(theSalary);

		testSetGet.displayData();
	}
}

أتمنى كلامى يفى بطلبك

و هذ لينك به تعريف ال KeyWords المشهورة

http://en.wikipedia.org/wiki/List_of_Java_keywords

و لو كلامى فى شئ غلط ياريت الأحباء الأفاضل يتكرموا و يفيدونا

شكراً

بيشوى عاطف

تم تعديل هذه المشاركة بواسطة mr.beshoy في 10 أغسطس 2009 في 03:53

#3

public String getCourseName()

{

return courseName;

}

عندما تقوم بمناداة getCourseName()

فسترجع الميثود لك ( نص ) و هو courseName

جرب ان تنادي الميثود داخل جملة طباعة و شاهد ماسيطبع

#4

سلام عليكم

الاخ mr.beshoy & استثنائيه اشكركم ياخوان على المساعدة والله يوفقكم

لكن مازالت الامور عندي ماهي واضحه

الكود هذا معناه انه متغير حرفي خاص

private String courseName; // course name for this GradeBook

لكن هل هذا متغير او method

public String getCourseName()		
17	 {									
18		return courseName;				
19	 } // end method getCourseName

الى اعرفه انه اذا كان method لابد يكون void

والـ return لس مادخلت العقل اتمنى انكم تستحملوني ياخوان

وبصراحة انا مبتداء في البرمجة

وانا عندي لغة بسيطة تخليني اقراء كتب بالانكليزي

والف شكر لكم ياخوان واله ماستغني عنكم

تم تعديل هذه المشاركة بواسطة ahaaat في 11 أغسطس 2009 في 17:19

#5
private String courseName;

هذا متغير من نوع String

public String getCourseName()		
17	 {									
18		return courseName;				
19	 } // end method getCourseName

بكل بساطة ال getCourseName()

هذه method من نوع String

فحتماً لابد أن تُرجع متغير من نوع String

كما فعل return CourseName

الى اعرفه انه اذا كان method لابد يكون void

ال method تكون من أى نوع

لو من نوع int ترجع متغير من نوع int

" " " float " " " " float

لو من نوع string ترجع متغير من نوع string

أما لو من نوع void فهى لا تُرجع شئ كما فى الكود الذى كتبته لحضرتك

	public void displayData() {
		System.out.println("The name is :" + getName() + "\n" + "The country is: " +  getCountry() + "\n " + "The age is: " + getAge() + " \n " + " The salary is: " + getSalary());
	}

شد حيلك و مش تقلق و وواظب على الممارسة مهما كانت المشاكل

ربنا معاك .... باى باى :lol:

بيشوى عاطف

#6

سلام عليكم

mr.beshoy يعطيك الف عافية الحمدالله وضحت

لكن لمن انتقلت للدرس الثاني في شي مافهمته

Fist file

public class GradeBook
 5  {
 6	 private String courseName; // course name for this GradeBook
 7
 8	 // constructor initializes courseName with String supplied as argument
 9	 public GradeBook( String name )									   
10	 {																	 
11		courseName = name; // initializes courseName					   
12	 } // end constructor												  
13
14	 // method to set the course name
15	 public void setCourseName( String name )
16	 {
17		courseName = name; // store the course name
18	 } // end method setCourseName
19
20	 // method to retrieve the course name
21	 public String getCourseName()
22	 {
23		return courseName;
24	 } // end method getCourseName
25
26	 // display a welcome message to the GradeBook user
27	 public void displayMessage()
28	 {
29		// this statement calls getCourseName to get the
30		// name of the course this GradeBook represents
31		System.out.printf( "Welcome to the grade book for\n%s!\n",
32		   getCourseName() );
33	 } // end method displayMessage
34
35  } // end class GradeBook

Second file

public class GradeBookTest
 6  {
 7	 // main method begins program execution
 8	 public static void main( String args[] )
 9	 {
10		// create GradeBook object
11		GradeBook gradeBook1 = new GradeBook(		 
12		   "CS101 Introduction to Java Programming" );
13		GradeBook gradeBook2 = new GradeBook(		 
14		   "CS102 Data Structures in Java" );		 
15
16		// display initial value of courseName for each GradeBook
17		System.out.printf( "gradeBook1 course name is: %s\n",
18		   gradeBook1.getCourseName() );
19		System.out.printf( "gradeBook2 course name is: %s\n",
20		   gradeBook2.getCourseName() );
21	 } // end main
22
23  } // end class GradeBookTest

بالذات هذا الكود مافهمته

public GradeBook( String name )									   
10	 {																	 
11		courseName = name; // initializes courseName					   
12	 } // end constructor												  
13

وشكرا لك ياغالي

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