package exp { object Main { def main(args: Array[String]): Unit = { B[String].say("hello"); //B[String]后的括号可以省略,编译器知道这不是要用类型B而是要调用B.apply[String] A().say("hello"); //括号省略了不知道是要用object的类型A还是A.apply() } } class A { def say(x:String) = println(x); } object A { def apply():A = new A; } class B[T] { def say(x:T) = println(x); } object B { def apply[T]():B[T] = new B[T]; } }
trait Stu { val age: Int; lazy val x = 100 / age; } abstract class S { val name: String; def say = println(s"hello $name"); } object Main { def main(args: Array[String]): Unit = { abstract class Sa extends S with Stu; val y = new Sa() { val name = "Fred"; val age = 28 }; // val y = new S(){val name="wengmj";val age=28} with Stu; 直接这么写就不行; 怎样直接构造一个继承抽象类和特质的匿名具体类 y.say println(y.x); } }
scala 的内部类
class Outer { class Inner; } object Main { def main(args: Array[String]): Unit = { val o = new Outer; val i = new o.Inner; println(i.isInstanceOf[Outer#Inner]); } }
java 的内部类
public class Ppp { public static void main(String[] args) { Outer a = new Outer(); Outer.Inner b = a.new Inner(); ////Outer.Inner i = new Outer().new Inner(); System.out.print(b instanceof Outer.Inner); } } class Outer { class Inner{} }