关于方法的使用
-以下是一个示例小程序-
-学生类的属性有学号、姓名、性别、语文、英语、数学等的成绩,实现方法有求总分,求平均分-
-班级类用SET集合实现增删查改以及排序等-
-最后在主函数调用-
学生类
package com.xuedao.model;
public class Student implements Comparable<Student> {
private int id;
private String name;
private String sex;
private double math;
private double chinese;
private double english;
public Student() {
super();
}
public Student(int id, String name, String sex, double math, double chinese, double english) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.math = math;
this.chinese = chinese;
this.english = english;
}
//求总分
public double getTotal() {
return math + english + chinese;
}
//求平均分
public double getAvg() {
return getTotal()/3;
}
//和其他学生比较
public int compareTo(Student o) {
if(this.getTotal() > o.getTotal()) {
return 1;
}else if(this.getTotal() < o.getTotal()) {
return -1;
}else if(chinese >o.getChinese()) {
return 1;
}else if(chinese <o.getChinese()) {
return -1;
}else if(math >o.getMath()) {
return 1;
}else if(math <o.getMath()) {
return -1;
}else {
return 0;
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getMath() {
return math;
}
public void setMath(double math) {
this.math = math;
}
public double getChinese() {
return chinese;
}
public void setChinese(double chinese) {
this.chinese = chinese;
}
public double getEnglish() {
return english;
}
public void setEnglish(double english) {
this.english = english;
}
@Override
public String toString() {
return id+"\t"+name+"\t"+sex+"\t"+math+"\t"+chinese+"\t"+english+"\t"+getTotal()+"\t"+getAvg();
}
}
班级类
package com.xuedao.model;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Classes {
private String name;
private Set<Student> stus;
public void init(int n) {
stus = new HashSet<Student>();
for (int i = 0; i < n; i++) {
Student s = new Student(i, "小"+i, ((int)(Math.random()*2))==1?"男":"女", (int)(Math.random()*30)+70, (int)(Math.random()*20)+60, (int)(Math.random()*10)+50);
stus.add(s);
}
}
//增
public void add(Student s) {
stus.add(s);
}
//删除
public void delete(int id) {
Iterator<Student> it = stus.iterator();
while(it.hasNext()) {
if(id == it.next().getId()) {
it.remove();
return;
}
}
}
//改
public void modify(int id, Student s) {
delete(id);
add(s);
}
//查
public Student get(int id) {
Iterator<Student> it = stus.iterator();
while(it.hasNext()) {
Student next = it.next();
if(id == next.getId()) {
return next;
}
}
return null;
}
//排序
public void sort() {
stus = new TreeSet<Student>(stus);
}
public void show() {
System.out.println("所有学生的信息");
for (Student s : stus) {
System.out.println(s);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStus() {
return stus;
}
public void setStus(Set<Student> stus) {
this.stus = stus;
}
}
MainClass
package com.xuedao.model;
public class MainClass {
public static void main(String[] args) {
Classes c = new Classes();
c.init(10);
c.show();
c.sort();
c.show();
}
}