@Test
public void test6() {
List extends Person> list1=null;
List super Person> list2=null;
List list3=new ArrayList<>();
List list4=new ArrayList<>();;
List list5=new ArrayList<>();;
list1=list3; //编译通过
list1=list4; //编译通过
list1=list5; //编译不通过
list2=list3; //编译不通过
list2=list4; //编译通过
list2=list5; //编译通过
//读取数据
list1=list3;
Person p = list1.get(0); //编译通过
Student st=list1.get(0); //编译不通过
list2=list4;
Object obj = list2.get(0); //编译通过
Person obj2=list2.get(0); //编译不通过
//写入数据
list1.add(new Student()); //编译不通过
list2.add(new Person()); //编译通过
list2.add(new Student()); //编译通过
}
自定义泛型的测试
User类
public class User {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
private int id;
public User(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
private int age;
private String name;
}
DAO类
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 定义泛型类DAO
* 创建一下方法
* public void save(String id ,T entity):保存T类型对象到Map变量中
* public T get(String id):从map中获取id对应的对象
* public void update(String id,T entity):替换map中key为id的内容,改为entity对象
* public List list():返回map中存放的所有T对象
* public void delete (String id):删除指定id对象
*
* @author CharlieLiang
* @param
*
*/
public class DAO {
private Map map;
public DAO() {
map=new HashMap<>();
}
//保存T类型对象到Map变量中
public void save(String id ,T entity) {
map.put(id, entity);
}
//从map中获取id对应的对象
public T get(String id) {
return map.get(id);
}
//替换map中key为id的内容,改为entity对象
public void update(String id,T entity) {
if(map.containsKey(id)) {
map.put(id, entity);
}
}
//返回map中存放的所有T对象
public List list(){
//错误的
// Collection values = map.values();
// return (List) values;
//正确的
List list=new ArrayList<>();
Collection values = map.values();
for(T t:values) {
list.add(t);
}
return list;
}
//删除指定id对象
public void delete (String id) {
map.remove(id);
}
}
DAOTest
import java.util.List;
import org.junit.jupiter.api.Test;
public class DAOTest {
@Test
public void testList() {
DAO dao = new DAO<>();
dao.save("1001", new User(1001,34,"周杰伦"));
dao.save("1002", new User(1002,20,"昆凌"));
dao.save("1003", new User(1002,20,"蔡依林"));
List list = dao .list();
list.forEach( i->System.out.println(i));
}
@Test
public void testUpdate() {
DAO dao = new DAO<>();
dao.save("1001", new User(1001,34,"周杰伦"));
dao.save("1002", new User(1002,20,"昆凌"));
dao.save("1003", new User(1002,20,"蔡依林"));
dao.update("1003", new User(1003,40,"方文山"));
List list=dao.list();
list.forEach(i->System.out.println(i));
}
@Test
public void testDelete() {
DAO dao = new DAO<>();
dao.save("1001", new User(1001,34,"周杰伦"));
dao.save("1002", new User(1002,20,"昆凌"));
dao.save("1003", new User(1002,20,"蔡依林"));
dao.delete("1003");
List list=dao.list();
list.forEach(i->System.out.println(i));
}
}
视图控制层代码demo如下:
@Controller
@RequestMapping("/")
public class MessageController {
private final MessageRepository messageRepository;
@Autowired
public MessageController(Mes
作为一名
Linux SA,日常运维中很多地方都会用到脚本,而服务器的ip一般采用静态ip或者MAC绑定,当然后者比较操作起来相对繁琐,而前者我们可以设置主机名、ip信息、网关等配置。修改成特定的主机名在维护和管理方面也比较方便。如下脚本用途为:修改ip和主机名等相关信息,可以根据实际需求修改,举一反三!
#!/bin/sh
#auto Change ip netmask ga