java第六周

泛型:
//定义一个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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值