public class HelloWorld { String name; String sex; static int age; public void HelloWorld () { System.out.println("通过构造方法初始化name"); name = "tom"; } { System.out.println("通过初始化块初始化sex"); sex = "男"; } { System.out.println("通过静态初始化块初始化age"); age = 20; } public void show() { System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age); } public static void main(String[] args) { HelloWorld hello = new HelloWorld(); Hello.show(); }}
1 回答
大咪
TA贡献785条经验 获得超332个赞
public HelloWorld () {
System.out.println("通过构造方法初始化name");
name = "tom";
}这样,删掉void
添加回答
举报
0/150
提交
取消