الانتقال إلى المحتوى

استدعاء Method


emand1

Recommended Posts

  • بعد 4 أسابيع...

اختى الفاضلة
constructor
الفائدة منه تحديد طريقة بداية الكلاس وتنفيذه للاوبجيكت
you can declare your own constructor to specify how objects of a class should be initialized
اليك هذه الامثلة
هذا المثال بوضح كيفية تعريف الكونستركتور

class ConstructorE // class to show constructor declarations 

{ 

public int x; 
public double y; 
public String name; 

public ConstructorE(int p) 
{ 
x=p; 
} 

public ConstructorE(double p) 
{ 
y=p; 
} 

public ConstructorE(String p) 
{ 
name=p; 
} 

}// ends the constructor 


هذا المثال بعمل تيست للبرنامج السابق يعني بعمل استدعاء للكونستركتور

public class ConstructorTest 

{ 

public static void main(String args[]) 

{  // create objects using constructors 

 ConstructorE a1=new ConstructorE(4); 
 ConstructorE a2=new ConstructorE(5.66); 
 ConstructorE a3=new ConstructorE("Chris"); 

 System.out.println(a1.x + " " + a2.y + " " + a3.name); 

}//ends the main 

}//ends Test class 



بالنسبة للمثود اختى الفاضلة
سأدرج لك ان شاء الله امثلة تبين كيف يتم منادة الدوال
Methods
methodsinmethods.java

// writes a method with a class and class it 
// converts metres to inches 

import java.io.*; 

public class Simple 

{ 
public static double convertMetre(double metre) 

{ 

double inches = (metre *39.37); 
return(inches); 
}// Class within a class without a main 

 
public static void main(String args[]) throws IOException 

{ 

 //create a bufferreadser object rd to read the InputStreamReader 

 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 

 String s = new String(); 
 double metre,inches; 

 System.out.print("Enter number of metres" + " "); 
 System.out.flush(); 

 s=rd.readLine(); 

 Double m = new Double(s);  // converts string to float 
 metre = m.doubleValue();   // sets inches equals to m 
 inches= convertMetre(metre);  // uses the convert method to convert metres to inches 

System.out.println("There are " + inches + " inches in " + metre + " metres."); 

}// ends the main 
}//ends the class Simple 




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

callingamethod.java

// Calls a method from outside a class 
// and calling the method of the class 
// converts metres to inches 

import java.io.*; 
import SimpleMetre; 

publcis class SimpleChange 

{ 

public static void main(String args[]) throws IOException 

{ 

//create a buffer reader object rd to read the InputStreamReader 
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 

String s = new String(); 
double metre; 
double inches; 

System.out.print("Enter number of metres "); 
System.out.flush(); 
s=rd.readLine(); 

Double m=Double.valueOf(s) // converts string to float 
metre=m.doubleValue();	 //sets metres equals to f 
inches=SimpleMetre.convertMetre(metre);  // uses the convert method to convert metres to inches 

System.out.println("There are " + inches + " inches in " +metre+ " metres"); 

}//ends the main 

}//ends the class SimpleChange 





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

callingamethod2.java

//calls a method from outside a class by reating an instance of the Simplemetre clas 
// and calling the mthod 
//converts metres to inches 

import java.io.*; 
import SimpleMetre; 

public class SimpleChangeCall 

{ 

public static void main (String args[]) throws IOException 
 
{ 

 //create a bufferreader object rd to read the inputStreamReader 
 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 

 SimpleMetre sm = new SimpleMetre(); 

 String s= new String(); 
 double metre,inches; 

 System.out.print("Enter number of metres "); 
 System.out.flush(); 
 s=rd.readLine(); 

 Double m=new Double(s);  // converts string to float 
 metre=m.doubleValue();   //sets metres equals to m 
 inches = sm.convertMetre(metre);  // uses the convert method to convert metres to inches 

System.out.println("There are " + inches + " inches in " + metre + " metres"); 

}//ends the main 

}//ends the class 





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

callingmethod3.java

import java.io.*; 
import SimpleExtendsMetre; 

public class SimpleChangeExtendsCall 
{ 

public static void main (String args[]) throws IOException 
{ 

 //create a bufferreader object rd to read the InputStreamReader 
 BufferedReader rd = new BufferedReader(new InputStreamReader(System.in)); 
 
 SimpleExtendsMetre sm = new SimpleExtendsMetre(); 

 String answer; 
 String s = new String(); 
 double metres; 
 double inches; 
 do 
 {// use of do while loop 
 System.out.println("Enter number of inches "); 
 System.out.flush();// clears the input stream 

 s=rd.readLine(); 
 Double m= new Double(s); // converts string to float 
 inches = m.doubleValue();  // sets metres equal to m 

 // converts method to convert inches to metres 
 metres = sm.convertInchesToMetres(inches); 
 System.out.println("There are " + metres + " metres in " + inches + " inches."); 

 // TESTTING THE INHERITANCE 
 System.out.println("This part calls the inherited method convertMetre "); 
 System.out.println("Enter the number of metres "); 
 System.out.flush(); 
 s=rd.readLine(); 
 Double me=new Double(s); // Converts the string to a float 
 metres= me.doubleValue(); // sets metres equal to me 
 inches = SimpleExtendsMetre.convertMetre(metres); // uses the convert method to convert metres to inches . 
 
 System.out.println("There are " + inches + " inches in " + metres + " metres."); 
 
 //This is for the continuation loop 
 System.out.println("Do you want to continue. Enter Y to carry on, anything else to quit "); 
 System.out.flush(); 
 answer=rd.readLine(); 
	 } while(answer.equalsIgnoreCase("Y")); 

} 

} 





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

callingmethod4.java

//Create a class SimpleExtendsMetre to show Inheritance 

public class SimpleExtendsMetre extends SimpleMetre // class previously created 
{ 

public double inchesToMetresFactor = 39.37; 
 
//subclass extends SimpleMetre with a new method that provide more functionality 
//this class also inherits the methods of SimpleMetre 

public double convertInchesToMetres(double inches) 

{ 
 double metres = (inches/this.inchesToMetresFactor);// use of this 
 return(metres); 
} 

}// ends the extention of the SimpleMetre 



ان شاء الله اختى تستفسدي منها
وانا حاضرة لاي سؤال ان شاء الله

رابط هذا التعليق
شارك

انضم إلى المناقشة

يمكنك المشاركة الآن والتسجيل لاحقاً. إذا كان لديك حساب, سجل دخولك الآن لتقوم بالمشاركة من خلال حسابك.

زائر
أضف رد على هذا الموضوع...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   تمت استعادة المحتوى السابق الخاص بك.   مسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

جاري التحميل
×
×
  • أضف...

برجاء الإنتباه

بإستخدامك للموقع فأنت تتعهد بالموافقة على هذه البنود: سياسة الخصوصية