package com.atguigu.exer;
/*
- 定义一个Employee类,
该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday
*/
public class Employee implements Comparable{
private String name;
private int age;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee(String name, int age, MyDate birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return “Employee [name=” + name + “, age=” + age + “, birthday=”
+ birthday + “]”;
}
@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e = (Employee)o;
return this.name.compareTo(e.name);
}
return 0;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
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;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.atguigu.exer;
/*
- 定义一个Employee类,
该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;
并为每一个属性定义 getter, setter 方法;
并重写 toString 方法输出 name, age, birthday
*/
public class Employee1 {
private String name;
private int age;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Employee1(String name, int age, MyDate birthday) {
super();
this.name = name;
this.age = age;
this.birthday = birthday;
}
@Override
public String toString() {
return “Employee [name=” + name + “, age=” + age + “, birthday=”
+ birthday + “]”;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
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;
Employee1 other = (Employee1) obj;
if (age != other.age)
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
package com.atguigu.exer;
/*
- MyDate类包含:
private成员变量month,day,year;并为每一个属性定义 getter, setter 方法;
*/
public class MyDate {
private int day;
private int month;
private int year;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public MyDate(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
@Override
public String toString() {
return "MyDate [day=" + day + ", month=" + month + ", year=" + year
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate other = (MyDate) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}
}
package com.atguigu.exer;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
import org.junit.Test;
/*
- 创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)
分别按以下两种方式对集合中的元素进行排序,并遍历输出:
1). 使Employee 实现 Comparable 接口,并按 name 排序
2). 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。
提示:Employee类是否需要重写equals()方法?MyDate类呢?
*/
public class TestEmployee {
//定制排序:创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序。
@Test
public void test2(){
Comparator com = new Comparator(){
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Employee1 && o2 instanceof Employee1){
Employee1 e1 = (Employee1)o1;
Employee1 e2 = (Employee1)o2;
MyDate birth1 = e1.getBirthday();
MyDate birth2 = e2.getBirthday();
if(birth1.getYear() != birth2.getYear()){
return birth1.getYear() - birth2.getYear();
}else{
if(birth1.getMonth() != birth2.getMonth()){
return birth1.getMonth() - birth2.getMonth();
}else{
return birth1.getDay() - birth2.getDay();
}
}
}
return 0;
}
};
TreeSet<Employee1> set = new TreeSet<>(com);
Employee1 e1 = new Employee1("刘德华", 55, new MyDate(4, 12, 1976));
Employee1 e2 = new Employee1("郭富城", 43, new MyDate(7, 3, 1954));
Employee1 e3 = new Employee1("张学友", 33, new MyDate(9, 12, 1954));
Employee1 e4 = new Employee1("黎明", 54, new MyDate(12, 3, 1954));
Employee1 e5 = new Employee1("李敏镐", 65, new MyDate(4, 21, 1945));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
Iterator<Employee1> i = set.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
//自然排序: 使Employee 实现 Comparable 接口,并按 name 排序
@Test
public void test1(){
Employee e1 = new Employee("刘德华", 55, new MyDate(4, 12, 1976));
Employee e2 = new Employee("郭富城", 43, new MyDate(7, 3, 1965));
Employee e3 = new Employee("张学友", 33, new MyDate(9, 12, 1954));
Employee e4 = new Employee("黎明", 54, new MyDate(12, 2, 1967));
Employee e5 = new Employee("李敏镐", 65, new MyDate(4, 21, 1945));
// Employee e6 = new Employee(“李敏镐”, 63, new MyDate(4, 21, 1945));
TreeSet set = new TreeSet<>();
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
// set.add(e6);
Iterator<Employee> i = set.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
package com.atguigu.exer1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
- 定义个泛型类 DAO,在其中定义一个Map 成员变量,Map 的键为 String 类型,值为 T 类型。
分别创建以下方法:
public void save(String id,T entity): 保存 T 类型的对象到 Map 成员变量中
T get(String id):从 map 中获取 id 对应的对象
void update(String id,T entity):替换 map 中key为id的内容,改为 entity 对象
List list():返回 map 中存放的所有 T 对象
void delete(String id):删除指定 id 对象
*/
public class DAO {
Map<String,T> map;
public void delete(String id){
map.remove(id);
}
public List<T> list(){
List<T> list = new ArrayList<T>();
for(String s : map.keySet()){
list.add(map.get(s));
}
return list;
}
public void update(String id,T entity){
//map.remove(id);
map.put(id, entity);
}
public T get(String id){
return map.get(id);
}
public void save(String id,T entity){
map.put(id, entity);
}
}
package com.atguigu.exer1;
import java.util.HashMap;
import java.util.List;
/*
- 创建 DAO 类的对象, 分别调用其 save、get、update、list、delete 方法来操作 User 对象,
使用 Junit 单元测试类进行测试。
*/
public class TestDAO {
public static void main(String[] args) {
DAO dao = new DAO();
dao.map = new HashMap<String,User>();
dao.save("1001", new User(1, 32, "梁朝伟"));
dao.save("1002", new User(2,34,"汤唯"));
dao.save("1003", new User(3,23,"刘嘉玲"));
User u = dao.get("1002");
System.out.println(u);
dao.update("1002", new User(4,45,"成龙"));
dao.delete("1003");
List<User> list = dao.list();
System.out.println(list);
}
}
package com.atguigu.exer1;
/*
- 定义一个 User 类:
该类包含:private成员变量(int类型) id,age;(String 类型)name。
*/
public class User {
private int id;
private int age;
private String 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(int id, int age, String name) {
super();
this.id = id;
this.age = age;
this.name = name;
}
@Override
public String toString() {
return “User [id=” + id + “, age=” + age + “, name=” + name + “]”;
}
@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;
}
}
package com.atguigu.java;
public class Customer {
}
package com.atguigu.java;
public class CustomerDAO extends DAO{
}
package com.atguigu.java;
import java.util.List;
//DAO:database access object 数据库访问对象
public class DAO {
public void add(T t){
//…
}
public T get(int index){
return null;
}
public List getForList(int index){
return null;
}
public void delete(int index){
}
}
package com.atguigu.java;
public class ExamStudentDAO extends DAO{
}
package com.atguigu.java;
import java.util.ArrayList;
import java.util.List;
//自定义泛型类
public class Order {
private String orderName;
private int orderId;
private T t;
List list = new ArrayList<>();
public void add(){
list.add(t);
}
public T getT(){
return t;
}
public void setT(T t){
this.t = t;
}
//不可以在static方法中使用泛型的声明
// public static void show(){
// System.out.println(t);
// }
public void info(){
//不可以在try-catch中使用类的泛型的声明
// try{
//
// }catch(T e){
//
// }
}
//声明泛型方法
public static E getE(E e){
return e;
}
//实现数组到集合的复制
public List fromArrayToList(E[] e,List list){
for(E e1 : e){
list.add(e1);
}
return list;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
@Override
public String toString() {
return "Order [orderName=" + orderName + ", orderId=" + orderId
+ ", t=" + t + "]";
}
}
//继承泛型类或泛型接口时,可以指明泛型的类型
class SubOrder extends Order{
}
package com.atguigu.java;
public class Student {
}
package com.atguigu.java;
public class TestCustomerDAO {
public static void main(String[] args) {
CustomerDAO c = new CustomerDAO();
c.add(new Customer());
c.get(0);
}
}
package com.atguigu.java;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
/*
- 泛型的使用
- 1.在集合中使用泛型(掌握)
- 2.自定义泛型类、泛型接口、泛型方法(理解 —>使用)
- 3.泛型与继承的关系
- 4.通配符
/
public class TestGeneric {
/
* 通配符的使用
/
@Test
public void test7(){
List list = new ArrayList();
list.add(“AA”);
list.add(“BB”);
List<?> list1 = list;
//可以读取声明为通配符的集合类的对象
Iterator<?> iterator = list1.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//不允许向声明为通配符的集合类中写入对象。唯一例外的是null
// list1.add(“CC”);
// list1.add(123);
list1.add(null);
}
/
* 通配符 ?
* List、List、。。。。都是List<?>的子类
*
* ? extends A :可以存放A及其子类
* ? super A:可以存放A及其父类
*/
@Test
public void test6(){
List<?> list = null;
List list1 = new ArrayList();
List list2 = new ArrayList();
list = list1;
list = list2;
show(list1);
// show(list2);
show1(list1);
show1(list2);
List<? extends Number> list3 = null;
List<Integer> list4 = null;
list3 = list4;
// list3 = list1;
List<? super Number> list5 = null;
list5 = list1;
}
public void show(List<Object> list){
}
public void show1(List<?> list){
}
/*
* 泛型与继承的关系:
* 若类A是类B的子类,那么List<A>就不是List<B>的子接口
*/
@Test
public void test5(){
Object obj = null;
String str = "AA";
obj = str;
Object[] obj1 = null;
String[] str1 = new String[]{"AA","BB","CC"};
obj1 = str1;
List<Object> list = null;
List<String> list1 = new ArrayList<String>();
// list = list1;
//假设list = list1满足
//list.add(123);
//String str = list1.get(0);//出现问题,所以假设不满足
}
//自定义泛型类的使用
@Test
public void test4(){
//1.当实例化泛型类的对象时,指明泛型的类型。
//指明以后,对应的类中所有使用泛型的位置,都变为实例化中指定的泛型的类型
//2.如果我们自定义了泛型类,但是在实例化时没有使用,那么默认类型是Object类的
Order<Boolean> order = new Order<Boolean>();
// order.getT();
order.setT(true);
System.out.println(order.getT());
order.add();
List list = order.list;
System.out.println(list);
SubOrder o = new SubOrder();
List<Integer> list1 = o.list;
System.out.println(list1);
//当通过对象调泛型方法时,指明泛型方法的类型。
Integer i = order.getE(34);
Double d = order.getE(2.3);
Integer[] in = new Integer[]{1,2,3};
List<Integer> list2 = new ArrayList<>();
List<Integer> list3 = order.fromArrayToList(in, list2);
System.out.println(list3);
}
@Test
public void test3(){
Map<String,Integer> map = new HashMap<>();
map.put("AA", 78);
map.put("BB", 87);
map.put("DD", 98);
Set<Map.Entry<String,Integer>> set = map.entrySet();
for(Map.Entry<String,Integer> o : set){
System.out.println(o.getKey() + "--->" + o.getValue());
}
}
//2.在集合中使用泛型
@Test
public void test2(){
List<Integer> list = new ArrayList<Integer>();
list.add(78);
list.add(87);
// list.add(“AA”);
// for(int i = 0;i < list.size();i++){
// int score = list.get(i);
// System.out.println(score);
// }
Iterator it = list.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
//1.在集合中没有使用泛型的情况下
@Test
public void test1(){
List list = new ArrayList();
list.add(89);
list.add(87);
list.add(67);
//1.没有使用泛型,任何Object及其子类的对象都可以添加进来
list.add(new String("AA"));
for(int i = 0;i < list.size();i++){
//2.强转为int型时,可能报ClassCastException的异常
int score = (Integer)list.get(i);
System.out.println(score);
}
}
}
package com.atguigu.java1;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//自定义的注解
@Target({TYPE,FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default “hello”;
}
package com.atguigu.java1;
import java.util.ArrayList;
import java.util.List;
/*
- 注解
- 1.JDK提供的常用的注解
- @Override: 限定重写父类方法, 该注释只能用于方法
@Deprecated: 用于表示某个程序元素(类, 方法等)已过时
@SuppressWarnings: 抑制编译器警告
2.如何自定义一个注解
3.元注解
*/
public class TestAnnotation {
public static void main(String[] args) {
Person p = new Student();
p.walk();
p.eat();
@SuppressWarnings({ "rawtypes", "unused" })
List list = new ArrayList();
@SuppressWarnings("unused")
int i = 10;
// System.out.println(i);
}
}
@MyAnnotation(value = “atguigu”)
class Student extends Person{
@Override
public void walk(){
System.out.println(“学生走路”);
}
@Override
public void eat(){
System.out.println(“学生吃饭”);
}
}
@Deprecated
class Person{
String name;
int age;
public Person() {
super();
}
@MyAnnotation(value = "atguigu")
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@MyAnnotation(value = "atguigu")
public void walk(){
System.out.println("走路");
}
@Deprecated
public void eat(){
System.out.println("吃饭");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package com.atguigu.java1;
public class TestSeason {
public static void main(String[] args) {
Season spring = Season.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());
}
}
//枚举类
class Season{
//1.提供类的属性,声明为private final
private final String seasonName;
private final String seasonDesc;
//2.声明为final的属性,在构造器中初始化。
private Season(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.通过公共的方法来调用属性
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
//4.创建枚举类的对象:将类的对象声明public static final
public static final Season SPRING = new Season(“spring”, “春暖花开”);
public static final Season SUMMER = new Season(“summer”, “夏日炎炎”);
public static final Season AUTUMN = new Season(“autumn”, “秋高气爽”);
public static final Season WINTER = new Season(“winter”, “白雪皑皑”);
@Override
public String toString() {
return “Season [seasonName=” + seasonName + “, seasonDesc=”
+ seasonDesc + “]”;
}
public void show(){
System.out.println(“这是一个季节”);
}
}
package com.atguigu.java1;
/*
- 一、枚举类
- 1.如何自定义枚举类
- 2.如何使用enum关键字定义枚举类
-
>常用的方法:values() valueOf(String name)
-
>如何让枚举类实现接口:可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同。(相当于让每个对象重写抽象方法)
*/
public class TestSeason1 {
public static void main(String[] args) {
Season1 spring = Season1.SPRING;
System.out.println(spring);
spring.show();
System.out.println(spring.getSeasonName());
System.out.println();
//1.values()
Season1[] seasons = Season1.values();
for(int i = 0;i < seasons.length;i++){
System.out.println(seasons[i]);
}
//2.valueOf(String name):要求传入的形参name是枚举类对象的名字。
//否则,报java.lang.IllegalArgumentException异常
String str = "WINTER";
Season1 sea = Season1.valueOf(str);
System.out.println(sea);
System.out.println();
Thread.State[] states = Thread.State.values();
for(int i = 0;i < states.length;i++){
System.out.println(states[i]);
}
sea.show();
}
}
interface Info{
void show();
}
//枚举类
enum Season1 implements Info{
SPRING(“spring”, “春暖花开”){
public void show(){
System.out.println(“春天在哪里?”);
}
},
SUMMER(“summer”, “夏日炎炎”){
public void show(){
System.out.println(“生如夏花”);
}
},
AUTUMN(“autumn”, “秋高气爽”){
public void show(){
System.out.println(“秋天是用来分手的季节”);
}
},
WINTER(“winter”, “白雪皑皑”){
public void show(){
System.out.println(“冬天里的一把火”);
}
};
private final String seasonName;
private final String seasonDesc;
private Season1(String seasonName,String seasonDesc){
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasonDesc() {
return seasonDesc;
}
@Override
public String toString() {
return "Season [seasonName=" + seasonName + ", seasonDesc="
+ seasonDesc + "]";
}
// public void show(){
// System.out.println(“这是一个季节”);
// }
}