lzj 5.30 范型要点
1、数组属于引用数据类型,只能保存同一种数据类型,数组的长度一旦分配不可改变。
优点:可高效运行。 缺点:无法直接删除和增加元素。
2、ArrayList list = new ArrayList(); list没有指定是什么对象,是object类型,如果添加一个String类型,有类型强制转换,容易出错。
3、ArrayList<Integer> list = new ArrayList<Integer>();可以为ArrayList指定能保存的数据类型只能是int数据类型,而int对应的包装类是:Integer。
4、范型类:public class 类名<字母> 常用字母为:T(tupe),E(entity),K(key),V(value)
5、范型方法:在定义方法时,先定义范型参数并马上用在返回值或形参中,这样的帆帆发就是范型方法 1、<T>void 方法名(T 变量名) 2、<T>T 方法名() 3、<T>T 方法名(T 变量名)
例如:public <E> void print1(E a) public <E>E print2() public <E> E print3(E a)
6、范型接口:指在接口定义时指定范型
例如:public interface IShow<T>{
void show(T value);}
7、?代表任何类型, List<?>声明了List中包含的元素类型是未知的, 通配符所代表的其实是“一种数据类型”,但具体类型是未知的 , List<?>并不等于List<Object> ,List<Object>集合中可以包含任何“多种数据类型”的元素。
8、List<? extends 类> 例如:public static void showList(List<? extends Animal> list) 可以接受的集合的数据类型为该类类型或者为该类的子类类型。不能添加对象
9、List<? super 类> 例如:public static void showList(List<? super Animal> list) 可以添加该类及该类子类的对象。
10、List<? extends IFly> 例如:public static void showList(List<? extends IFly> list) 可以添加该类及该类子类的对象。 因为该类实现了接口 子类相当于也实现了接口
package com.iit.mian; import com.iit.entity.User; import org.junit.jupiter.api.Test; import java.util.Scanner; public class Start { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String userName = sc.next(); System.out.println("请输入密码:" ); String password1 = sc.next(); System.out.println("请再次输入密码:" ); String password2 = sc.next(); User user = new User(userName, password1, password2); boolean b1 = user.checkUserName(); boolean b2 = user.checkPassword(); user.register(b1,b2); } }
package com.iit.entity; public class User { private String userName; private String password1; private String password2; public User(){} public User(String userName, String password1, String password2){ this.userName = userName; this.password1 = password1; this.password2 = password2; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword1() { return password1; } public void setPassword1(String password1) { this.password1 = password1; } public String getPassword2() { return password2; } public void setPassword2(String password2) { this.password2 = password2; } @Override public String toString() { return "User{" + "userName='" + userName + '\'' + '}'; } public boolean checkUserName() { boolean check = false; boolean check1; boolean check2 = true; boolean check3; boolean check4 = false; boolean check5 = false; char firstChar = userName.charAt(0); check1 = Character.isAlphabetic(firstChar); for (int i = 1; i < userName.length(); i++) { if (!Character.isAlphabetic(userName.charAt(i)) && !Character.isDigit(userName.charAt(i))) { check2 = false; break; } } for (int i = 0; i < userName.length(); i++) { if (Character.isAlphabetic(userName.charAt(i))) { if (Character.isLowerCase(userName.charAt(i))) { check4 = true; } } } for (int i = 0; i < userName.length(); i++) { if (Character.isAlphabetic(userName.charAt(i))) { if (Character.isUpperCase(userName.charAt(i))) { check5 = true; } } } check3 = check4 && check5; if (check1 && check2 && check3) { check = true; } return check; } public boolean checkPassword() { return password1.equals(password2); } public void register(boolean b1,boolean b2) { if (b1 && b2) { System.out.printf("用户%s注册成功!", userName); } else { System.out.printf("注册失败,请检查账户或密码不正确"); } } }

浙公网安备 33010602011771号