【示例4-3】模拟学生使用电脑学习

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class Computer {

    String brand;  //品牌

}

public class SxtStu {

    // field

    int id;

    String sname;

    int age;

    Computer comp;

    void study() {

        System.out.println("我正在学习!使用我们的电脑,"+comp.brand);

    }

    SxtStu() {

    }

    public static void main(String[] args) {

        SxtStu stu1 = new SxtStu();

        stu1.sname = "张三";

        Computer comp1 = new Computer();

         comp1.brand = "联想";

        stu1.comp = comp1;

        stu1.study();

    }

}

  执行结果:

一个典型类的定义

图4-2 示例4-3运行结果

  对应的UML图如下:

一个典型类的定义

图4-3 SxtStu和Computer的UML类图

测试代码:

一个典型类的定义

一个典型类的定义

一个典型类的定义

相关文章:

  • 2021-09-06
  • 2021-08-10
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-12-12
猜你喜欢
  • 2021-11-16
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-06-03
  • 2022-01-04
  • 2021-07-01
相关资源
相似解决方案