بتاريخ: 17 ديسمبر 200619 سنة comment_86406 السلام عليكم ارجو ممن لديه اجابة ان يجيب على سؤالي ماذا تعنى constructor وكيف يمكن استدعاء tow method from class on main تقديم بلاغ
بتاريخ: 13 يناير 200719 سنة comment_89159 اختى الفاضلة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 ان شاء الله اختى تستفسدي منهاوانا حاضرة لاي سؤال ان شاء الله تقديم بلاغ
انضم إلى المناقشة
يمكنك المشاركة الآن والتسجيل لاحقاً. إذا كان لديك حساب, سجل دخولك الآن لتقوم بالمشاركة من خلال حسابك.