English 中文(简体)
我能否使用一种按先期法约束的类型,然后在子类中“加强”定义?
原标题:Can I use a type bound on a Scala abstract method and then "tighten up" the definition in a subclass?
  • 时间:2011-10-05 19:39:12
  •  标签:
  • scala
  • types

我基本上想做这样的事情:

class Shape

class CoordSystem
class C3D(val x: Double, y: Double, z: Double) extends CoordSystem
class C2D(val x: Double, y: Double) extends CoordSystem

abstract class Shape {
  def getCoords[C <: CoordSystem]: List[C]
} 

class Pyramid extends Shape {
  def getCoords: List[C3D] = 
    List(new C3D(1,2,1), new C3D(1,1,1), new C3D(2,2,1), new C3D(2,1,1), new C3D(1.5,1.5,3))
}   
>> error: class Pyramid needs to be abstract, since method getCoords in class Shape of type [C <: CoordSystem]List[C] is not defined

我在上看到了几个不同的想法,但其中没有一个意见对本案来说是正确的,因为它们似乎不让我在其他地方撰写提及myShape.Coords。 如在<代码>Shape下分类中正确定义,将物体清单从<代码>下分类。 CoordSystem。

我还发现,在Schala Lang电子邮件名单上就通用s进行了令人感兴趣的讨论,但不能将其与我的情况相重。

感激之情!

最佳回答

如何做这样的事情:

class CoordSystem
class C3D(val x: Double, y: Double, z: Double) extends CoordSystem
class C2D(val x: Double, y: Double) extends CoordSystem

trait ShapeLike[+C <: CoordSystem] {
  def getCoords: List[C]
}

abstract class Shape extends ShapeLike[CoordSystem]

class Pyramid extends Shape with ShapeLike[C3D] {
  def getCoords: List[C3D] =
    List(new C3D(1, 2, 1), new C3D(1, 1, 1), new C3D(2, 2, 1), new C3D(2, 1, 1), new C3D(1.5, 1.5, 3))
}

当然,你没有义务宣布额外类别ShapeLike;其目的是允许你在没有额外类型参数的情况下使用<代码>Shape。

So, actually the answer to your question as stated in the title is: you may “tighten up” the type bound of a type parameter in a subclass if it is defined as a covariant type parameter in the superclass; conversely, you may “loosen” the type bound of a contravariant type parameter.

问题回答

一类参数为一类参数,超级合同要求你不要收紧合同——使其不那么严格——不会违反合同,尽管我不认为允许这样做。

但是,你可以做其他事情:将这种类型放在该类。

class Shape

class CoordSystem
class C3D(val x: Double, y: Double, z: Double) extends CoordSystem
class C2D(val x: Double, y: Double) extends CoordSystem

abstract class Shape {
  type C <: CoordSystem
  def getCoords: List[C]
} 

class Pyramid extends Shape {
  override type C = C3D
  def getCoords: List[C3D] = 
    List(new C3D(1,2,1), new C3D(1,1,1), new C3D(2,2,1), new C3D(2,1,1), new C3D(1.5,1.5,3))
}   




相关问题
Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

Creating (boxed) primitive instance when the class is known

I need a method that returns an instance of the supplied class type. Let s assume that the supplied types are limited to such that an "empty" instance of them can be created. For instance, supplying ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

How to generate an instance of an unknown type at runtime?

i ve got the following in C#: string typename = "System.Int32"; string value = "4"; theses two strings should be taken to generate an object of the specified type with the specified value... result ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

热门标签