• 语法的省略不能造成编译器的歧义


    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{}  
    }
    

      

  • 相关阅读:
    mongodb的sql例子(简单版)
    git上传github上
    git中的版本库,暂存区和工作区
    进程与线程的区别
    mysql 在linux 修改账号密码
    linux 下 yum 安装mysql
    linux 下 修改mysql账号密码
    linux 下开放端口问题
    linux 下安装tomcat
    Ubuntu 配置Tomcat环境(转载)
  • 原文地址:https://www.cnblogs.com/scala/p/3697005.html
Copyright © 2020-2023  润新知