成都热土科技有限公司2019面试题

面试时间: 2019-3-29

1. java多态定义及优点

定义:

1
父类型 对象名= new 子类型

优点:

  • 提高了代码的维护性(继承保证)
  • 提高了代码的扩展性(由多态保证)
  • 把不同的子类对象都当做父类类型来看待,可以屏蔽不同子类对象之间的实现差异,从而写出通用的代码达到通用编程,以适应需求的不断变化。

    2.指出下面程序的运行结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    // 执行结果:1a2b2b
    // 说明:先执行父类静态代码块,再执行子类静态代码块,又先执行父类构造,再执行子类构造
    public class Hello {
    public static void main(String[] args) {
    A ab = new B();
    ab = new B();
    }
    }
    class A{
    static {
    System.out.print("1");
    }

    public A() {
    System.out.print("2");
    }
    }
    class B extends A{
    static {
    System.out.print("a");
    }

    public B() {
    System.out.print("b");
    }
    }

3.下面程序打印结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 执行结果:
//try
//finally
//100
public class TryTest {
public static void main(String[] args) {
System.out.println(test());
}

private static int test() {
int num = 10;
try {
System.out.println("try");
return num += 10;
} catch (Exception e) {
System.out.println("error");
} finally {
if (num > 20) {
System.out.println("num>20:" + num);
}
System.out.println("finally");
num = 100;
return num;
}
}
}

4. 实现一个线程安全的单例模式,至少使用两种方法

懒汉式单例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class LazySingleton {
// 私有静态对象,加载时候不做初始化
private static LazySingleton singleton =null;
// 私有构造方法,避免外部创建实例
private LazySingleton(){

}
/**
* 静态工厂方法,返回此类的唯一实例.
* 当发现实例没有初始化的时候,才初始化.
* @return LazySingleton
*/
public static synchronized LazySingleton getInstance(){
if(singleton ==null){
singleton =new LazySingleton();
}
return singleton;
}
}

饿汉式单例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class EagerSingleton {
// 私有的唯一实例成员,在类加载的时候就创建好了单例对象
private static final EagerSingleton SINGLETON = new EagerSingleton();
//私有构造方法,避免外部创建实例
private EagerSingleton() {

}
/**
* 静态工厂方法,返回此类的唯一实例.
*
* @return EagerSingleton
*/
public static EagerSingleton getInstance() {
return SINGLETON;
}
}

5. 冒泡排序实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 执行结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
public class BubbleSort {
public static void main(String[] args) {
int[] arr = new int[]{5, 2, 1, 3, 6, 4, 9, 8, 7};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] < arr[j]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}

6. 生产消费者

CSDN

  • 生产者:往一个公共的盒子里面放苹果
  • 消费者:从公共的盒子里面取苹果
  • 盒子:盒子的容量不能超过5
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    public class ProducerAndCustomerLockTest {
    public static void main(String[] args) {
    // 创建一个盒子
    Box box = new Box();
    // 创建一个生产者
    Producer producer = new Producer(box);
    // 创建一个消费者
    Customer customer = new Customer(box);
    new Thread(producer, "生产者A").start();
    new Thread(customer, "消费者A").start();
    new Thread(producer, "生产者B").start();
    new Thread(customer, "消费者B").start();
    }
    }

    /**
    * 苹果
    */
    class Apple {
    /**
    * 苹果编号
    */
    private int id;

    public Apple(int id) {
    this.id = id;
    }

    public int getId() {
    return id;
    }
    }

    /**
    * 盒子
    */
    class Box {
    /**
    * 最大容量
    */
    private int max = 5;
    /**
    * 最小容量
    */
    private int min = 0;
    /**
    * 装苹果的盒子 为什么是链表 因为 它有boxs.removeFirst();方法 方便取出苹果
    */
    private LinkedList<Apple> boxs = new LinkedList<>();
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
    * 放
    */
    public void push(Apple apple) {
    // 加锁
    lock.lock();
    try {
    while (boxs.size() >= this.max) {
    System.out.println("盒子已满!");
    try {
    condition.await();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    System.out.println(Thread.currentThread().getName() + "放苹果:" + apple.getId());
    boxs.add(apple);
    condition.signalAll();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    // 释放锁
    lock.unlock();
    }
    }

    /**
    * 取
    */
    public void pop() {
    lock.lock();
    try {
    while (boxs.size() <= this.min) {
    System.out.println("盒子已空!");
    try {
    condition.await();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    // 每次卖一个产品
    System.out.println(Thread.currentThread().getName() + "取苹果:" + boxs.removeFirst().getId());
    condition.signalAll();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    }
    }
    }

    /**
    * 生产者
    */
    class Producer implements Runnable {
    private static Integer count = 0;
    private Box box;

    public Producer(Box box) {
    this.box = box;
    }

    @Override
    public void run() {
    for (int i = 0; i < 20; i++) {
    // 放苹果
    box.push(new Apple(++count));
    }
    }
    }

    /**
    * 消费者
    */
    class Customer implements Runnable {
    private Box box;

    public Customer(Box box) {
    this.box = box;
    }

    @Override
    public void run() {
    for (int i = 0; i < 20; i++) {
    // 取苹果
    box.pop();
    }
    }
    }

synchronized实现生产者消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* synchronized实现生产者消费者
*/
public class ProducerAndCustomerLockTest {
public static void main(String[] args) {
// 创建一个盒子
Box box = new Box();
// 创建一个生产者
Producer producer = new Producer(box);
// 创建一个消费者
Customer customer = new Customer(box);
new Thread(producer, "生产者A").start();
new Thread(customer, "消费者A").start();
new Thread(producer, "生产者B").start();
new Thread(customer, "消费者B").start();
}
}

/**
* 盒子
*/
class Box {
/**
* 最大容量
*/
private int max = 5;
/**
* 最小容量
*/
private int min = 0;
/**
* 初始容量
*/
private int product = 0;
/**
* 放
*
* @return int
*/
public synchronized void get() {
while (product >= this.max) {
System.out.println("盒子已满!");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 每次进一个产品
System.out.println(Thread.currentThread().getName() + ":" + ++product);
this.notifyAll();
}
/**
* 取
*/
public synchronized void sale() {
while (product <= this.min) {
System.out.println("盒子已空!");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 每次卖一个产品
System.out.println(Thread.currentThread().getName() + ":" + --product);
this.notifyAll();
}
}

/**
* 生产者
*/
class Producer implements Runnable {
private Box box;

public Producer(Box box) {
this.box = box;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
// 放苹果
box.get();
}
}
}

/**
* 消费者
*/
class Customer implements Runnable {
private Box box;
public Customer(Box box) {
this.box = box;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
// 取苹果
box.sale();
}
}
}

Lock实现生产者消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Lock实现生产者消费者
*/
public class ProducerAndCustomerLockTest {
public static void main(String[] args) {
// 创建一个盒子
Box box = new Box();
// 创建一个生产者
Producer producer = new Producer(box);
// 创建一个消费者
Customer customer = new Customer(box);
new Thread(producer, "生产者A").start();
new Thread(customer, "消费者A").start();
new Thread(producer, "生产者B").start();
new Thread(customer, "消费者B").start();
}
}

/**
* 盒子
*/
class Box {
/**
* 最大容量
*/
private int max = 5;
/**
* 最小容量
*/
private int min = 0;
/**
* 初始容量
*/
private int product = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();

/**
* 放
*
* @return int
*/
public void get() {
// 加锁
lock.lock();
try {
while (product >= this.max) {
System.out.println("盒子已满!");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 每次进一个产品
System.out.println(Thread.currentThread().getName() + ":" + ++product);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放锁
lock.unlock();
}
}

/**
* 取
*/
public void sale() {
lock.lock();
try {
while (product <= this.min) {
System.out.println("盒子已空!");
try {
condition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 每次卖一个产品
System.out.println(Thread.currentThread().getName() + ":" + --product);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}

/**
* 生产者
*/
class Producer implements Runnable {
private Box box;

public Producer(Box box) {
this.box = box;
}

@Override
public void run() {
for (int i = 0; i < 20; i++) {
// 放苹果
box.get();
}
}
}

/**
* 消费者
*/
class Customer implements Runnable {
private Box box;

public Customer(Box box) {
this.box = box;
}

@Override
public void run() {
for (int i = 0; i < 20; i++) {
// 取苹果
box.sale();
}
}
}

7. JavaScript实现倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <script type="text/javascript">
window.onload = function () {
var numLable = document.getElementById("numLable");
var time=setInterval(function () {
var num = Number.parseInt(numLable.innerHTML);
if (num > 0) {
numLable.innerHTML = (--num).toString();
}else{
clearInterval(time);
}
}, 1000);
}
</script>
</head>
<body>
<label id="numLable">5</label>
</body>