简体   繁体   English

Java编程访问对象变量

[英]Java programming accessing object variables

Helo, there are 3 files, CustomerClient.java , CustomerServer.java and Customer.java Helo,有3个文件, CustomerClient.javaCustomerServer.javaCustomer.java


PROBLEM: In the CustomerServer.java file, i get an error when I compile the CustomerServer.java at line : System.out.println(a[k].getName()); 问题:CustomerServer.java文件中,在以下行编译CustomerServer.java时出现错误: System.out.println(a [k] .getName());


ERROR: 错误:


init:

deps-jar:

Compiling 1 source file to C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\build\classes

C:\Documents and Settings\TLNA\My Documents\NetBeansProjects\Server\src\CustomerServer.java:44: cannot find symbol

symbol  : method getName()

location: class Customer

                        System.out.println(a[k].getName());
1 error
BUILD FAILED (total time: 0 seconds)


CustomerClient.java CustomerClient.java


import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;


public class CustomerClient extends JApplet {

private JTextField jtfName = new JTextField(32);
private JTextField jtfSeatNo = new JTextField(32);
// Button for sending a student to the server
private JButton jbtRegister = new JButton("Register to the Server");
// Indicate if it runs as application
private boolean isStandAlone = false;
// Host name or ip
String host = "localhost";

public void init() {


    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(2, 1));
    p1.add(new JLabel("Name"));
    p1.add(jtfName);
    p1.add(new JLabel("Seat No."));
    p1.add(jtfSeatNo);

    add(p1, BorderLayout.CENTER);
    add(jbtRegister, BorderLayout.SOUTH);


    // Register listener
    jbtRegister.addActionListener(new ButtonListener());

    // Find the IP address of the Web server
    if (!isStandAlone) {
        host = getCodeBase().getHost();
    }
}

/** Handle button action */
private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        try {
            // Establish connection with the server
            Socket socket = new Socket(host, 8000);

            // Create an output stream to the server
            ObjectOutputStream toServer =
                    new ObjectOutputStream(socket.getOutputStream());

            // Get text field
            String name = jtfName.getText().trim();
            String seatNo = jtfSeatNo.getText().trim();

            // Create a Student object and send to the server
            Customer s = new Customer(name, seatNo);
            toServer.writeObject(s);
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

/** Run the applet as an application */
public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("Register Student Client");

    // Create an instance of the applet
    CustomerClient applet = new CustomerClient();
    applet.isStandAlone = true;

    // Get host
    if (args.length == 1) {
        applet.host = args[0];

    // Add the applet instance to the frame
    }
    frame.add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.pack();
    frame.setVisible(true);
}
} 

CustomerServer.java CustomerServer.java


import java.io.*;
import java.net.*;

public class CustomerServer {

private String name;
private int i;
private ObjectOutputStream outputToFile;
private ObjectInputStream inputFromClient;

public static void main(String[] args) {
    new CustomerServer();

}

public CustomerServer() {
    Customer[] a = new Customer[30];
    try {
        // Create a server socket
        ServerSocket serverSocket = new ServerSocket(8000);
        System.out.println("Server started ");

        // Create an object ouput stream
        outputToFile = new ObjectOutputStream(
                new FileOutputStream("student.dat", true));

        while (true) {
            // Listen for a new connection request
            Socket socket = serverSocket.accept();

            // Create an input stream from the socket
            inputFromClient =
                    new ObjectInputStream(socket.getInputStream());

            // Read from input
            //Object object = inputFromClient.readObject();
            for (int k = 0; k <= 2; k++) {
                if (a[k] == null) {
                    a[k] = (Customer) inputFromClient.readObject();
                    // Write to the file
                    outputToFile.writeObject(a[k]);
                    //System.out.println("A new student object is stored");

                    System.out.println(a[k].getName());

                    break;
                }

                if (k == 2) {
                    //fully booked
                    outputToFile.writeObject("All seats are booked");
                    break;
                }
            }



        }
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            inputFromClient.close();
            outputToFile.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
}

Customer.java 客户.java


public class Customer implements java.io.Serializable {

private String name;
private String seatno;

public Customer(String name, String seatno) {
    this.name = name;

    this.seatno = seatno;
}

public String getName() {
    return name;
}

public String getSeatNo() {
    return seatno;
}
}

The build message says its only compiling one source file. 生成消息说它仅编译一个源文件。 Perhaps the Customer class changed to include the getName function and has not been recompiled since. 可能Customer类已更改为包括getName函数,并且此后未重新编译。

Did you try compiling all three source files at the same time? 您是否尝试同时编译所有三个源文件?

Recompile the Customer.java and make sure you don't have duplicate versions of the class file hanging around. 重新编译Customer.java,并确保没有重复的类文件版本。 Use debugger (set breakpoint after the customer class de-serialization) for further debugging. 使用调试器(在客户类反序列化之后设置断点)进行进一步的调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM