2008-09-23

Java 中,+ 号连接的多行文本,与单行文本的有区别吗?

在代码编写中,经常会有一长串的文本,为了阅读方便,一般是以多行输入的。在 C# 中,可以使用 @ 符来指定这是一个多行文本块,但是在 Java 中,并没有找到类似的语言特性,所以我只能用 + 号来连接多行文本,并以为这与单行文本,最终的处理方式是不一样的,会浪费 CPU 资源,进行字符串拼接。


如下面的代码:

public ArrayList getPage(EnterpriseForm formBean, Integer pageNo) throws Exception{
ArrayList result = new ArrayList();
// SQL文
SQLBuilder sql = new SQLBuilder("SELECT id, code, category, name, phone1, fax1, phone2, fax2, " +
"email, url, address, keyword, " +
"bank_name1, bank_account1, bank_account_name1, " +
"bank_name2, bank_account2, bank_account_name2, " +
"bank_name3, bank_account3, bank_account_name3, " +
"contact1_name, contact1_phone1, contact1_phone2, contact1_phs, " +
"contact1_mobile1, contact1_mobile2, contact1_email, " +
"contact1_qq, contact1_msn, contact1_skype, " +
"contact2_name, contact2_phone1, contact2_phone2, contact2_phs, " +
"contact2_mobile1, contact2_mobile2, contact2_email, " +
"contact2_qq, contact2_msn, contact2_skype, " +
"contact3_name, contact3_phone1, contact3_phone2, contact3_phs, " +
"contact3_mobile1, contact3_mobile2, contact3_email, " +
"contact3_qq, contact3_msn, contact3_skype, " +
"memo, " +
"regist_user_id, regist_time, update_user_id, update_time " +
"FROM enterprise");

但是一想,编译器难道不会作优化处理么?
为了验证,使用 jadnt158 进行了反编译,验证了这个想法。
结果不出所料,从而可以没有顾虑地使用多行字符串了。


上面代码反编译后的结果:

public ArrayList getPage(EnterpriseForm formBean, Integer pageNo)
throws Exception
{
ArrayList result = new ArrayList();
SQLBuilder sql = new SQLBuilder("SELECT id, code, category, name, phone1, fax1, phone2, fax2, email, url, address, keyword, bank_name1, bank_account1, bank_account_name1, bank_name2, bank_account2, bank_account_name2, bank_name3, bank_account3, bank_account_name3, contact1_name, contact1_phone1, contact1_phone2, contact1_phs, contact1_mobile1, contact1_mobile2, contact1_email, contact1_qq, contact1_msn, contact1_skype, contact2_name, contact2_phone1, contact2_phone2, contact2_phs, contact2_mobile1, contact2_mobile2, contact2_email, contact2_qq, contact2_msn, contact2_skype, contact3_name, contact3_phone1, contact3_phone2, contact3_phs, contact3_mobile1, contact3_mobile2, contact3_email, contact3_qq, contact3_msn, contact3_skype, memo, regist_user_id, regist_time, update_user_id, update_time FROM enterprise");

2008-09-22

偶见汉字[ "的" "地" "得"] 的讨论

上网浏览时,发现这些网友评论好有趣,虽然说离题万里,倒也是令人受教了。
比如下面这个(来源:http://www.cnbeta.com/articles/65289.htm


引用匿名人士发表于2008-09-22 14:42:40的评论:
引用webbe发表于2008-09-22 14:23:32的评论:
引用匿名人士发表于2008-09-22 13:53:04的评论:
引用webbe发表于2008-09-22 13:30:49的评论:
用Windows7画图工具的我得意的飘过。。。
第二个“的”,应该是“地”,“飘过”属于动词。
不巧西厢记有同类例:《西厢记诸宫调》(卷六):平生踪迹无定着,如断蓬。听塞鸿,哑哑的飞过暮云重。 不巧我是学汉语言文学的;不巧我导师又是吕叔湘的学生;不巧吕叔湘又主张“的”“地”合并;不巧又有杨秀君、李赓钧、田小琳、倪宝元、鞠复信等学者支持这 个观点;不巧1984年发布了《中学教学语法系统提要(试用)》中提倡不分“的”、“地”,都用“的”字;不巧我正好生在1986年就接受了这个教 育。。。太多的不巧了。
我95年小学毕业,当时老师对“地的得”三字的用法还强调的十分厉害,作文用错要扣分,何解楼上的86年通用的?难道九几年又改成不能通用了?但据说现在已经可以通用了,难道又改了?
“的”对于“地”“得”两字的合并从上世纪末就一直在学术界大讨论了。作为一种文化科学,是没有绝对的真理这种说法的,也就是说其实语言这种东西一直在随 着使用习惯的变化而不断变化,看看古代汉语到现代汉语的变化就能知道。所以一个字怎么用,可能在某几年是一定的,但是一旦有争议的话,则很可能发生变化。 对于这三个“的”“地”“得”,其实至今没有定论。小学的强调是为了能让孩子更好的掌握语法结构把握句子的意思,突出强调的内容。譬如“严厉的批评是必要 的”和“严厉地批评是必要的”,两句话都是正确的,但是因为助词“的”与“地”的不同所以强调的点不同,前者倾向于强调“批评”这一行为的必要,而后者倾 向于强调批评的程度——“严厉”的必要。 但是语言毕竟是一种工具,还是来用的,规范有规范的必要,但是一味的讲究规范而否定发展变化就是不对的,因此过分强调用法的行为只会在小学打基础的时候。 所以也就有了你说的一会儿强调一会儿又不强调的情况。 但这三个字也并不是所谓的“通用”,而是“的”作为助词的时候能在不给句子造成歧异的情况下能替代“地”与“得”,用通俗点的话来说就是现在这三个字不是 各管三分之一了,而是现在“的”范围变大了,能涵盖一部分的“地”和“得”了。 其实我们导师说的是,在一个结构你很把握不准的情况下,用“的”的话正确率一般会在75%以上。 《中学教学语法系统提要(试用)》是确实存在的,只是在可以不分但是分的话更利于学生、孩子掌握语句的情况下,多数小学的老师还是选择了强调分。仅此而 已。

2008-09-19

Excel add-in 备忘

两项机能:

1、自动添加工具栏按钮,文档关闭时,自动删除所添加的工具栏按钮
2、点击按钮图标,执行模块(Module)中的宏机能

注:在取得工具栏对象时,如果指定名称的工具栏不存在,会报错。下面代码的处理是对工具栏集合进行遍历,判断是否存在,当然,也可以直接使用异常处理(on error goto)来进行判断。

代码1(ThisWorkBook):

Option Explicit
Const toolbarCaption As String = "MyToolbar"
Const onActionString As String = "autoAdjust"

Private Sub Workbook_Open()
Dim toolbar As CommandBar

Set toolbar = getCommandBar(toolbarCaption)
If toolbar Is Nothing Then
Set toolbar = Application.CommandBars.Add(Name:=toolbarCaption)
toolbar.Visible = True
End If

With toolbar.Controls.Add(Type:=msoControlButton, Before:=1)
.Caption = "Auto Adjust"
.TooltipText = "Run Auto Adjust"
.OnAction = onActionString
ThisWorkbook.Worksheets(1).Shapes(1).Copy
.PasteFace
End With

Set toolbar = Nothing

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim toolbar As CommandBar
Dim i As Integer

Set toolbar = getCommandBar(toolbarCaption)
If toolbar Is Nothing Then
Exit Sub
End If

For i = 1 To toolbar.Controls.Count
With toolbar.Controls(i)
If .OnAction = "'" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & "'!" & onActionString Then
.Delete
Exit For
End If
End With
Next i

If toolbar.Controls.Count = 0 Then
toolbar.Delete
Set toolbar = Nothing
End If
End Sub

Private Function getCommandBar(barCaption As String)
Dim i As Integer

For i = Application.CommandBars.Count To 1 Step -1
If Application.CommandBars(i).Name = barCaption Then
Set getCommandBar = Application.CommandBars(i)
Exit Function
End If
Next i

Set getCommandBar = Nothing
End Function


代码2(Module):

Option Explicit

Sub autoAdjust()
Dim formulaString As String
Dim i As Integer
Dim sheet As Worksheet

Set sheet = ActiveSheet

'addjust column
sheet.Columns("E:Z").ColumnWidth = 8
sheet.Columns("G:G").Delete Shift:=xlToLeft
sheet.Columns("H:H").Delete Shift:=xlToLeft
sheet.Columns("I:J").Delete Shift:=xlToLeft
sheet.Columns("J:J").Delete Shift:=xlToLeft

' unmerge
Dim mergeCells, cell As Range
Dim formula As Variant
Dim iRow, iCol As Integer

For iRow = 3 To 100
For iCol = 1 To 15
Set cell = sheet.Cells(iRow, iCol)
If cell.mergeCells Then
Set mergeCells = cell.MergeArea
cell.UnMerge
formula = cell.formula
mergeCells.Cells.formula = formula
End If
Next iCol
Next iRow

'split size
'size formula format: =length * width * height
Dim lst() As String
For i = 3 To 100
formulaString = sheet.Cells.Item(i, "I").formula
If Len(formulaString) > 1 Then
lst = Split(Mid(formulaString, 2), "*")
If UBound(lst) <> 2 Then
With sheet.Range(sheet.Cells.Item(i, "I"), sheet.Cells.Item(i, "L"))
.formula = formulaString
.Interior.ColorIndex = 3
.Interior.Pattern = xlSolid
End With
Else
sheet.Cells.Item(i, "J") = lst(0) 'length
sheet.Cells.Item(i, "K") = lst(1) 'width
sheet.Cells.Item(i, "L") = lst(2) 'height
End If
End If
Next i

sheet.Columns("I:I").Delete Shift:=xlToLeft
sheet.Columns("A:B").Insert Shift:=xlToRight

sheet.Range("A3:A100") = Split(ActiveWorkbook.Name, ".")(0)
sheet.Range("B3") = 1
Range("B4").FormulaR1C1 = "=R[-1]C + 1"
Range("B4").AutoFill Destination:=Range("B4:B96"), Type:=xlFillDefault
Range("B3").Select

End Sub

(完)
By mostone.Jiang