Scala:是否有返回子对象的函数';谁的父母?

Scala:是否有返回子对象的函数';谁的父母?,scala,Scala,假设我们有两个对象,A和B,它们具有父子关系。比如: A.child = B 是否有一个函数或某种方法可以在Scala中实现这样的函数: B.getParent() //returns A 一种解决方案是按名称使用参数和lazy val,如下所示 class TreeNode[E](l: => Option[TreeNode[E]], value: E, r: => Option[TreeNode[E]], p: => Option[TreeNode[E]]

假设我们有两个对象,A和B,它们具有父子关系。比如:

    A.child = B
是否有一个函数或某种方法可以在Scala中实现这样的函数:

    B.getParent() //returns A

一种解决方案是按名称使用参数和
lazy val
,如下所示

class TreeNode[E](l: => Option[TreeNode[E]], value: E, r: => Option[TreeNode[E]], p: => Option[TreeNode[E]]) {

  lazy val left: Option[TreeNode[E]] = l

  lazy val right: Option[TreeNode[E]] = r

  lazy val parent: Option[TreeNode[E]] = p

}

// must explicitly define the types for left and right!
val left: TreeNode[Int] = new TreeNode(None, 6, None, Some(root))
val right: TreeNode[Int] = new TreeNode(None, 15, None, Some(root))
val root = new TreeNode(Some(left), 10, Some(right), None)
请注意,中不允许使用按名称参数。

Scala没有管理此类关系的内置功能

但是,将父成员添加到
B
的构造函数并按如下方式填充它是很简单的:

在这种特殊情况下,
A
还负责构造
B
,这使得
A
B
都是不可变的

这个主题有很多变化

一种更通用的方法是使用外部映射来跟踪关系,这种方法允许
A
B
的实例不那么紧密地耦合。以下是如何以功能性方式实现这一点的示例:

class A {
  // ...
}

class B {
  // ...
}

// Constructor is private to require construction through companion's factory method.
class Relationships private(private val parentToChild: Map[A, B], private val childToParent: Map[B, A]) {

  // Return new relationship that adds relationship between child and parent instance.
  //
  // This particular approach assumes a one-to-one mapping (one child per parent).
  // One-to-many (multiple children per parent) are possible by mapping the parent to a
  // collection of children.
  def relate(parent: A, child: B): Relationships = {

    // Verify that neither parent nor child currently have relationships.
    require(!parentToChild.contains(parent) && !childToParent.contains(child))

    // Add the relationship, return new relationships instance.
    val newParentToChild = parentToChild + (parent -> child)
    val newChildToParent = childToParent + (child -> parent)
    new Relationships(newParentToChild, newChildToParent)
  }

  // Lookup child of particular parent. Return None if no child found, Some(child) otherwise.
  def childOf(parent: A): Option[B] = parentToChild.get(parent)

  // Lookup parent of particular child. Return None if no parent found, Some(parent) otherwise.
  def parentOf(child: B): Option[A] = childToParent.get(child)
}

// Companion.
object Relationships {

  // Create initial relationship instance with no relationships.
  def apply() = new Relationships(Map.empty[A, B], Map.empty[B, A])
}

// Sample use:
val a = new A
val b = new B
val tracker = Relationships().relate(a, b)
tracker.childOf(a) // Should return Some(b).
tracker.parentOf(b) // Should return Some(a).

有,但你需要写一个。这个问题毫无意义。你能再详细一点吗?
class A {
  // ...
}

class B {
  // ...
}

// Constructor is private to require construction through companion's factory method.
class Relationships private(private val parentToChild: Map[A, B], private val childToParent: Map[B, A]) {

  // Return new relationship that adds relationship between child and parent instance.
  //
  // This particular approach assumes a one-to-one mapping (one child per parent).
  // One-to-many (multiple children per parent) are possible by mapping the parent to a
  // collection of children.
  def relate(parent: A, child: B): Relationships = {

    // Verify that neither parent nor child currently have relationships.
    require(!parentToChild.contains(parent) && !childToParent.contains(child))

    // Add the relationship, return new relationships instance.
    val newParentToChild = parentToChild + (parent -> child)
    val newChildToParent = childToParent + (child -> parent)
    new Relationships(newParentToChild, newChildToParent)
  }

  // Lookup child of particular parent. Return None if no child found, Some(child) otherwise.
  def childOf(parent: A): Option[B] = parentToChild.get(parent)

  // Lookup parent of particular child. Return None if no parent found, Some(parent) otherwise.
  def parentOf(child: B): Option[A] = childToParent.get(child)
}

// Companion.
object Relationships {

  // Create initial relationship instance with no relationships.
  def apply() = new Relationships(Map.empty[A, B], Map.empty[B, A])
}

// Sample use:
val a = new A
val b = new B
val tracker = Relationships().relate(a, b)
tracker.childOf(a) // Should return Some(b).
tracker.parentOf(b) // Should return Some(a).