为什么输出会出现乱码呢?
RT。这是我同学托我请教大家的。他在国外读computer science本科,学到java class部分(就是用class去写程序)
他之前写的那个运行正常 但为什么第二个写的就出现了问题呢?(能运行,但运行出来的结果是乱码,底下会贴出)。
这是他写的程序(每一个class对应一个类)
eclipse写的。
class eye
{
private int r;
public double pi() {return 3.1416;}
public eye(int r1)
{
r=r1;
}
public int getR(){return r;}
public void setR(int r1){r=r1;}
public boolean issmall(){return r>10;}
public boolean close(){return r==0;}
}
class mouth
{
private int w,h;
public double pi(){return 3.1416;}
public mouth(int w1,int h1)
{
w=w1;
h=h1;
}
public int getW(){return w;}
public int getH(){return h;}
public void setW(int w1){w=w1;}
public void setH(int h1){h=h1;}
public boolean isbig(){return w>10||h>5;}
public boolean close(){return w==0||h==0;}
}
class nose
{
private int x,y,z;
public nose(int x1,int y1,int z1)
{
x=x1;
y=y1;
z=z1;
}
public int getX(){return x;}
public int getY(){return y;}
public int getZ(){return z;}
public void setX(int x1){x=x1;}
public void setY(int y1){y=y1;}
public void setZ(int z1){z=z1;}
public boolean isbig(){return x>10||y>10||z>10;}
public boolean cut(){return x==0||y==0||z==0;}
}
class head
{
private int r;
private int w,h;
private int x,y,z;
private int numHair;
private int k;
eye le;
eye re;
nose n;
mouth m;
public head(int r1,int w1,int h1,int x1,int y1,int z1)
{
r=r1;
w=w1;
h=h1;
x=x1;
y=y1;
z=z1;
}
public head(int nu,int k1,eye le1,eye re1,nose n1,mouth m1)
{
numHair=nu;
k=k1;
le=le1;
re=re1;
n=n1;
m=m1;
}
public int getNumHair(){return numHair;}
public int getK(){return k;}
public eye getle(){return le;}
public eye getre(){return re;}
public nose getN(){return n;}
public mouth getM(){return m;}
public void setle(eye le1){le=le1;}
public void setre(eye re1){re=re1;}
public void setn(nose n1){n=n1;}
public void setm(mouth m1){m=m1;}
public void setNumHair(int nu){numHair=nu;}
public void setK(int k1){k=k1;}
public boolean isnormal(){return le.getR()==re.getR();}
public boolean headache(){return le.close()&&re.close()&&m.close()&&n.cut();}
}
public class headtest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
head head1=new head(1,2,3,4,5,6);
System.out.println("The head's all of number is:"+head1);
}
}
理论上的输出应该是:The head's all of number is:123456
实际上输出是:The head's all of number is:head@120bf2c
请问这是为什么?哪里出了问题?
如果要改的话应该怎么改?
[解决办法]
head必须实现toString方法
public String toString()
{
return "" + r + w + h + x + y + z;
}
[解决办法]
class eye { private int r; public double pi() { return 3.1416; } public eye(int r1) { r = r1; } public int getR() { return r; } public void setR(int r1) { r = r1; } public boolean issmall() { return r > 10; } public boolean close() { return r == 0; }}class mouth { private int w, h; public double pi() { return 3.1416; } public mouth(int w1, int h1) { w = w1; h = h1; } public int getW() { return w; } public int getH() { return h; } public void setW(int w1) { w = w1; } public void setH(int h1) { h = h1; } public boolean isbig() { return w > 10 || h > 5; } public boolean close() { return w == 0 || h == 0; }}class nose { private int x, y, z; public nose(int x1, int y1, int z1) { x = x1; y = y1; z = z1; } public int getX() { return x; } public int getY() { return y; } public int getZ() { return z; } public void setX(int x1) { x = x1; } public void setY(int y1) { y = y1; } public void setZ(int z1) { z = z1; } public boolean isbig() { return x > 10 || y > 10 || z > 10; } public boolean cut() { return x == 0 || y == 0 || z == 0; }}class head { private int r; private int w, h; private int x, y, z; private int numHair; private int k; eye le; eye re; nose n; mouth m; public head(int r1, int w1, int h1, int x1, int y1, int z1) { r = r1; w = w1; h = h1; x = x1; y = y1; z = z1; } public head(int nu, int k1, eye le1, eye re1, nose n1, mouth m1) { numHair = nu; k = k1; le = le1; re = re1; n = n1; m = m1; } public int getNumHair() { return numHair; } public int getK() { return k; } public eye getle() { return le; } public eye getre() { return re; } public nose getN() { return n; } public mouth getM() { return m; } public void setle(eye le1) { le = le1; } public void setre(eye re1) { re = re1; } public void setn(nose n1) { n = n1; } public void setm(mouth m1) { m = m1; } public void setNumHair(int nu) { numHair = nu; } public void setK(int k1) { k = k1; } public boolean isnormal() { return le.getR() == re.getR(); } public boolean headache() { return le.close() && re.close() && m.close() && n.cut(); } public String toString() { return new StringBuffer().append(r).append(w).append(h).append(x).append(y).append(z).toString(); }}public class headtest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub head head1 = new head(1, 2, 3, 4, 5, 6); System.out.println("The head's all of number is:" + head1); }}
[解决办法]
在head类中加入 @Overridepublic String toString() { return "head:[r=" + r + ",w="+ w + ",h="+ h + ",x="+ x + ",y="+ y+ ",z="+ z+ "]" ;}
[解决办法]
这不是乱码 head类是继承Object类的
而这里是输出的Object类的toString()方法。
默认的toString()方法是
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
所以要输出想要的内容,head类里要重写toString方法。