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

كيف استطيع اخذ قيمة متغير من كلاس ووضعها بكلاس اخر

مغلق
بدأه المستقيل في 4 يونيو 2007 · 10 رد · 729 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليم

يوجد لدي كلاس فيه متغير ولنفرض ان اسم هذا المتغير a كيف استطيع ان اخذ القيمة النهائية لهذا المتغير و وضعها بكلاس اخر

#2
ClassB.setB ( ClassA.getA() );

منقطع من حين لاخر

#3

شكرا اخي ابو اتلفؤاد على ردك

لقد قمت بكتابة هذا الكود

DeviceControl.setB(SerialToGsm.getc());

وعند عمل كمبايل للكود اعطى هذا الخطا

<identifier> expected
DeviceControl.setB(SerialToGsm.getc());
				  ^
1 error

Tool completed with exit code 1
#4

تاكد من التعاريف للميثود و نوع المتغير المرجع مطابق للمتغير الذي تريد وضعه

نص الخطا غير واضح

منقطع من حين لاخر

#5

لقد قمت بالتاكد من تعريف الميثود

والمتغيرين من نفس النوع integer

#6

www.google.com

OO principles

composition

#7

:blink:

#8

لا حل سوا وضع الكود الكامل !!

منقطع من حين لاخر

#9

هاذا هو كود الكلاس الاول والذي يحتوي على متغير اسمه c

import javax.swing.*;
import parport.ParallelPort;
import java.io.*;
import java.util.BitSet;
import javax.comm.*;
import java.lang.*;
public class SerialToGsm {
static int check=0,connect=0;
static int number=0;
int aByte=0;
static int voicen=0;
static String text="";
 ParallelPort lpt1 = new ParallelPort(0x378); // 0x378 is normally the base address for the LPT1 port
static CReceiveThread receiveThread;
 SerialPort sp;
static int c=0;
   InputStream in;
	OutputStream out;
	String lastIndexRead;
	String senderNum;
	String smsMsg;
	String ON;
	SerialToGsm(String porta) {}
	   public void connect(String porta)
	   {
		try {

		  CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(porta);
			 sp = (SerialPort)portId.open("Sms_GSM", 0);
			sp.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			sp.setFlowControlMode(sp.FLOWCONTROL_NONE);

			in = sp.getInputStream();
			out = sp.getOutputStream();

receiveThread = new CReceiveThread();
		receiveThread.start();
		//receiveThread.stop();




		}
		catch (Exception e) {
			System.out.println("Exception " + e);

		}
	}

	private String sendAndRecv(String s, int timeout) {
		try {
			// clean serial port input buffer
			in.skip(in.available());
			System.out.println("=> " + s);
			s = s + "\r";		 // add CR
			out.write(s.getBytes());
			out.flush();

			String strIn = new String();
			for (int i = 0; i < timeout; i++){
				int numChars = in.available();
				if (numChars > 0) {
					byte[] bb = new byte[numChars];
					in.read(bb,0,numChars);
					strIn += new String(bb);
				}
				// start exit conditions
				// ---------------------
				if (strIn.indexOf(">\r\n") != -1) {
					break;
				}

				if (strIn.indexOf("OK\r\n") != -1){
					break;
				}

				if (strIn.indexOf("ERROR") != -1) { // if find 'error' wait for CR+LF
					if (strIn.indexOf("\r\n",strIn.indexOf("ERROR") + 1) != -1) {
						break;
					}
				}

		Thread.sleep(100); // delay 1/10 sec
			}

			System.out.println("<= " + strIn);

			if (strIn.length() == 0) {
				return "ERROR: len 0";
			}

		  //  JOptionPane.showMessageDialog(null,""+strIn);
			return strIn;
		}
		catch (Exception e) {
			System.out.println("send e recv Exception " + e);
			return "ERROR: send e recv Exception";
		}
	}


public void close()
		{
			try { sp.close(); } catch (Exception e) {}
		}
public void discont(int con)
{

connect=con;
if(con==0)
{
	//receiveThread = new CReceiveThread();
//receiveThread.start()
}
else
{//receiveThread.stop();
//receiveThread.stop();
}
}
public void call(int num1,String text1,int voice)
{check=1;
number=num1;
text=text1;
voicen=voice;

	}

	private class CReceiveThread extends Thread
	{
		public void run()
		{ Object SYNC=new Object();

String sms=" ",num="",subsms="";
int i=1;
sendAndRecv("AT+csmp=17,168,0,0",2);
sendAndRecv("AT+cmgf=1",2);



while(true)
{													//START WHILE LOOP
	  synchronized (SYNC)
					  {


	  try { sleep(500); } catch (Exception e) {}



	//SET THE MOBILE LANGUAGE TYPE (ENGLISH)


sms=""+sendAndRecv("AT+cmgr=1", 10);	//READ THE MESSAGE NUMBER 1 FROM MOBILE


if(!sms.equals(" "))
{
	if(sms.length()>=90)
	{


		//num=sms.substring(33,45);
		//System.out.println(""+num);



			boolean NUM;
			NUM = sms.indexOf("962777963453") > 0;			//CHEK THE THE MESSAGE SENDER NUMBER
			 if (NUM==true)



		//if(num.equals("962777915979"))
		{



		//subsms=sms.substring(112,114);					//CHEK THE DATA TO BE CONTROLLING
		//System.out.println(""+subsms);


/***************************CHEK FOR THE MESSAGE CONTENT****************************************/

					  boolean A;
					A = sms.indexOf("#11*") > 0;			//CHEK THE DATA TO BE CONTROLLING
					if (A==true)
					{
					   	c= c | 1;							//LOGICAL OR GATE
						  lpt1.write(c);					  // write a byte to the port's DATA pins

		 				 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	 A IS ON 32", 30);

		 			   }


		 			 boolean B;
					B = sms.indexOf("#10*") > 0;
					if (B==true)
					  {
					  c= c & 254;							//LOGICAL AND GATE
					  lpt1.write(c);

		 				 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	  A IS OFF 32", 30);
		 			 }
					 boolean C;
					C = sms.indexOf("#21*") > 0;
					if (C==true)
		 			 {

					   	c= c | 2;
					   lpt1.write(c);

		 				//sendAndRecv("AT+CMGS=\"0795998684\",129\r	 B IS ON 32", 30);
		 			 }
					 boolean D;
					D = sms.indexOf("#20*") > 0;
					if (D==true)
		 			 {
						c= c & 253;
					  lpt1.write(c);
					  lpt1.write(c);

		 				 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	 B IS OFF 32", 30);
		 			 }

					 boolean E;
					E = sms.indexOf("#31*") > 0;
					if (E==true)

		 			 {

					  c= c | 4;
					  lpt1.write(c);
		 		  // sendAndRecv("AT+CMGS=\"0795998684\",129\r	 C IS ON  32", 30);
		 			 }
					 boolean F;
					F = sms.indexOf("#30*") > 0;
					if (F==true)
		 			 {
					  c= c | 251;
					  lpt1.write(c);
		 				//sendAndRecv("AT+CMGS=\"0795998684\",129\r	 C IS OFF 32", 30);
		 			 }
					 boolean G;
					G = sms.indexOf("#41*") > 0;
					if (G==true)
		 			 {
					  c= c | 8;
					  lpt1.write(c);

		 				// sendAndRecv("AT+CMGS=\"0795998684\",129\r	 D IS ON  32", 30);
		 			 }
					 boolean H;
					H = sms.indexOf("#40*") > 0;
					if (H==true)
		 			 {
						c= c & 247;
					  lpt1.write(c);

		 				// sendAndRecv("AT+CMGS=\"0795998684\",129\r	 D IS OFF 32", 30);
		 			 }

		 			 boolean I;
			   		I = sms.indexOf("#51*") > 0;
					if (I==true)
							 		  {
						c= c | 16;
 			   		  lpt1.write(c);

 			   			 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	  E IS OFF 32", 30);
			   		 }

		 			 boolean J;
					  J = sms.indexOf("#50*") > 0;
					if (J==true)
			   		  {
						c= c & 239;
			   		  lpt1.write(c);

			   			 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	  E IS OFF 32", 30);
			   		 }
					 boolean K;
			 		K = sms.indexOf("#61*") > 0;
					if (K==true)
			 			 {
					  c= c | 32;
			   		lpt1.write(c);

			 			 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	 F IS ON 32", 30);
			   			  }
					 boolean L;
					  L = sms.indexOf("#60*") > 0;
					if (L==true)
			   			 {

			   			c= c & 223;
			   		 lpt1.write(c);

			   		 	// sendAndRecv("AT+CMGS=\"0795998684\",129\r	 F IS OFF 32", 30);
			   			  }

					 boolean M;
			 		M = sms.indexOf("#71*") > 0;
					if (M==true)

			   			  {

						c= c | 64;
			   		 lpt1.write(c);

				   		//sendAndRecv("AT+CMGS=\"0795998684\",129\r	 G IS ON  32", 30);
			   		 		}
					 boolean N;
					  N = sms.indexOf("#70*") > 0;
					if (N==true)
			   		 		{

						c= c & 191;
			   		lpt1.write(c);
			   		 	//sendAndRecv("AT+CMGS=\"0795998684\",129\r	 G IS OFF 32", 30);
			   	 			 }
					 boolean O;
					  O = sms.indexOf("#81*") > 0;
					if (O==true)
			   		 		 {

					  c= c | 128;
			   	   lpt1.write(c);

			   		 	//sendAndRecv("AT+CMGS=\"0795998684\",129\r	 H IS ON  32", 30);
			   		 		 }
					 boolean P;
					  P = sms.indexOf("#80*") > 0;
					if (P==true)
				 			 {

						c= c & 127;
					  lpt1.write(c);

			   			// sendAndRecv("AT+CMGS=\"0795998684\",129\r	 H IS OFF 32", 30);
		 			 		}
					 boolean Q;
			 		Q = sms.indexOf("#ALL0*") > 0;
					if (Q==true)
				 			 {
						  aByte=0;
					  lpt1.write(aByte);

			   			 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	 ALL DEVICES ARE OFF 32", 30);
		 			 		}

					 boolean R;
					R = sms.indexOf("#ALL1*") > 0;
					if (R==true)
				 			 {
						  aByte=255;
					  lpt1.write(aByte);

			   			 //sendAndRecv("AT+CMGS=\"0795998684\",129\r	 ALL DEVICES ARE ON 32", 30);
		 			 		}

					 boolean S;
		   			S = sms.indexOf("#??*") > 0;
					if (S==true)
		   				   {
						aByte = lpt1.read(); // read a byte from the port's STATUS pins
	 					System.out.println("Input from parallel port: " +ON);

		   			   	// sendAndRecv("AT+CMGS=\"0795998684\",129\r	 ON +DEVICES ARE ON 32", 30);
		 			 		}


					   boolean T;
			 	 	T = sms.indexOf("#11*") > 0 || sms.indexOf("#10*") > 0 || sms.indexOf("#21*") > 0 || sms.indexOf("#20*") > 0
			 	 	|| sms.indexOf("#31*") > 0  || sms.indexOf("#30*") > 0  || sms.indexOf("#41*") > 0  || sms.indexOf("#40*") > 0
			 	 	|| sms.indexOf("#51*") > 0  || sms.indexOf("#50*") > 0  || sms.indexOf("#61*") > 0  || sms.indexOf("#60*") > 0
			 	 	|| sms.indexOf("#71*") > 0  || sms.indexOf("#70*") > 0  || sms.indexOf("#81*") > 0  || sms.indexOf("#80*") > 0
			 	 	|| sms.indexOf("#ALL1*") > 0  || sms.indexOf("#ALL0*") > 0  || sms.indexOf("#??*") > 0;
				   if (T==false)
		 		{
		 			//sendAndRecv("AT+CMGS=\"0776708407\",129\r	 WRONG DATA 32", 30);

		 			System.out.println("WRONG DATA");

		 			}
/***************************END OF CHEK FOR HTE MESSAGE CONTENT************************************/

		}


  boolean UNRE;
 UNRE = sms.indexOf("REC READ") == 20;		//CHEK FOR THE MESSAGE READ OR NO
 if (UNRE==true)
 	{
 	  int f= sms.indexOf("REC READ");
	  System.out.println(f);
 	  sendAndRecv("AT+CMGD=1",1);		//IF READ DELET IT
	  }



 // else
  //  {
	// sendAndRecv("AT+CMGS=\"\"+num\"\r  YOU ARE UNAUTHORIZED PERSON 32", 30);
//
  //	   }

}

}


}						//SYN END


}						//WHILE END

}



}

  public static void main(String a[])
{
SerialToGsm	port=new SerialToGsm("COM1");

port.connect("COM1");


	}



}

وهذا الكلاس الثاني والذي اريد ان اخذ فيه قيمة المتغير c من الكلاس الاول

import parport.ParallelPort;



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Summary description for DeviceControl
 *
 */
public class DeviceControl extends JFrame
{
	// Variables declaration
	private JLabel jLabel1;
	private JCheckBox C1;
	private JCheckBox C2;
	private JCheckBox C3;
	private JCheckBox C4;
	private JCheckBox C5;
	private JCheckBox C6;
	private JCheckBox C7;
	private JCheckBox C8;
	private JButton B1;
	private JPanel contentPane;
	// End of variables declaration
SerialToGsm gsm =new SerialToGsm();

ParallelPort lpt1 = new ParallelPort(0x378);
int B;
DeviceControl.setB=SerialToGsm.getc();
	public DeviceControl()
	{
		super();
		initializeComponent();
		//
		// TODO: Add any constructor code after initializeComponent call
		//

		this.setVisible(true);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always regenerated
	 * by the Windows Form Designer. Otherwise, retrieving design might not work properly.
	 * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder
	 * to retrieve your design properly in future, before revising this method.
	 */
	private void initializeComponent()
	{
		jLabel1 = new JLabel();
		C1 = new JCheckBox();
		C2 = new JCheckBox();
		C3 = new JCheckBox();
		C4 = new JCheckBox();
		C5 = new JCheckBox();
		C6 = new JCheckBox();
		C7 = new JCheckBox();
		C8 = new JCheckBox();
		B1 = new JButton();
		contentPane = (JPanel)this.getContentPane();

		//
		// jLabel1
		//
		jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
		jLabel1.setHorizontalTextPosition(SwingConstants.LEADING);
		jLabel1.setText("Checke or remove the checked from  the device number that would like to ON or OFF it.");
		jLabel1.setVerticalAlignment(SwingConstants.TOP);
		//
		// C1
		//
		C1.setText("Device 1");
		C1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
			C1_itemStateChanged(e);
			if (C1.isSelected()== true)
								{
			c= c | 1;
			 lpt1.write(c);}
			else
			 c=c & 254;
			 {lpt1.write(c);
		}
			}

		});
		//
		// C2
		//
		C2.setText("Device 2");
		C2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C2_itemStateChanged(e);
			if (C2.isSelected()== true)
							{
			 c= c | 2;
			 lpt1.write(c);}
			else
			 c= c & 253;
			 {lpt1.write(c);
		}
			}

		});
		//
		// C3
		//
		C3.setText("Device 3");
		C3.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C3_itemStateChanged(e);
			if (C3.isSelected()== true)
				{
				c= c | 4;
				lpt1.write(c);}
			else
			c=c & 251;
			{lpt1.write(c);
		}
			}

		});
		//
		// C4
		//
		C4.setText("Device 4");
		C4.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C4_itemStateChanged(e);
			if (C4.isSelected()== true)
							{
							c= c | 8;
							lpt1.write(c);}
						else
						c=c & 247;
						{lpt1.write(c);
		}
			}

		});
		//
		// C5
		//
		C5.setText("Sevice 5");
		C5.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C5_itemStateChanged(e);
			if (C5.isSelected()== true)
							{
							c= c | 16;
							lpt1.write(c);}
						else
						c=c & 239;
						{lpt1.write(c);
		}
			}

		});
		//
		// C6
		//
		C6.setText("Device 6");
		C6.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C6_itemStateChanged(e);
			if (C6.isSelected()== true)
							{
							c= c | 32;
							lpt1.write(c);}
						else
						c=c & 223;
						{lpt1.write(c);
		}
			}

		});
		//
		// C7
		//
		C7.setText("Device 7");
		C7.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C7_itemStateChanged(e);
			if (C7.isSelected()== true)
							{
							c= c | 64;
							lpt1.write(c);}
						else
						c=c & 191;
						{lpt1.write(c);
		}
			}

		});
		//
		// C8
		//
		C8.setText("Device 8");
		C8.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e)
			{
				C8_itemStateChanged(e);
			if (C8.isSelected()== true)
							{
							c= c | 128;
							lpt1.write(c);}
						else
						c=c & 127;
						{lpt1.write(c);
		}
			}

		});
		//
		// B1
		//
		B1.setText("Exit");
		B1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				B1_actionPerformed(e);
			//DeviceControl devicecontrol =new DeviceControl();
			//devicecontrol.setVisible(false);
			setVisible(false);
			}

		});
		//
		// contentPane
		//
		contentPane.setLayout(null);
		contentPane.setMaximumSize(new Dimension(419, 0));
		addComponent(contentPane, jLabel1, 6,195,423,25);
		addComponent(contentPane, C1, 27,21,96,24);
		addComponent(contentPane, C2, 27,60,96,24);
		addComponent(contentPane, C3, 27,97,96,24);
		addComponent(contentPane, C4, 28,138,96,24);
		addComponent(contentPane, C5, 180,21,96,24);
		addComponent(contentPane, C6, 179,58,96,24);
		addComponent(contentPane, C7, 179,96,96,24);
		addComponent(contentPane, C8, 177,138,96,24);
		addComponent(contentPane, B1, 329,241,83,28);
		//
		// DeviceControl
		//
		this.setTitle("DeviceControl - extends JFrame");
		this.setLocation(new Point(0, 0));
		this.setSize(new Dimension(442, 317));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}


	//
	// TODO: Add any appropriate code in the following Event Handling Methods
	//
	private void C1_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC1_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C2_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC2_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C3_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC3_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C4_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC4_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C5_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC5_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C6_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC6_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C7_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC7_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void C8_itemStateChanged(ItemEvent e)
	{
		System.out.println("\nC8_itemStateChanged(ItemEvent e) called.");
		System.out.println(">>" + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected":"unselected"));
		// TODO: Add any handling code here

	}

	private void B1_actionPerformed(ActionEvent e)
	{
		System.out.println("\nB1_actionPerformed(ActionEvent e) called.");
		// TODO: Add any handling code here

	}

	}
#10
int number = SerialToGsm.c;
#11

شكرا اخي ali7876 لقد نجحت هذه الطريقة

شكرا جزيلا

هذا الموضوع مغلق.

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…