一些面试的问题
TOP
一些面试的问题
一些面试的问题
一些面试的问题
个人经验分享
我的简历
备忘录
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2w8okwkqzraco
scala记录
class Test1(var name: String, var age: Int){
def method():Unit
private var id
}
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 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是无参的,构造函数不能传入参数
记录
对于java使用线程有以下三种方式:
第一种普通提供的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();
}
}
}
内核网络基础