第一阶段:JAVA 快速入门(第六十二课:JAVA类的定义和UML图)

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

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图如下:

2.png

图4-3 SxtStu和Computer的UML类图

发布了70 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ZGL_cyy/article/details/104111590