A UML class diagram can describe both:
Suppose a soccer Team class has a method that returns its score.
A class-diagram method entry can be written as:
GetScore() : intRead it as:
In:
GetScore() : intthe pieces are:
GetScoremethod name
()parameter list
intreturn type
The colon separates the method portion from the return type.
A class might include:
DisplayScore() : void
GetScore() : intThe first method performs behavior without returning a data value.
The second method returns an integer.
The UML notation lets you see that difference before reading the C# implementation.
UML:
GetScore() : intC#:
public int GetScore()
{
return this.score;
}The class diagram describes the method structure.
The C# contains the executable behavior.
The two representations should agree about:
Suppose:
team1.score = 3A call to GetScore() may produce:
3But the class diagram still shows:
GetScore() : intIt does not normally change to:
GetScore() : 3Why?
Because the class diagram describes the method's type structure for every Team object.
The value 3 belongs to one runtime situation.
You already know parameter notation such as:
SetScore(score : int) : voidA returning method can also have a parameter, although CO-043 keeps the examples simple.
The reading pattern is:
MethodName(parameterName : Type) : ReturnTypeThe parentheses describe information going into the method.
The final type describes information coming out.
Compare:
DisplayScore() : voidwith:
GetScore() : intA caller cannot expect a numeric result from DisplayScore().
A caller can expect an integer from GetScore().
The class diagram therefore communicates an important part of the method contract.
If the method returns:
intthe UML should show:
: intDo not change the UML to double because another numeric type seems more flexible.
The model and code should represent the same design.
A class can contain a field:
score : intand a method:
GetScore() : intThey use the same data type for different reasons.
The field:
score : intdescribes stored object state.
The method:
GetScore() : intdescribes behavior that produces an integer for the caller.
Conceptually, a Team class might include:
Team
-------------------------
name : string
score : int
-------------------------
DisplayScore() : void
GetScore() : intRead it as:
For a parameterless returning method:
MethodName() : ReturnTypeExample:
GetScore() : intThe class diagram answers: