Java编程访问对象变量

标签 java compiler-errors

嗨,有3个文件,CustomerClient.javaCustomerServer.javaCustomer.java

<小时/>

问题:CustomerServer.java文件中,当我编译CustomerServer.java行时出现错误: System.out.println(a[k].getName());

<小时/>

错误:

<小时/>
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

<小时/>
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

<小时/>
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

<小时/>
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;
}
}

最佳答案

构建消息表示它只编译一个源文件。也许 Customer 类已更改为包含 getName 函数,并且此后就没有重新编译过。

您是否尝试同时编译所有三个源文件?

关于Java编程访问对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2931940/

相关文章:

Java UTC 到本地时间不起作用

c++ - 为什么在尝试使i = i * i时出现错误 “using uninitialized memory ' i'”和 “uninitialized local variable '我已使用”

c++ - _DEBUG 和 LLVM 5.0 C++ : Preprocessor Expected value in expression

Java 有界泛型 : Type inference bug?(方法调用,JLS 15.12.2.7)

debugging - 使看不到带有%标记的规则

java - 通用方法与通用容器结合使用

java - Java 面试的重要主题/API 列表

java - 在 repaint() 调用之后,如何知道所有 Java2D 渲染何时完成?

java - 在java中使用公共(public)属性可以获得性能提升吗?

python - python程序显示编译错误