编程创建Student类为其定义成员变量有姓名name、学号StuNo、年龄age 性别sex;成员方法——learn定义听课的方法在控制台直接输出具有的功能是能够输出name+正在认真听课 doHomework方法功能返回其现在是北京时间:+hour+点+name+正在写作业具体步骤如下。1在chapter05包中创建类Student 在类体中定义成员变量String stuID;String
代码如下:
//Student类 package chapter05;
public class Student { String stuID; String name; int age; String sex;
public void learn() {
System.out.println(name + "正在认真听课");
}
public String doHomework(int hour) {
return "现在是北京时间:" + hour + "点," + name + "正在写作业";
}
}
//StudentTest类 package chapter05;
public class StudentTest { public static void main(String[] args) { Student student1 = new Student(); student1.learn(); System.out.println(student1.doHomework(22));
student1.stuID = "001";
student1.name = "张三";
student1.age = 18;
student1.sex = "男";
System.out.println("姓名:" + student1.name);
System.out.println("学号:" + student1.stuID);
System.out.println("年龄:" + student1.age);
System.out.println("性别:" + student1.sex);
}
}
原文地址: https://www.cveoy.top/t/topic/6UD 著作权归作者所有。请勿转载和采集!