我需要将Java代码转换为Scala,但是编译器向我显示错误。我知道方法上的参数输入是val类型。如果我需要转换这些值,我可以采用哪种选择?我想应用案例类或类...在代码段代码下方(在Scala中):
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
return null
}else if(x == null && y == null){
return this
}else if(this.root == null && x == null){
return y
}else if(this.root == null && y == null){
return x
}else if(x == null){
y = y.pmerge(this) //error
null
}else if(this.root == null){
y = y.pmerge(x) //error
null
}else if (y == null){
y = this.pmerge(x) // error
null
}else{
y = y.pmerge(x)
this
}
}
请注意,更新y参数时显示错误。
谢谢
答案 0 :(得分:1)
是的,由于无法将某些内容重新分配给val以及Scala中方法的参数仅作为val(不可变的)发送,因此显示了错误。
由于您没有提供 'Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub login()
Const Url$ = "https://www.hedgefundresearch.com/user/login"
Dim oLogin As Object, oPassword As Object, oButton As Object, HTMLdoc As Object
Dim UserName As String, Password As String
UserName = "yyyyy@zzzz.com"
Password = "password"
Dim ie As Object, myURL As String
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate Url
ie.Visible = True
Do Until ie.ReadyState = 4
DoEvents
Loop
Set HTMLdoc = ie.document
Set oLogin = HTMLdoc.getElementsByClassName("form-control form-text required")(0)
Set oPassword = HTMLdoc.getElementsByClassName("form-control form-text required")(1)
Set oButton = HTMLdoc.getElementsByClassName("btn btn-primary form-submit icon-before")(0)
On Error Resume Next
oLogin.Value = UserName
On Error Resume Next
oPassword.Value = Password
On Error Resume Next
oButton.Click
myURL = "https://www.hedgefundresearch.com/download/index-ror-perf-download/2899"
ie.navigate myURL
'AppActivate "zzz@yyyy.net | Hedge Fund Research® - Internet Explorer"
'Sleep 1
Application.SendKeys ("%s")
End Sub
的完整定义,因此很难提出替代解决方案,但是:
通常,在Scala中,您可以使用pattern matching来代替GET /download/index-ror-perf-download/2899 HTTP/1.1
Host: www.hedgefundresearch.com
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Referer: https://www.hedgefundresearch.com/indices/hfri-fund-weighted-composite-index
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: _ga=GA1.2.1864475516.1551721637; COOKIE_TOU=2019-03-04+11%3A47%3A19; cookiesession1=297E7ACEBCDZVZC3YAZR1VC8IWL6AF6D; has_js=1; _gid=GA1.2.2018233246.1554731723; SSESScd329e696f514d42291a588234027dae=G6jjp0oCZUb2FjW43IXgTVZAOsb2UbyoU7iawYa2erw
General:
Request URL: https://www.hedgefundresearch.com/download/index-ror-perf-download/2899
Request Method: GET
Status Code: 200 OK
Remote Address: 167.88.151.170:443
Referrer Policy: no-referrer-when-downgrade
“ Java”样式,而可以使用Option来代替this
,这非常强大
例如,我建议以这种“ Scala”样式(部分实现)重构您的方法
if-else
这样一来,您将返回null
, def pmerge_FA(x: Pennant, y: Pennant): Option[Pennant] = {
(Option(x),Option(y), Option(this.root)) match {
case (None, None, None) => None
case (None, None, _) => Option("")
case (None, _, None) => Option(y)
case (_, None, None) => Option(x)
case (None, _, _) =>
....
}
}
作为它们的新值,或者创建一个case类,例如:
x
并在需要时将其返回。
同样,如果您将提供有关y
类的更多信息,则可以为该方法提供更好的替代实现。
答案 1 :(得分:0)
分配后再也不会使用y
(即y.pmerge(...)
)的新值。因此,我猜所有的赋值y = y.pmerge(...)
都可以被调用y.pmerge(...)
代替。
y.pmerge(...)
有副作用吗?以防万一,如果没有,则永远不会使用值y.pmerge(...)
(仅返回null
或this
),因此在这种情况下,可以完全删除行y = y.pmerge(...)
。 / p>
所以代码可以是(如果有副作用)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
y.pmerge(this)
null
}else if(this.root == null){
y.pmerge(x)
null
}else if (y == null){
this.pmerge(x)
null
}else{
y.pmerge(x)
this
}
}
或(如果没有副作用)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
null
}else if(this.root == null){
null
}else if (y == null){
null
}else{
this
}
}
答案 2 :(得分:0)
哦,对了!共有三个类来构建Bag数据结构对象,并在完全平衡的树中添加节点。这些方法适用于该方法。下面是完整的代码(使用Java)。 Pennant类使用图对象的节点构建森林。
节点类:
公共类节点{
==
}
公共类锦旗{
private Node left;
private Node right;
private int item;
public Node() {
left = null;
right = null;
item = 0;
}
public Node(int value) {
left = null;
right = null;
item = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public int getItem() {
return this.item;
}
public void setItem(int item) {
this.item = item;
}
}