定义学生类用来存储学生的信息

***定义的学生类:包括属性、属性的getter、setter 方法

public class Student {//定义一个学生类,包括学号,姓名,三科成绩
	//属性和方法
	private String number;
	private String name;
	private int englishScore;
	private int mathScore;
	private int sportsScore;
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getEnglishScore() {
		return englishScore;
	}
	public void setEnglishScore(int englishScore) {
		this.englishScore = englishScore;
	}
	public int getMathScore() {
		return mathScore;
	}
	public void setMathScore(int mathScore) {
		this.mathScore = mathScore;
	}
	public int getSportsScore() {
		return sportsScore;
	}
	public void setSportsScore(int sportsScore) {
		this.sportsScore = sportsScore;
	}
	public Student(String number, String name, int englishScore, int mathScore, int sportsScore) {
		super();
		this.number = number;
		this.name = name;
		this.englishScore = englishScore;
		this.mathScore = mathScore;
		this.sportsScore = sportsScore;
	}
	@Override
	public String toString() {
		return "Student [number=" + number + ", name=" + name + ", englishScore=" + englishScore + ", mathScore="
				+ mathScore + ", sportsScore=" + sportsScore + "]";
	}
}

2)查询并输出所有科目不及格的人数及名单。

public class TestStudent {
	private static Student[] stu;//声明对象数组
	public static void main(String[] args) {
		stu=new Student[5];//实例化对象数组
		stu[0]=new Student("111","aaaa",30,80,70);
		stu[1]=new Student("222","aabb",60,80,20);
		stu[2]=new Student("333","bbcc",40,80,70);
		stu[3]=new Student("444","ddee",30,30,70);
		stu[4]=new Student("555","ffgg",30,80,20);
		search("a");
		searchfail();
	}
	/*
	 * 根据姓名查找成绩
	 */
	public static void search(String name) {
		for(int i=0;i

(3)自己定义包含main方法的类,类中定义2个方法,分别用来实现按名字查询成绩、查找不及格人数并打印不及格名单的功能。。
public class TestChange {

public static void main(String[] args) {
	//实例化两个Swap对象
	Swap s1=new Swap(10);
	Swap s2=new Swap(100);
	swap(s1,s2);
	System.out.println(s1.getNum());
	System.out.println(s2.getNum());
}

public static void swap(Swap s1, Swap s2) {
	/*int temp=s1.getNum();
	s1.setNum(s2.getNum());
	s2.setNum(temp);*/
	Swap temp=s1;
	s1=s2;
	s2=temp;
	
}

}

public class Swap {
	private int num;
	//定义一个变量
	
	//setter,getter方法
	public int getNum() {
		return num;
	}

	public Swap(int num) {
		super();
		this.num = num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	
}

你可能感兴趣的:(getter,setter,toString)