public class demo01 {
public static void main(String[] args) {
student xiaohong = new student();
xiaohong.id = 123456;
xiaohong.name = "小红";
xiaohong.sex = "男";
xiaohong.s1();
}
//定义一个学生类,他有三个属性,分别是id、name、性别
public static class student {
int id;
String name;
String sex;
//在学生类中定义一个方法,方法名叫s1
public void s1() {
if (sex == "男") {
System.out.println(name + "的学号是" + id +"\n"+ "他是个" + sex + "孩子");
} else if (sex == "女") {
System.out.println(name + "的学号是" + id +"\n"+ "她是个" + sex + "孩子");
} else {
System.out.println("这家伙是个机器人");
}
}
}
}
运行结果如下:
小红的学号是123456
他是个男孩子
进程已结束,退出代码为 0