重写tostring()方法并传递对象引用而不获取哈希

时间:2017-12-03 19:52:05

标签: java arrays object reference method-overriding

我试图覆盖toString()方法并在没有哈希的情况下打印。当我在尝试打印时使用tostring()方法时,需要我创建另一个对象asset server = new asset();。当我执行此操作并仅使用server时,它将返回默认构造函数,或者在使用server[i]时出现错误。我写的代码返回:

 `ant -f C:\\Users\\kalle\\Documents\\NetBeansProjects\\assign7 -
  Dnb.internal.action.name=run run
  init:
  Deleting: C:\Users\kalle\Documents\NetBeansProjects\assign7\build\built-
  jar.properties
  deps-jar:
  Updating property file: 
  C:\Users\kalle\Documents\NetBeansProjects\assign7\build\built-
  jar.properties
  compile:
  run:
  server switch etc.
  0
  vendor model number
  serial number
  1

  driver.asset@15db9742
  server
  server0
  10
  a1
  1
   driver.asset@15db9742
   server
  server1
  20
  a2
  1
  BUILD SUCCESSFUL (total time: 0 seconds)`

这是我的代码:`

 package driver;

public class Driver {

    public static void main(String[] args) {
        int i;
        asset basic = new asset();
        asset[] server = new asset[2];
        server[0]= new asset("server", 10, "server0", "a1", 1);
        server[1]= new asset("server", 20, "server1", "a2",1);
        for (i=0;i<server.length;i++){
            System.out.println(basic);
            System.out.println(server[i].Type);
            System.out.println(server[i].Name);
            System.out.println(server[i].ID);
            System.out.println(server[i].Serial);
            System.out.println(server[i].PID);
        }
    }
}

public class asset {
    String Type;
    int ID;
    String Name;
    String Serial;
    int PID;
    int i;

    public String getType(){
        return Type;
    }

    public void setType(String Type){
        this.Type = Type;
    }

    public int getID(){ 
        return ID;
    }

    public void setID(int getid){
        Integer.toString(ID);
        this.ID= ID;
    }

    public String getName(){
        return Name;
    }

     public void setName(String Name){
         this.Name = Name;
     }

     public String getSerial(){
         return Serial;
     }

    public void setSerial(String Serial){
        this.Serial = Serial;
    }

    public int getPID() {        
        return PID;    
    }

    public void setPID(int getPID) {
        Integer.toString(PID);
        this.PID= PID;     
    }

    public asset(){
        Name ="vendor model number";
        ID =0;
        Serial ="serial number";
        Type ="server switch etc.";
        PID =1;
        System.out.println( Type + "\n " + ID + "\n " + Name + "\n " + Serial + "\n " + PID + "\n ");}

        asset(String Type, int ID, String Name, String Serial, int PID){
        this.Type= Type;
        this.Name= Name;
        this.PID= PID;
        this.Serial= Serial;
        this.ID= ID;        
    }

    public String toString (asset[] a){
        getType();
        getID();
        setID(ID);
        getName();
        getSerial();
        getPID();
        setPID(PID);

       return this.Type + " " + this.ID + " " + this.Name + " " + this.Serial + " " + this.PID + " ";
    }
}

我如何摆脱&#34; driver.asset@15db9742"。

2 个答案:

答案 0 :(得分:0)

覆盖toString方法的正确方法是具有以下结构:

public String toString(){
    // return String type
}

对你而言(致电getXY()不做任何事):

public String toString (){  
   return this.Type + " " + this.ID + " " + this.Name + " " + this.Serial + " " + this.PID + " ";
}

另外,为了遵循惯例,你最好:

  • CamelCase 中的名称类:asset - &gt; Asset
  • name attributs(&amp; parameter&amp; variables)跟随 camelCase typeidnamepid

答案 1 :(得分:0)

试试这个:

int i;
asset basic = new asset();
asset[] server = new asset[2];
server[0]= new asset("server", 10, "server0", "a1", 1);
server[1]= new asset("server", 20, "server1", "a2",1);
System.out.println(basic); // just delete this line.
for (i=0;i<server.length;i++)
{
System.out.println(server[i].Type);
System.out.println(server[i].Name);
System.out.println(server[i].ID);
System.out.println(server[i].Serial);
System.out.println(server[i].PID);}

}

你不断打印出基本的值,这可能是为什么它产生了字符串哈希名而不是实际存储值。只需要使用System.out.println(基本);出于for循环?更好的是,完全删除该行!