一. IO操作问题描述程序功能:在网络传输过程中,对象数据往往需要进行序列化操作,即将对象序列化为byte数组,同时也需要反序列化操作将byte数组转化为对象。下面程序中定义了一个Student类,在该类中已经实现了反序列化操作,请完成序列化的操作代码。程序结构如下:import java.io.*;class Student{int stuNo;//学号String name;//姓名String sex;//性别int age;//年龄String grade;//年级String classname;//班级public Student(int stuNo,String name,String sex,int age,String grade,String classname){this.stuNo=stuNo;this.name=name;this.sex=sex;this.age=age;this.grade=grade;this.classname=classname;}//反序列化操作public static Student GetStudent(byte[] byteStudent){try{ByteArrayInputStream bais=new ByteArrayInputStream(byteStudent);DataInputStream dis=new DataInputStream(bais);int stuNo=dis.readInt();String name=dis.readUTF();String sex=dis.readUTF();int age=dis.readInt();String grade=dis.readUTF();String classname=dis.readUTF():return new Student(stuNo,name,sex,age,grade,classname);}catch(Exception ex){return null;}//序列化操作,请完成public byte[] GetBytes(){... //补充此处代码}public class Exam2{public static void main(String args[]){Student stu=new Student(20090223,”jack”,”man”,21,”三年级”,”2班”);byte[] bytestu=stu.GetBytes();Student stu1=Student.GetStudent(bytestu);System.out.println(stu1.s5uNo);}程序要求1. 实现序列化代码
举一反三
- 下面程序运行的结果是 struct Student { public int age; public string name; public Student(int age, string name) { this.age = age; this.name = name; } } class Program { static void Main(string[] args) { Student stu1 = new Student(18, "小方"); Student stu2 = new Student(24, "小刚"); stu2 = stu1; stu1.age = 30; stu1.name = "小燕"; Console.WriteLine("{1},{0}",stu2.age,stu2.name); } }
- 分析下面的程序,输出的结果是? public class Test { public static void main(String[] args) { final Person p = new Person("张三", 23); p.setName("李四"); p.setAge(24); System.out.println(p); } } class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } 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; } @Override public String toString() { return "我的姓名是:" + name + ",我的年龄是:" + age ; } }
- 【填空题】public class ThisTest{ private String name; private int age; public ThisTest() { System.out.println("产生一个新的Person对象。"); } public ThisTest (String name, int age) { this(); this.name = name; this.age = age; } public String getInf() { return "姓名:" + name + ",年龄:" + age; } public static void main(String[] args) { ThisTest per = new ThisTest("张三", 20); System.out.println(per.getInf()); } } 运行结果为:____
- 【填空题】阅读程序写结果 public class Student { private static int sid = 0; private String name; int id; Student(String name) { this.name = name; id = sid++; } public void info() { System.out.println("My name is "+name+" No."+id); } public static void infoSta() { System.out.println(sid); } public static void main(String arg[]) { Student.sid = 100; Student st1 = new Student("huahua"); st1.sid = 2000; Student st2 = new Student("meimei"); st1.info(); st2.info(); } }
- 在C#中,下列代码的运行结果是( )(选一项)[br][/br] struct Student { public int age; public string name; public Student(int age,string name) { this.sge = sge; this.name = name; } } public class Test { static void Main() { Student stu1 = new Student(18,"小芳"); Student stu2 = new Student(24,"小刚"); stu2 = stu1; stu1.age = 30; stu1.name = "小燕"; Console.WriteLine(stu2.age); Console.WriteLine(stu2.name); } } A: 18 小芳 B: 18 小燕 C: 30 小燕 D: 30 小芳