Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
  
 
 
 
 
 
Lab Session II 
          Object-Oriented Specification 
  Lecturer: Dr. K. Lano 
     
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Reload 
To reload previous work, select “Recent” from the tool bar. 
 
    
 
 
Generating Java 
Java code can be generated from this model by the option Generate Java in the 
‘Synthesis’ menu as shown in Figure 2. 
 
 
 
      Figure 1: 
      Figure 2: 
The code consist of Java classes of each class of Figure 2, in a file Controller.java, 
together with the main class, Controller, of the system. A file SystemTypes.java 
contains an interface with utility operations (for com- puting OCL expressions), and 
ControllerInterface.java contains an interface for Controller. A file GUI.java defines a 
simple Swing interface for the system, with buttons for each use case. The file 
Controller.java is also displayed to the user (Figure 3). 
 
 
 
 
 
 
 
 
 
Adding an Operation 
To add an operation to the class Student click ‘Edit’ on the menu bar and select 
‘Modify’. Then click on the “Student” diagram box and select ‘Add Operation’ and 
      Figure 3: 
press Ok. First add an integer attribute “year” to Student, then open up the Add 
Operation dialog: 
Figure 4: 
 
 
 
The following window will open.  
 Figure 5: 
 Fill the form in the following manner. 
 Name: progress 
 Type: none 
 Parameters: none(leave it blank) 
 Pre: true 
 Post: year=year@pre+1 
 Select Update 
 
The resulting class diagram should be similar to the diagram in the following figure. 
 Figure 6: 
 
 
 Remember to add ‘year’ as an attribute for class Student (type integer). 
 
Inheritance 
Inheritance is one of the characteristic aspects of object-oriented notations, which 
distinguishes them from earlier modeling languages such as entity-relationship 
diagrams. 
 
Example: every HumanPlayer is also a Player: inheritance represents “is-a” relation 
between objects, in contrast to an association, which represents “has-a” relation. 
Semantically, set of objects of HumanPlayer is always subset of those of Player.  
 
 
 
Figure 7: 
 
 
Player is called superclass and HumanPlayer subclass in this relationship. 
HumanPlayer is said to inherit from Player: each feature (attribute, operation or 
association) of Player is also implicitly a feature, with same name and type, of 
HumanPlayer, and does not have to be written explicitly on subclass. 
 
*Common mistake: repeating all features of superclass on a subclass. 
 Continuing with our example. We want to have 2 subclasses (BScStudent and 
MScStudent) class that inherent from the “Student” class. 
Create classes with the following names: BScStudent and MScStudent. Then select 
“Create” from the menu bar and select “Inheritance”. Remember since the “Student” 
is the superclass, therefore the target of the inheritance arrow must be pointing at it. 
 
Subclass extends from the Superclass 
Generate Java code of your class diagram to see how inheritance looks in the actual 
code. The following figure is an example of inheritance in Java. 
 Figure: 8 
 Saving your work 
 
In order to save your work, select “File” from the toolbar and click on “Save” option 
as shown in the following figure. 
 
  
  
 
      Figure 9: