泛型:
//定义一个GenericClass类,E表示未知的数据类型
public class GenericClass {private E name; public E getName() { return name; } public void setName(E name) { this.name = name; }}
//main方法public class Demo02GenericClass {public static void main(String[] args) { GenericClass gc=new GenericClass(); //不写泛型默认为Object类型 gc.setName(“只能是字符串”); Object obj=gc.getName(); //创建GenericClass对象,泛型使用Integer类型 GenericClass gc2=new GenericClass<>(); gc2.setName(1); Integer name=gc2.getName(); System.out.println(name);//1
//创建GenericClass对象,泛型使用String类型GenericClass gc3=new GenericClass<>(); gc3.setName(“小红”); String name1=gc3.getName(); System.out.println(name1);//小红 }}
课堂代码:
public class Student {
private int stuNo;//学号唯一性
private String stuName;
private String className;
public Student() {
}
public Student(int stuNo, String stuName, String className) {
this.stuNo=stuNo;
this.stuName=stuName;
this.className=className;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public void setClassName(String className) {
this.className = className;
}
public int getStuNo() {
return this.stuNo;
}
public String getStuName() {
return stuName;
}
public String getClassName() {
return className;
}
//以前 showINfo;
public String toString() {
return “stuNo:”+stuNo+",stuName:"+stuName+",className:"+className;
}
}
3.StuManage
public class StuManage {
// 学生信息存储:50;
private Student stus[];
private static int count;
public StuManage() {
stus = new Student[50];
for(int i=0;i<3;++i) {
stus[i]=new Student(20180+i,“AA”+i,“wl”);
++count;
}
}
// 添加学生信息
public boolean add(Student student) {
if (count > stus.length) {
return false;
}
stus[count++] = student;
return true;
}
//3
public boolean del(int stuNo) {
// 1.遍历学生数组,找到学生信息位置
for (int i = 0; i < count; ++i) {
if (stuNo == stus[i].getStuNo()) {
// 2.删除;后边元素往前移动
for (int j = i; j < count - 1; ++j) {
stus[j] = stus[j + 1];
}
count–;
return true;
}
}
return false;
}
public boolean del(Student student) {
// 1.遍历学生数组,找到学生信息位置
for (int i = 0; i < count; ++i) {
if (student.getStuNo() == stus[i].getStuNo()) {
// 2.删除;后边元素往前移动
for (int j = i; i < count - 1; ++j) {
stus[j] = stus[j + 1];
}
return true;
}
}
return false;
}
public boolean update(Student oldStu, Student newStu) {
// 1.遍历学生数组,找到学生信息位置
for (int i = 0; i < count; ++i) {
if (oldStu.getStuNo() == stus[i].getStuNo()) {
// 2.替换;后边元素往前移动
stus[i] = newStu;
return true;
}
}
return false;
}
public Student query(int stuNo) {
for (int i = 0; i < count; ++i) {
if (stuNo == stus[i].getStuNo()) {
// 2.替换;后边元素往前移动
return stus[i];
}
}
return null;
}