- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的类功能,目前作为案例类实现
case class Feature(value :String)
有多种操作可以用不同的属性装饰特征,例如,有一个函数可以计算特征的出现次数,所以我可能需要一个 CountedFeature。除了计数之外,我可能还需要一个 WeightedFeature、一个 IndexedFeature 等等。
我的直觉说它适合 traits 所以我定义了以下 traits
trait Counted {def count :Long}
trait Weighted {def weight :Double}
trait Indexed {def index :Int}
这会弹出两个问题:1. 我是否需要创建一个具体类来实现每个特征组合(例如,实现 CountedWeightedFeature、CountedIndexedfeature 等),或者是否有某种方法可以避免它。如果我转向更多的装饰品,就不可能为所有组合维护类(class)。2. 我想设计一个函数,根据特征的数量对特征进行加权。它的签名应该类似于:
def computeWeightsByCount(features :List[T <: Feature with Counted]) :List[T with Weighted] = {...}
这里的 T 可能是索引的,也可能不是索引的,所以这个函数应该有某种方法来获取一个类并实例化一个新类,该新类具有堆叠在内部的原始类的所有特征加上一个额外的特征。
在 Scala 中是否有一些优雅的方法可以做到这一点,或者我应该完全重新考虑这种设计?
最佳答案
这个设计在我看来很好,除了不推荐扩展案例类。可以在此处找到原因的简要总结:https://stackoverflow.com/a/12705634/2186890
所以你可能想要重写 Feature
就像这样:
trait Feature { def value: String }
现在你可以像这样为模式匹配等定义案例类:
case class CountedFeature(value: String, count: Long) extends Feature with Counted
没有简单的方法可以避免像这样的案例类的组合爆炸,但您可以使用 Feature with Counted
等类型无论你喜欢哪里。请记住,您可以轻松创建匹配类型 Feature with Counted
的对象。在飞行中。例如:
val x: Feature with Counted = new Feature with Counted { val value = ""; val count = 0L }
实现 computeWeightsByCount
像你想要的那样有点棘手,因为没有简单的方法来构建 T with Weighted
不知道更多类型 T
.但它可以用隐式方法来完成。本质上,我们需要有一个定义的路径来生成 T with Weighted
来自T
对于每个 Feature with Counted
您要将此方法应用到的对象。例如,我们从这个开始:
trait Feature { def value: String }
trait Counted { def count: Long }
trait Weighted { def weight: Double }
trait Indexed { def index: Int }
我们要定义computeWeightsByCount
就像您在问题中所做的那样,但也采用了一种隐式方法,该方法采用 T
和重量,并产生一个 T with Weighted
:
def computeWeightsByCount[
T <: Feature with Counted](
features: List[T])(
implicit weighted: (T, Double) => T with Weighted
): List[T with Weighted] = {
def weight(fc: Feature with Counted): Double = 0.0d
features map { f => weighted(f, weight(f)) }
}
现在我们需要定义一个隐式方法来从输入特征中生成加权特征。让我们从获取 Feature with Counted with Weighted
开始来自Feature with Counted
.我们将把它放在 Feature
的伴随对象中:
object Feature {
implicit def weight(fc: Feature with Counted, weight: Double): Feature with Counted with Weighted = {
case class FCW(value: String, count: Long, weight: Double) extends Feature with Counted with Weighted
FCW(fc.value, fc.count, weight)
}
}
我们可以这样使用它:
case class FC(value: String, count: Long) extends Feature with Counted
val fcs: List[Feature with Counted] = List(FC("0", 0L), FC("1", 1L))
val fcws: List[Feature with Counted with Weighted] = computeWeightsByCount[Feature with Counted](fcs)
对于您想要为其计算加权计数的任何类型,您需要定义一个类似的此类隐式方法。
不可否认,这远不是一个完美的解决方案。所以是的,你是对的,你可能想重新考虑设计。然而,这种方法的优点是对 Feature
的任何进一步扩展无需对 computeWeightsByCount
进行任何更改即可创建“层次结构” .编写新特征的人也可以提供适当的隐式方法。
关于Scala 向类添加可堆叠属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23442248/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!