梁越
class、case class、object、case object的区别

scala记录

对于一般的class,是这样写的:

class Test1(var name: String, var age: Int){
    def method():Unit
    private var id
}

而对于case class,形式如下:

class Test2(name: String, age: Int)

case class的成员默认都是常量,所以case class适用于数据成员不改变的场景,而且case class之间可以进行比较

case class Message(sender: String, recipient: String, body: String)

val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val messagesAreTheSame = message2 == message3  // true

尽管message2和message3引用不同的对象,但是他们的值是相等的,所以message2 == message3为true。

case class也很适合做匹配,以下是做类型匹配:

abstract class Notification

case class Email(sender: String, title: String, body: String) extends Notification

case class SMS(caller: String, message: String) extends Notification

case class VoiceRecording(contactName: String, link: String) extends Notification

class Main{
    def showNotification(notification: Notification): String = {
    notification match {
        case Email(sender, title, _) =>
        s"You got an email from $sender with title: $title"
        case SMS(number, message) =>
        s"You got an SMS from $number! Message: $message"
        case VoiceRecording(name, link) =>
        s"you received a Voice Recording from $name! Click the link to hear it: $link"
    }
    }
}

一般的object,形式如下:

object Car{
    val numberOfWheels = 4

    def run(): Unit = {
        val currentDateAndTime: Date = new Date(System.currentTimeMillis())
        println(s"I am a new car running on $currentDateAndTime!")
    }
}

object对象是单例对象,和class不同的是,object是无参的,构造函数不能传入参数

而case object和object的区别主要有:

  1. case object添加了默认可序列化方法,转化为文本或者二进制数据,而object需要extends Serializable
  2. case object支持模式匹配
  3. case object支持枚举
  4. case object添加了默认toString方法
ThreadLocal详解

记录

对于java使用线程有以下三种方式:

  1. 使用Thread创建,然后start,而这里又有三种写法来创建
  2. 使用线程池,submit
  3. 使用ThreadLocal

第一种普通提供的Thread:


//普通调用
public class Main{
    static public void main(String[] args){
        Thread t=new Thread();
        t.start();
    }
}

//继承Thread类,重写run方法
//普通调用
public class Main{
    static public void main(String[] args){
        Thread t=new MyThread();
        t.start();
    }
}

public class MyThread extends Thread{
    @Override
    public void run(){
        System.out.println("start new thread!");
    }
}

//继承Runnable接口,重写run方法
public class Main {
    public static void main(String[] args) {
        Thread t = new Thread(new MyRunnable());
        t.start(); // 启动新线程
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("start new thread!");
    }
}

第二种提供的线程池ExecutorService:

import java.util.concurrent.*;

public class Main {
    public static void main(String[] args) {
        // 创建一个固定大小的线程池:
        ExecutorService es = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 6; i++) {
            es.submit(new Task("" + i));
        }
        // 关闭线程池:
        es.shutdown();
    }
}

class Task implements Runnable {
    private final String name;

    public Task(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println("start task " + name);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        System.out.println("end task " + name);
    }
}

第三种特殊的ThreadLocal,它可以在一个线程中传递同一个对象:

public class MyThreadLocal{
    public class MyThreadLocal{
    static private ThreadLocal<Integer> localInt = new ThreadLocal<>();

    public int setAndGet(){
        localInt.set(8);
        return localInt.get();
    }
}
}