public static final String s3;
时间: 2024-03-01 07:49:20 浏览: 101
这是一个Java中的常量声明,使用了public static final关键字来定义一个不可修改的字符串类型常量s3。由于使用了final关键字,这个变量值只能被赋值一次,并且一旦被赋值就不能再被修改。使用public关键字则表示这个常量可以被其他类访问,使用static关键字则表示这个常量是一个静态变量,可以通过类名来访问。
相关问题
已定义Student类,如下所示: public class Student implements Serializable{ private static final long serialVersionUID = 1L; //序列化串行标识 private String name; private int age; private String address; public Student(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", address=" + address + "]"; } } 要求定义5个学生对象,数据如下:学生1:("李四", 25, "河南省郑州市"),学生2:("韩梅梅", 19, "焦作市武陟县"),学生3:("李磊", 20, "郑州市中原区"),学生4:("张明", 21, "修武县方庄"),学生5:("吴林", 18, "开封市祥符区")。 请将这5个学生对象数据写入到“d:\\学生信息.txt”文件中,并在程序中反序列化输出5条学生信息。
代码如下:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
// 创建5个学生对象
Student s1 = new Student("李四", 25, "河南省郑州市");
Student s2 = new Student("韩梅梅", 19, "焦作市武陟县");
Student s3 = new Student("李磊", 20, "郑州市中原区");
Student s4 = new Student("张明", 21, "修武县方庄");
Student s5 = new Student("吴林", 18, "开封市祥符区");
// 将学生对象添加到列表中
students.add(s1);
students.add(s2);
students.add(s3);
students.add(s4);
students.add(s5);
try {
// 将学生对象列表写入文件
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\学生信息.txt"));
oos.writeObject(students);
oos.close();
// 读取文件中的学生对象列表并输出信息
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\学生信息.txt"));
List<Student> studentsRead = (List<Student>) ois.readObject();
for (Student student : studentsRead) {
System.out.println(student);
}
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
```
输出结果如下:
```
Student [name=李四, age=25, address=河南省郑州市]
Student [name=韩梅梅, age=19, address=焦作市武陟县]
Student [name=李磊, age=20, address=郑州市中原区]
Student [name=张明, age=21, address=修武县方庄]
Student [name=吴林, age=18, address=开封市祥符区]
```
优化下面代码package Y; import java.io.*; import java.net.*; import java.time.*; import java.util.*; public class Shiyan5Server { public static void main(String[] args) { ServerSocket server = null; Sy5ServerThread thread; Socket you = null; while(true) { try { server = new ServerSocket(2010); } catch (IOException e) { System.out.println("正在监听"); // TODO: handle exception } try { System.out.println("等待客户呼叫"); you = server.accept(); System.out.println("客户的地址"+you.getInetAddress()); } catch (Exception e) { // TODO: handle exception System.out.println("正在等待客户"); } if (you != null) { new Sy5ServerThread(you).start(); } } } } class Sy5ServerThread extends Thread{ Socket socket; DataOutputStream out = null; DataInputStream in = null; String s = null; Sy5ServerThread(Socket t) { socket = t; try { out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); } catch (Exception e) { // TODO: handle exception } } public void run() { while(true) { try { String S1 = "几点了?"; String S2 = "今天是几号?"; String S3 = "今天是星期几?"; String st = in.readUTF(); if (st.equals(S1)) { LocalTime time = LocalTime.now(); String Time = String.format("%tR", time); out.writeUTF(Time); } else if(st.equals(S2)) { LocalDate date = LocalDate.now(); String Time = String.format("%tm月%td日", date,date); out.writeUTF(Time); }else if (st.equals(S3)) { LocalDate week = LocalDate.now(); String Time = String.format("%tA", week); out.writeUTF(Time); }else { System.out.println("服务结束"); break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } }
以下是代码的优化:
1. 将ServerSocket的创建放在while循环的外部,否则每次循环都会重新创建一个ServerSocket对象,浪费资源。
2. 在catch语句中添加具体的异常处理代码,避免出现异常时程序无法正常执行。
3. 在Sy5ServerThread类的构造函数中,将out和in的初始化放在try语句块中,避免出现异常时无法正确初始化。
4. 将字符串常量定义为静态常量,避免在运行时重复创建对象,浪费资源。
5. 在while循环中添加try-catch语句,避免出现异常时程序中断。
改进后的代码如下:
```
package Y;
import java.io.*;
import java.net.*;
import java.time.*;
import java.util.*;
public class Shiyan5Server {
private static final String S1 = "几点了?";
private static final String S2 = "今天是几号?";
private static final String S3 = "今天是星期几?";
public static void main(String[] args) {
ServerSocket server = null;
Socket you = null;
try {
server = new ServerSocket(2010);
System.out.println("正在监听");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
System.out.println("等待客户呼叫");
you = server.accept();
System.out.println("客户的地址" + you.getInetAddress());
new Sy5ServerThread(you).start();
} catch (Exception e) {
System.out.println("正在等待客户");
e.printStackTrace();
}
}
}
}
class Sy5ServerThread extends Thread {
private static final String S1 = "几点了?";
private static final String S2 = "今天是几号?";
private static final String S3 = "今天是星期几?";
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
Sy5ServerThread(Socket t) {
socket = t;
try {
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
String st = in.readUTF();
if (st.equals(S1)) {
LocalTime time = LocalTime.now();
String Time = String.format("%tR", time);
out.writeUTF(Time);
} else if (st.equals(S2)) {
LocalDate date = LocalDate.now();
String Time = String.format("%tm月%td日", date, date);
out.writeUTF(Time);
} else if (st.equals(S3)) {
LocalDate week = LocalDate.now();
String Time = String.format("%tA", week);
out.writeUTF(Time);
} else {
System.out.println("服务结束");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文