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

كتابة أكثر من صف و إنشاء صف تجريبي لهم

بدأه yahya91 في 10 ديسمبر 2010 · 4 رد · 312 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

كتبت هذا البرنامج التالي(يتألف من ثلاث صفوف عادية لايوجد بينها وراثة و لا أي شيء) :

/***********building class**********/
class Building 
{
  //المتحولات الخاصة بالصف
 private int floor,rooms,sqmeter;
 //الباني العادي
 Building(int floor,int rooms,int sqmeter)
 {
  this.floor=floor;
  this.rooms=rooms;
  this.sqmeter=sqmeter;
 }
 //الباني الافتراضي
 Building()
 {
  this.floor=0;
  this.rooms=0;
  this.sqmeter=0;
}
 void export()
 {
  System.out.println("building info:\n floors:"+floor+" rooms:"+rooms+" sqmeter:"+sqmeter); 
 }
  public static void main(String args[])
 {
    /*class-1- test*/
    Building b1=new Building();
    Building b2=new Building(5,10,200);
    b1.export();
    b2.export();
 }

}//نهاية الصف 

/**********House class*************/
class House 
{
  private int bedrooms,bathrooms;
  //الباني العادي
  House(int bedrooms,int bathrooms)
  {
    this.bedrooms=bedrooms;
    this.bathrooms=bathrooms;
  }
  //الباني الافتراضي
   House()
  {
    this.bedrooms=0;
    this.bathrooms=0;
  }
   void export()
 {
  System.out.println("house info:\n bedrooms:"+bedrooms+" bathrooms:"+bathrooms); 
 }
    //public static void main(String args[])
 //{
// House o1=new House();
  //  House o2=new House(5,6);
  //  o1.export();
  //  o2.export();
 //}
}//نهاية الصف

/**************class office*************/
class Office
{
  private int phones,extnguishers;
  //الباني العادي
  Office(int phones,int extnguishers)
  {
    this.phones=phones;
    this.extnguishers=extnguishers;
  }
  Office()
  {
    this.phones=0;
    this.extnguishers=0;
  }
   void export()
 {
  System.out.println("office info:\n phones:"+phones+" extnguishers:"+ extnguishers); 
 }
   // public static void main(String args[])
 //{
 //Office o1=new Office();
   // Office o2=new Office(5,6);
   // o1.export();
   // o2.export();
 //}
}
/*class Test
{
  public static void main(String args[])
  {
    Building b1=new Building();
    Building b2=new Building(5,10,200);
     b1.export();
     b2.export();
  House h1=new House();
    House h2=new House(5,6);
    h1.export();
    h2.export();
    Office o1=new Office();
    Office o2=new Office(5,6);
    o1.export();
    o2.export();
  }
}*/

و في هذه الصيغة الحالية استطعت اختبار الصف الأول و لكن عندما حاولت وضع صف جديد test يحوي على main method فقط لإنشاء أغراض من الصفوف الثلاثة أخرج لي خطأ و قال أن ها البرنامج لا يملك static public main methd

علماً أنه كانت صياغة الصف test كالآتي :

class Test
{
public static void main(String []args )
{
Building b1=new Building(10,10,10);
House h1=new House(10,10);
Office o1=new Office(10,10);
b1.export();
11.export();
o1.export();
}
}
فما الخطأ علماً أني الآن لا أريد استخدام الوراثة .
#2

تعمل بشكل جيد

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

House h1=new House(10,10);

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

11.export();

اولا اسم الكائن غلط

ثانيا لا يجوز ان يبدا اسم الكائن او المتغير بأرقام

وبالتالى هيكون التصحيح هكذا

class Test {

	public static void main(String[] args) {
    	Building b1 = new Building(10, 10, 10);
    	House h1 = new House(10, 10);
    	Office o1 = new Office(10, 10);
    	b1.export();
    	h1.export();
    	o1.export();
	}
}

من المفضل ان تفصلى كل class فى ملف على حدى

بالتوفيق

1

Software Developer
Mahmoudkelany.com


 

#3

بالنسبة لاسم الغرض أنا أخطأت في نقله عن إنشاء الموضوع (كتبته على المنتدى مباشرة أي كان صحيح عند الترجمة) و على كل الأحوال أعدت الترجم و ظهر لي الخطأ التالي :

Static Error: This class does not have a static void main method accepting String[].

>

علماً أني حاولت الترجمة على jdk باستخدام cmd و باستخدام المترجم drjava و ظهر نفس الخطأ

وسأعيد لك البرنامج تماماً كما وضعته في المترجم

/***********building class**********/
class Building 
{
  //المتحولات الخاصة بالصف
 private int floor,rooms,sqmeter;
 //الباني العادي
 Building(int floor,int rooms,int sqmeter)
 {
  this.floor=floor;
  this.rooms=rooms;
  this.sqmeter=sqmeter;
 }
 //الباني الافتراضي
 Building()
 {
  this.floor=0;
  this.rooms=0;
  this.sqmeter=0;
}
 void export()
 {
  System.out.println("building info:\n floors:"+floor+" rooms:"+rooms+" sqmeter:"+sqmeter); 
 }


}//نهاية الصف 

/**********House class*************/
class House 
{
  private int bedrooms,bathrooms;
  //الباني العادي
  House(int bedrooms,int bathrooms)
  {
    this.bedrooms=bedrooms;
    this.bathrooms=bathrooms;
  }
  //الباني الافتراضي
   House()
  {
    this.bedrooms=0;
    this.bathrooms=0;
  }
   void export()
 {
  System.out.println("house info:\n bedrooms:"+bedrooms+" bathrooms:"+bathrooms); 
 }

}//نهاية الصف

/**************class office*************/
class Office
{
  private int phones,extnguishers;
  //الباني العادي
  Office(int phones,int extnguishers)
  {
    this.phones=phones;
    this.extnguishers=extnguishers;
  }
  Office()
  {
    this.phones=0;
    this.extnguishers=0;
  }
   void export()
 {
  System.out.println("office info:\n phones:"+phones+" extnguishers:"+ extnguishers); 
 }

}
class Test {

        public static void main(String[] args) {
        Building b1 = new Building(10, 10, 10);
        House h1 = new House(10, 10);
        Office o1 = new Office(10, 10);
        b1.export();
        h1.export();
        o1.export();
        }
}

شكراً لك .

#4

مفيش فيه اى مشاكل خالص

ممكن تعملى clean and build لو كنتى بتستخدمى netbeans

او ممكن حضرتك تعملى مشروع من جديد

وتضعى فيه الكود مره اخرى

وتجربى

انما الكود سليم وشغال تمام

وهذه صوره من عمل البرنامج

post-217802-053866700 1292015219_thumb.p

المرفقات
222.png

Software Developer
Mahmoudkelany.com


 

#5

جزاك الله خيراً . الخطأ بالتالي من المترجم لأني أغلقت المترجم و فتحته من جديد و لم أستفد شيئاً على كل الأحوال

مرشحpost_alert_3.png ... أخوك يحيى

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