java类中的toString构造方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_43316411/article/details/89376549

贴代码:
1.创建的类:

public class TestToString {
	private String name1;
	private String name2;
	private String name3;
	private String name4;
	
	public TestToString() {}
	
	
	public TestToString(String name1, String name2, String name3, String name4) {
		this.name1 = name1;
		this.name2 = name2;
		this.name3 = name3;
		this.name4 = name4;
	}
	public String getName1() {
		return name1;
	}
	public void setName1(String name1) {
		this.name1 = name1;
	}
	public String getName2() {
		return name2;
	}
	public void setName2(String name2) {
		this.name2 = name2;
	}
	public String getName3() {
		return name3;
	}
	public void setName3(String name3) {
		this.name3 = name3;
	}
	public String getName4() {
		return name4;
	}
	public void setName4(String name4) {
		this.name4 = name4;
	}


	@Override
	public String toString() {
		return this.name1 + "," + this.name2 +  "," + this.name3 +  "," + this.name4;
	}

测试类:

package flow;

public class Test2 {

	public static void main(String[] args) {
		TestToString tt = new TestToString("liu","qian","yang","min");
		System.out.println(tt);

	}

}

输出结果为:

liu,qian,yang,min

但是要是第一个类中没有重写toString构造方法,输出结果是:

flow.TestToString@15db9742

返回的应该是默认的toString方法输出的东西,反正看不懂。

展开阅读全文

没有更多推荐了,返回首页