【问题标题】:How do I display name, from serialized object如何从序列化对象中显示名称
【发布时间】:2016-01-06 23:31:48
【问题描述】:

我还是一个新手,所以请原谅我缺乏知识。我试图保存一个字符串(经理姓名),该字符串通过扫描仪类作为输入。这有效,我也序列化了字符串,但是当我反序列化字符串时,显示的是内存地址,而不是管理器名称。

如何显示经理名称,从学习的角度来看,为什么我保存的字符串在序列化后默认为内存地址。下面是一些代码sn-ps,(请不要提及尝试资源,我正在学习,最终会这样做!) 在此先感谢:)

import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class ReadClub{


    public void tryReadClub(ClubInfo club, MainMenu getFullName){

    String fileName = "/home/cg/root/savedgames/" + club.clubID + " " + club.teamName;

    try
      {
         FileInputStream fileIn = new FileInputStream("" + fileName + ".ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         club = (ClubInfo) in.readObject();
         getFullName = (MainMenu) in.readObject();    
         in.close();
         fileIn.close();

      }catch(IOException i)
      {
      //   i.printStackTrace();

      System.out.println("Not a valid number");
      return;

      }catch(ClassNotFoundException c)
      {
         System.out.println("Club class not found");
         c.printStackTrace();
        return;
      }

      System.out.println("Saved game loaded...");

      System.out.println("Name: " + club.teamName);
      System.out.println("Stadium: " + club.stadium);
      System.out.println("Division: " + club.division);
     // System.out.println("SSN: " + club.SSN);
      System.out.println("Stadium Capacity: " + club.stadiumCapacity);
      System.out.println("Manager Name :" + getFullName);
    }

}

public class DeSerializer extends ReadClub
{
    public void DeSerialize(ClubInfo club){

MainMenu pass_choice2 = new MainMenu();
    int passed_choice = pass_choice2.savedTeamList(club);

MainMenu getFullName = new MainMenu();
     if(passed_choice==1){tryReadClub(club, getFullName);}
}
--------------------------------------------------------------------------------

// This method is held in my MainMenu class

public String createProfile(){

System.out.println("Please enter your first name: ");
Scanner in = new Scanner(System.in);     
    System.out.println("\n");
    String firstName = in.nextLine();
    System.out.println("Please enter your surname: ");
    String surname = in.nextLine();
    String fullName = firstName + surname;

    System.out.println("Your full name is:" + fullName);
    return fullName;
} // end createProfile

【问题讨论】:

  • 您要打印的经理名称是 MainMenu 类型的对象,这可能是它不打印为字符串的原因。
  • 你能分享你的 ClubInfo 和 MainMenu 课程吗?您是否将这些对象写入文件?他们实现可序列化吗?
  • 要么从 MainMenu 对象中获取管理器名称,要么在那里实现适当的 toString() 方法。此外,将值保存到方法参数中不是一个好习惯
  • @JanHruby 当然它包含一个 ToString() 这是对象的一部分 (docs.oracle.com/javase/7/docs/api/java/lang/Object.html) 但它不会被 ops MainMenu 对象覆盖。
  • @Blaatz0r 你真的认为 Java 中的每个对象都有从 Object 类继承的默认 toString() 方法这一事实值得一提吗?

标签: java deserialization


【解决方案1】:

因为在这种情况下 getFullName 是一个对象 MainMenu。默认情况下,Java 中的 ToString() 是内存地址。 (Why does the default Object.toString() return a hex representation of the hashCode?)

System.out.println("Manager Name :" + getFullName);

如果您的 MainMenu 有一个属性或 getter 方法,那么您应该调用它。

例如。 getFullName.GetManagerName()

从我的角度来看,名称 getFullName 选择不当。由于它不包含 fullName,它包含一个 MainMenu 对象。因此,如果您将其更改为 mainMenu,那将是一个更好的名称。

然后你可以有类似的东西:

mainMenu.GetFullName() 

mainMenu.GetManagerName()

【讨论】:

  • 我确实有 getter 和 setter,我现在使用了“getter”,它第一次工作。我同意我对事物的命名,我发现很难想到更具体和更有意义的东西。无论如何,谢谢你的帮助:)
  • 我可以与 Darren 的命名问题联系起来。随着时间的推移,它会变得更容易。祝你的项目好运:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
相关资源
最近更新 更多