通用替代原则 - 如何管理原则的应用(Generic substitution principle - how application of principle is governed)
在Naftalin和Wadler的文本“Java Generics and Collections”中,一段说明尽管Integer是Number的子类型,但List <Integer>不是List <Number>的子类型。
这可以防止人们使用多态方式引用传统上可能允许这样的语句的地方。
我的问题是,由于“替换原则”不适用于List <Integer>和List <Number>的情况,原则上的限制是否与类和特定类型(分配给泛型类型)的位置有关。 组合 - 在所有情况下和一般情况下 - (例如,“List <Integer>”)。 在这种情况下,我将类似于“List <Integer>”的语句中的替代概念称为“List”,而不是“List”或“<Integer>”seperatley。
或者,限制是通过某种机制来定义的,该机制通过通常的扩展和实现机制来指定是否以及哪些类是子类型(因此何时何地应用)。
基本上我不理解在这种情况下将扣押原则定义为适用或不适用的机制。
非常感谢
from the text "Java Generics and Collections" by Naftalin and Wadler, a passage states that though Integer is a subtype of Number, List<Integer> is not a subtype of List<Number>.
This prevents one from using, polymorphically, references to the in places where one might traditionally expect to allow such statements.
My question here is, since the 'substitution principle' does not apply in the case List<Integer> and List<Number>, does the restriction on the principle relate to where a class and a specific type (assigned for a generic type) are combined - in all cases and in general - (here as 'List<Integer>' for example). Here in this case I refer to the notion of substiution in resepct to a statement like 'List<Integer>', as opposed to 'List', or '<Integer>' seperatley.
Or alternatively, is the restriciton instead defined through some mechanism that specifies whether and which classes are subtypes (and thus when and when it applies) as one does through the usual extends and implements mechanism.
Essentially i do not understand the mechanism through which the susbtitution principle in such cases are caused to be defined as applying or not applying.
many thanks
原文:https://stackoverflow.com/questions/37070331
最满意答案
Case
开关可能最适合这个:Select Case Range("A1").Value Case "Element1", "Element2", "Element3", "Element4", "Element5" 'Do stuff Case Else 'Do other stuff, or do nothing at all End Select
请注意,以上是区分大小写的。 如果你有混合大小写并且不关心大小写,请使用
LCASE
或UCASE
函数转换为单个大小写:Select Case LCase(Range("A1").Value)) Case "element1","element2","element3","element4","element5"
或者,您可以在向量数组上使用过滤器函数:
Const Elements As String = "element1,element2,element3,element4,element5" Dim val$, myArray myArray = Split(Elements, ",") val = "element3" If UBound(Filter(myArray, val)) >= 0 Then 'the value is in the array MsgBox True Else 'the value is not in the array MsgBox False End If
虽然有一些
Filter
功能的怪癖,我认为它并不总是100%可靠,因为它将返回部分匹配。或者您可以对数组使用
Match
函数:If Not IsError(Application.Match(val, myArray, False)) Then 'value exist in the list Else 'value does not exist in the list End If
A
Case
switch is probably best-suited for this:Select Case Range("A1").Value Case "Element1", "Element2", "Element3", "Element4", "Element5" 'Do stuff Case Else 'Do other stuff, or do nothing at all End Select
Note that the above is case-sensitive. If you have mixed-case and don't care about capitalization, convert to a single case using the
LCASE
orUCASE
function:Select Case LCase(Range("A1").Value)) Case "element1","element2","element3","element4","element5"
Alternatively, you can use the filter function on a vector array:
Const Elements As String = "element1,element2,element3,element4,element5" Dim val$, myArray myArray = Split(Elements, ",") val = "element3" If UBound(Filter(myArray, val)) >= 0 Then 'the value is in the array MsgBox True Else 'the value is not in the array MsgBox False End If
There are some quirks with the
Filter
function though, and I think it is not always 100% reliable since it will return partial matches.Or you can use the
Match
function against an array:If Not IsError(Application.Match(val, myArray, False)) Then 'value exist in the list Else 'value does not exist in the list End If
相关问答
更多-
这可以在没有AutoFilter的情况下完成: Dim r As Range For Each r In FindRange v = r.Value If InStr(v, "Consumer") > 1 Or _ InStr(v, "IENCE") > 0 Or _ InStr(v, "Non ICG") > 0 Or _ InStr(v, "Not past") > 0 Or _ InStr(v, "Not a past") ...
-
请记住,列表框是零索引的,所以你必须从i中减去一个: For i = 1 To allGrps lstAllGroups.Selected(i - 1) = True Next i 此外,请确保您有SelectSheetsList.MultiSelect = fmMultiSelectMulti ,以便能够在列表中选择多个项目。 有关详细信息,请参阅ListBox文档 。 Keep in mind that the listbox is zero-indexed so you've got to ...
-
Excel VBA - 为图表数据选择多个表列(Excel VBA - Selecting multiple table columns for purposes of chart data)[2022-04-21]
包括这样的代码: Dim rngData As Range With ActiveSheet.ListObjects("Table1").DataBodyRange Set rngData = Union(.Columns(2), .Columns(9), Columns(10), _ .Columns(11), .Columns(12)) End With myChart.SetSourceData Source:=rngData, PlotBy:=xlColumns Inc ... -
Excel VBA Autofilter - “开始于”的多个标准(Excel VBA Autofilter - Multiple Criteria of “begin with”)[2023-02-20]
注意:接受的答案,但是OP使用问题中前面提到的Gary的答案 假设我们有如下数据: 我们希望看到以A , D或M开头的项目。 运行这个宏: Sub ThreeWay() Dim rng As Range, r As Range Set rng = Range("A2:A25") For Each r In rng v = Left(r.Value, 1) If v = "A" Or v = "D" Or v = "M" Then ... -
Case开关可能最适合这个: Select Case Range("A1").Value Case "Element1", "Element2", "Element3", "Element4", "Element5" 'Do stuff Case Else 'Do other stuff, or do nothing at all End Select 请注意,以上是区分大小写的。 如果你有混合大小写并且不关心大小写,请使用LCASE或UCASE函数转 ...
-
是的,只需结合多个标准。 例如: strCriteria = "[PlantID]=" & Me!PlantID & " AND [Location]= '" & Me!Location & "'" 这里,第一个参数PlantID是一个整数,第二个是Location ,一个字符串(因此是单引号)。 Yes, simply combine the multiple criteria. For example: strCriteria = "[PlantID]=" & Me!PlantID & " AND [ ...
-
编辑 - 根据评论修复比较 您可以通过简单检查Criteria vaiables来完成此操作。 您已经进行了长度检查 - 稍后在构建动态SQL时使用它。 我添加了一个strSQL变量,以便更容易跟踪发生的事情。 并调整错误消息以允许一个或其他标准为空 Private Sub Command6_Click() Dim Q As QueryDef Dim DB As Database Dim Criteria As String Di ...
-
用于选择具有偏移条件的多个不同活动单元的vba(vba for selecting multiple different active cells with offset condition)[2023-02-21]
如果选择a1并运行代码,它将选择a1和b1。 如果我选择a1和a3和a7,则应选择单元格a1b1和a3b3以及a7b7。 更新: 跟进评论: Sub test1() Dim rng As Range Dim ar As Range Dim rngAE As Range, cell As Range With Selection For Each ar In .Areas If rng Is Nothing Then ... -
Excel VBA:查找具有多个条件的单元格的值的方法(Excel VBA: Way to find the value of a cell with multiple criteria)[2024-02-13]
简单方法怎么样? Function find(ByVal criteria1 As Date, ByVal criteria2 As Integer) As Variant For i = 2 To 300001 If Cells(i, 1).Value = criteria1 Then If Cells(i, 2).Value = criteria2 Then find = Cells(i, 3).Value ... -
如果你改变这个,我认为你的解决问题的努力会更容易...... rst.FindLast ("DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'") 这样的事...... Dim strFind As String strFind = "DONOR_CONTACT_ID= 'strDonor1' AND ORDER_NUMBER= 'strOrderNum1'" Debug.Print strFind rst.FindLast str ...