随笔 - 56  文章 - 0 评论 - 0 阅读 - 50727

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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];
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 的内部类

1
2
3
4
5
6
7
8
9
10
11
12
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 的内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
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{} 
}

  

posted on   scala  阅读(365)  评论(0编辑  收藏  举报
编辑推荐:
· 电商平台中订单未支付过期如何实现自动关单?
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 大模型 Token 究竟是啥:图解大模型Token
阅读排行:
· 瞧瞧别人家的限流,那叫一个优雅!
· 1.net core 工作流WorkFlow流程(介绍)
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 面试官:如果某个业务量突然提升100倍QPS你会怎么做?
· 短信接口被刷爆:我用Nginx临时止血
点击右上角即可分享
微信分享提示