考虑以下代码:
val a = new { val x = 1; val y = 2 }
val b = new { val x = 1; val y = 2 }
a == b // false
Run Code Online (Sandbox Code Playgroud)
如果字段/方法/值相同,那么如果匿名类定义了一些会返回true的相等,那么这不是明智的吗?
我想这也可以减少Scala在某些用例中必须生成的类文件的数量.
(据我所知,如果匿名类型的C#在相同的顺序中具有相同的值,则它们使用相同的类,并且true当它们相互比较时返回.)
我试过了
scala> class Foo extends Component { var font = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12) }
Run Code Online (Sandbox Code Playgroud)
我得到了:
<console>:10: error: overriding variable font in class Component of type java.awt.Font;
variable font needs `override' modifier
class Foo extends Component { var font = new java.awt.Font("Helvetica", java.awt.Font.BOLD, 12) }
^
Run Code Online (Sandbox Code Playgroud)
所以我试过了
scala> class Foo extends Component { override var font = java.awt.new Font("Helvetica", java.awt.Font.BOLD, 12) }
Run Code Online (Sandbox Code Playgroud)
但这根本没有帮助:
<console>:10: error: overriding variable font in class Component of type java.awt.Font;
variable font has incompatible type
class …Run Code Online (Sandbox Code Playgroud) 在阅读了 Clinton Begin(iBatis 的创建者)的这篇旧文章后,我真的想知道他关于注释与属性的主张是否被广泛接受,或者是否存在分歧。
他的观点是:
annotation不是关键字(与 不同enum)这些说法有道理吗?C# 如何对此进行改进?
我想知道在一台机器上启动两个应用程序实例所需的最小代码是什么,它可以相互发送和接收消息.
据我了解,我需要
我怎么能在Akka做1.和2.
刚刚开始使用Scala进行单元测试,并提出了这个基本问题.
class Test {
ClassToBeTested testObject;
@Before
void initializeConstructor() {
testObject = new ClassToBeTested(//Blah parameters);
}
@Test
//Blah
}
Run Code Online (Sandbox Code Playgroud)
Java中的上述示例显示我可以声明一个类型的对象ClassToBeTested并在以后初始化它.这可以在Scala中完成吗?我尝试过这个
class Test {
var testObject = new ClassToBeTested()
@Before def initializeConstructor() {
//I do not know how to proceed here!!!!!!
}
@Test def testOne() {
//Some test
}
}
Run Code Online (Sandbox Code Playgroud)
我不想在里面做所有事情,testOne()因为我想在不同的测试中使用该对象.构造函数的参数是模拟,JUnit我知道如果我全局初始化一个对象而不是内部,则不会初始化模拟@Before.
考虑这些方法 java.lang.String
/**
* Returns the string representation of the <code>Object</code> argument.
*
* @param obj an <code>Object</code>.
* @return if the argument is <code>null</code>, then a string equal to
* <code>"null"</code>; otherwise, the value of
* <code>obj.toString()</code> is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
/**
* Returns the string representation of the <code>char</code> array
* argument. The contents of the character array are copied; subsequent …Run Code Online (Sandbox Code Playgroud) 无论我用作输入的集合类型,LINQ总是返回IEnumerable<MyType>而不是List<MyType>或HashSet<MyType>.
从MSDN教程:
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// numQuery is an IEnumerable<int> <====== Why IEnumerable<int> instead of int[]?
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
Run Code Online (Sandbox Code Playgroud)
我想知道决定不保留集合类型(如元素类型)的原因是什么,而不是建议的解决方法.
我知道toArray,toDictionary或toList存在,这不是问题.
编辑:C#中的实现与Scala的不同之处在于如何在不覆盖各地的返回类型的情况下工作?
我无法使用伴随对象创建表示XML解析文档的类.
这是该类的代码:
package models
import javax.xml.bind.Element
import scala.xml.Elem
import javax.xml.validation.SchemaFactory
import javax.xml.transform.stream.StreamSource
trait MyXML {
case class ElémentXML(code_xml: scala.xml.Elem) {
def validate: Boolean = {
try ({
val schemaLang = "http://www.w3.org/2001/XMLSchema"
val factory = SchemaFactory.newInstance(schemaLang)
val schema = factory.newSchema(new StreamSource("Sites_types_libelles.xsd"))
val validator = schema.newValidator()
validator.validate(new StreamSource(code_xml.toString))
true
}) catch {
case t:Throwable => false
}
}
}
object ElémentXML {
def apply(fichier: String) {
try{
val xml_chargé = xml.XML.loadFile(fichier)
Some(new ElémentXML(xml_chargé))
}catch{
case e:Throwable => None
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
import java.util.Collections;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.User;
public class SecureStuff {
@PreAuthorize("#user.password == #oldPassword")
public static void changePassword(User user, String oldPassword, String newPassword){
System.out.print("Changing passwords ...");
}
public static void main(String[] args) {
User joe = new User("Joe", "HansWurst", true, true, true, true, Collections.EMPTY_LIST);
changePassword(joe, "HansWurst", "TeeWurst");
}
}
Run Code Online (Sandbox Code Playgroud)
我在STS(SpringSource工具套件)中运行代码,它按预期工作.(它打印出来"Changing passwords ...".)然后我将密码重命名为其他东西,期待方法调用现在失败.
我已将该行添加<global-method-security pre-post-annotations="enabled"/>到applicationContext-security.xml配置文件中.
我在这里错过了什么?
我真的很想知道这段代码是做什么的:
scala> import java.nio.file._
import java.nio.file._
scala> Files.copy(Paths.get(""), Paths.get(""))
res0: java.nio.file.Path =
Run Code Online (Sandbox Code Playgroud)
不应该抛出一个NoSuchFileException?
阅读JavaDoc揭示:
默认情况下,如果目标文件已存在或者是符号链接,则复制将失败,除非源和目标是同一文件,在这种情况下,方法完成而不复制文件.
但我不确定这是真正的原因,因为Files.copy(Paths.get("a"), Paths.get("a"))失败了.
scala ×7
java ×5
c# ×3
annotations ×2
actor ×1
akka ×1
attributes ×1
class ×1
collections ×1
components ×1
copy ×1
equals ×1
file ×1
filesystems ×1
fonts ×1
interop ×1
linq ×1
messaging ×1
networking ×1
null ×1
overloading ×1
overriding ×1
path ×1
types ×1
xml ×1