Java学习之路--Object类并重写toString
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 测试Object类并重写toString
* @author 葛宇
*/
package 面向对象核心;

public class TestObject {
public static void main(String[] args) {
TestObject obj = new TestObject();
System.out.println(obj.toString());

People ple = new People("Kevin",18);
System.out.println(ple);
}

@Override
public String toString() {
return "测试Overtide Object类的toString方法";
}
}

class People{
int age;
String name;

@Override
public String toString() {
return name+" 年龄:"+age;
}

public People(String name,int age) {
this.name = name;
this.age = age;
}
}
文章作者: GeYu
文章链接: https://nuistgy.github.io/2020/01/28/Java学习之路(30)/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yu's Blog