- class Test2_Student {
- public static void main(String[] args) {
- Student s1 = new Student();
- s1.setName("张三");
- s1.setAge(23);
- s1.setGender("男");
- System.out.println(s1.getName()+"..."+s1.getGender()+"..."+s1.getAge());
- s1.study();
- s1.sleep();
- System.out.println("------------");
- s1.speak();
- }
- }
- /*
- 属性:姓名,年龄,性别
- *行为:学习,睡觉
- */
- class Student {
- private String name;
- private int age;
- private String gender;
- //设置及获取名字
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- //设置并获取年龄
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- //设置并获取性别
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getGender() {
- return gender;
- }
- //定义学习和睡觉方法
- public void study() {
- System.out.println("学生学习");
- }
- public void sleep() {
- System.out.println("学生睡觉");
- }
- public void speak() {
- System.out.println("我叫"+name+",性别"+gender+",今年"+age+"岁");
- this.study();
- this.sleep();
- }
- }
复制代码 |
|