http://docs.scala-lang.org/style/naming-conventions.html表示所有对象都是带有大写首字母的camelCase,除非在尝试模仿包或函数时。但是如何模仿val
?
class MyFrame extends javax.swing.JFrame {
object myBox extends javax.swing.Box(javax.swing.BoxLayout.X_AXIS) {
object myLabel extends javax.swing.JLabel {
import scala.collection.JavaConverters._
object myFont extends java.awt.Font(Map(java.awt.font.TextAttribute.FOREGROUND -> java.awt.Color.RED).asJava)
setFont(myFont)
}
add(myLabel)
}
add(myBox)
setSize(100, 60)
}
object Main {
def main(args: Array[String]) {
val myFrame = new MyFrame
myFrame.myBox.myLabel.setText("Hello, World!")
myFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE)
myFrame.setVisible(true)
}
}
或者
class MyFrame extends javax.swing.JFrame {
object MyBox extends javax.swing.Box(javax.swing.BoxLayout.X_AXIS) {
object MyLabel extends javax.swing.JLabel {
import scala.collection.JavaConverters._
object MyFont extends java.awt.Font(Map(java.awt.font.TextAttribute.FOREGROUND -> java.awt.Color.RED).asJava)
setFont(MyFont)
}
add(MyLabel)
}
add(MyBox)
setSize(100, 60)
}
object Main {
def main(args: Array[String]) {
val myFrame = new MyFrame
myFrame.MyBox.MyLabel.setText("Hello, World!") // Seems a bit weird to me
myFrame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE)
myFrame.setVisible(true)
}
}
哪一个更好?
答案 0 :(得分:-2)
只需将camelCase用于包对象即可。 对于类中的嵌入对象使用Upper camelCase。