4.1Java一个典型类的定义和UML图

4.1Java一个典型类的定义和UML图

本章内容

  • 典型类

  • UML图


    public static void main(String args[]){
       Student stu = new Student();
       //当输入:stu(对象名)+'.'的时候就会出现前面定义的属性和方法---涉及到继承的概念
       stu.play(); //调用play
  }
/*
在Student后的Student()这是什么?
new+关键字=建对象
实际上是:
调用了Student类的一个构造方法,通过构造方法创建类---这个方法是系统自动创建的。

注意:

  • 一个.java文件可以有多个类

  • 但是只能有一个由public修饰的类

  • 下面的实例两个类不是包含的关系!!!

实例:

/**
* 测试父类和子类的调用
* @author Lucifer
*/
public class StudentNo2 {
   //定义属性
   int id;
   String name;
   int age;
   Computer comp;


   void study(){
       System.out.println("I am study with computer:");
  }

   void play(){
       System.out.println("I am play game which name is King");
  }

   StudentNo2(){
  }


   public static void main(String[] args) {
       StudentNo2 stuNo2 = new StudentNo2();
       stuNo2.id = 8848;
       stuNo2.name = "Lucifer";
       stuNo2.age = 18;

       //用子类创建一个对象
//       Computer c1 = new Computer();
  }

   class Computer{
       //定义一个类的属性
       String brand;
  }
}

上面是类的互相引用

UML图

 

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/14609713.html