0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 阿里实习面试总结

johnwong 2016-04-12 11:22:08 浏览1323

#### 1. 重载与多态区别

``````public class Shape {
public static void main(String[] args) {
Triangle tri = new Triangle();
System.out.println("Triangle is a type of shape? " + tri.isShape());// 继承
Shape shape = new Triangle();
System.out.println("My shape has " + shape.getSides() + " sides."); // 多态
Rectangle Rec = new Rectangle();
Shape shape2 = Rec;
System.out.println("My shape has " + shape2.getSides(Rec) + " sides."); // 重载
}

public boolean isShape() {
return true;
}

public int getSides() {
return 0;
}

public int getSides(Triangle tri) { // 重载
return 3;
}

public int getSides(Rectangle rec) { // 重载
return 4;
}
}

class Triangle extends Shape {
public int getSides() { // 重写,实现多态
return 3;
}
}

class Rectangle extends Shape {
public int getSides() { // 重写,实现多态
return 4;
}
}``````

#### 4. url中参数去掉一部分的算法

``````package monitor;

import java.util.Arrays;

class CheckHelper {
private char temp[] = new char[64];
private String params[];
private boolean check[];
private int tag = 0;

public CheckHelper(String params[]) {
this.params = params;
check = new boolean[params.length];
}

public void initCheck() {
for (int i = 0; i < check.length; i++)
check[i] = true;
tag = 0;
}

public void doCheck(char c) {
for (int i = 0; i < params.length; i++) {
String param = params[i];
temp[tag] = c;
if (tag >= param.length() || param.charAt(tag) != c) {
check[i] = false;
}
}
tag++;
}

public String getResult() {
for (int i = 0; i < params.length; i++) {
String param = params[i];
if (check[i] && param.length() == tag) {
initCheck();
return null;
}
}
String ret = new String(Arrays.copyOf(temp, tag));
initCheck();
return ret;
}
}

public class Test {
public static String removeParam(String url, String[] params) {
CheckHelper ch = new CheckHelper(params);
ch.initCheck();
StringBuilder sb = new StringBuilder();
boolean start = false;
boolean rest = false;
boolean copy = false;
for (int i = 0; i < url.length(); i++) {
char c = url.charAt(i);
if (!start) {
sb.append(c);
if (c == '?')
start = true;
} else if (c == '=') {
rest = true;
String ret = ch.getResult();
if (ret != null) {
if (sb.charAt(sb.length() - 1) != '?')
sb.append('&');
sb.append(ret);
sb.append('=');
copy = true;
} else {
copy = false;
}
} else if (!rest) {
ch.doCheck(c);
} else if (c != '&') {
if (copy)
sb.append(c);
} else {
ch.initCheck();
rest = false;
copy = false;
}
}
return sb.toString();
}

public static void main(String[] args) throws Exception {
String url = "http://s.taobao.com/search?spm=a230r.1.0.100.S98nmj&" +
"q=Apple%2F%C6%BB%B9%FB+MacBook+Pro+MD101CH%2FA&v=product&p=" +
"detail&pspuid=202666380&cat=1101&from_pos=55_1101.xlcombo_1_2_202666380";
String[] params = { "spm", "pspuid", "cat" };
System.out.println(removeParam(url, params));
}
}``````

PS. 2014年的今天已经是阿里巴巴的一员了。觉得应该对自己的Java有信心并提出让Java工程师来面试我。

johnwong
+ 关注

corcosa 11142人浏览