كيف استطيع استعمال متغير من كلاس اخر واخد فيمتها لكن بنفس الباكيج
استعمال المتغيرات ضمن نفس الحزمة package
السؤال غير واضح
رجاءاً قم بالتوضيح أكثر
شيء آخر رجاءاً لا تكتب مصطلحات أجنبية باللغة العربية
إما أن تكتب المصطلح البديل في العربية
أو تكتب الكلمة الأجنبية كما هي
تحياتي
تم تعديل هذه المشاركة بواسطة علاء الصالحي في 7 ديسمبر 2009 في 10:40
public class Frame JFrame {
int x,y;
/** Creates new form Frame */
public Frame() {
initComponents();
}
public void paint( Graphics g ) {
super.paint(g);
setTitle( "Click any where please" );
g.setColor( Color.BLACK);
g.fillRect( x,y, 1, 1 );
لو كان عندي class تاني موجود ضمن نفس package اللي فيها ال class اللي فوق (frame)
كيف اقدر استعمل قيم x,y اللي ب class اللي فوق
واستعملها ضمن class 2
إن شاء الله تكوننوا فهمتو علي هالمره
اعمل دالة getX و getY داخل الFrame ..
وعندما تريد الوصول لها ، قم بعمل كائن من الFrame و استدعي الدالتين ...
اذا لم تكن تريد عمل كائن من الFrame فيمكن جعل الدوال static ،، وقم بالوصول لها من خلال Frame.x و Frame.y أو Frame.getX اذا استخدمت الدوال ،
بالتوفيق ،،
http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة
https://moalfat.com للكتب الالكترونية والكورسات التعليمية
Everything we see now is just an engineering solution based on old science
استخدم هذا الكود:
Frame.x Frame.y
لكن يفضل أن تضع قبلهم المحدد private وتضيف للصف طرائق set و get للوصول إلى المتغيرات وتعديلها
// First solution which is not recommended.
public class Class1 {
public static String APPLICATION_TYPE = "WEB APPLICATION";
}
public class Class2 {
public static void main(String [] args)
{
System.out.println("Application type: " + Class1.APPLICATION_TYPE);
Class1.APPLICATION_TYPE = "DESKTOP APPLICATION";
System.out.println("Application type: " + Class1.APPLICATION_TYPE);
}
} // Second solution, you can pass the class1 object as a parameter to any other class where its needed
public class Class1 {
private String applicationType = "WEB APPLICATION";
/**
* @return the applicationType
*/
public String getApplicationType() {
return applicationType;
}
/**
* @param applicationType the applicationType to set
*/
public void setApplicationType(String applicationType) {
this.applicationType = applicationType;
}
}
public class Class2 {
public static void main(String [] args)
{
Class1 class1 = new Class1();
System.out.println("Application type: " + class1.getApplicationType());
class1.setApplicationType("DESKTOP APPLICATION");
System.out.println("Application type: " + class1.getApplicationType());
}
}شكرا جزيلا لكم جميعا
