【问题标题】:What does it mean when the return type on a UML Diagram is an object?当 UML 图上的返回类型是对象时,这意味着什么?
【发布时间】:2021-11-30 19:44:51
【问题描述】:

Chess 类有一个名为knighMoves(int i, int j): Chess 的方法,返回类型为Chess。方法头会是

public static Chess knightMoves(int i, int j)
{
   return
}

return 语句包含什么内容?

【问题讨论】:

  • 表示它的样子。该方法返回一个Chess的实例

标签: java class methods uml class-diagram


【解决方案1】:

您的模型告诉我们操作knightMoves() 不是静态的(静态成员应在图中带下划线),它返回一个Chess 对象。

您可能会感到困惑,因为返回类型也是封闭类。但这并没有改变任何东西。 java 方法看起来像:

public Chess knightMoves(int i, int j)
{
   Chess game;
   … // some code that would assign a value to game
       // and or change its state
   return game; 
}

或取决于您的设计意图:

public Chess knightMoves(int i, int j)
{
   … // some code that changes the current object
   return this; // return enclosing object
}

【讨论】:

  • 有道理。非常感谢!
猜你喜欢
  • 2014-07-08
  • 1970-01-01
  • 2014-04-03
  • 2013-01-16
  • 1970-01-01
  • 2013-07-03
  • 2021-03-01
  • 1970-01-01
  • 2012-10-08
相关资源
最近更新 更多