生活随笔
收集整理的這篇文章主要介紹了
Java实现一个学生类Student
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
- 1、設(shè)計一個類Student,該類包括姓名、學(xué)號和成績。設(shè)計一個方法,按照成績從高到低的順序輸出姓名、學(xué)號和成績信息。
public class Student {public static void main(String[] args) {Student [] num =new Student [3]; //創(chuàng)建一個一維數(shù)組num[0] = new Student ("scc",112,123); //為一維數(shù)組賦值num[1] = new Student ("sca",2018,1234);num[2] = new Student ("scb",2019,1235);// Student temp = new Student (); //兩種調(diào)用方法,使用這一種,需要在寫一個無參的構(gòu)造方法。一般都用下面的方法。
// temp.swap(num);Student .swap(num); //類名.方法名for(Student stu : num){ //foreach循環(huán),for(元素類型t 元素變量x : 遍歷對象obj)stu.show();}}private String name;private int number;private int score;// public String getName() {
// return name;
// }
// public void setName(String name) {
// this.name = name;
// }
// public int getId() {
// return number;
// }
// public void setId(int id) {
// this.number = number;
// }
// public int getScore() {
// return score;
// }
// public void setScore(int score) {
// this.score = score;
// }public Student (){ }public Student (String name,int number,int score){//super();this.name = name;this.number = number;this.score = score;}public void show(){System.out.println("姓名:" + name + "學(xué)號: " + number + "成績" + score);}public static void swap(Student [] stus){//String temp; //這樣定義的錯誤,是因為,在下面交換的是stus[j],而stus是StudentTrue來定義的所以,如下定義Student temp; for(int i = 0;i < stus.length-1;i++){for(int j = 0;j < stus.length - i - 1;j++){if(stus[j].score > stus[j + 1].score){temp = stus[j]; //整個數(shù)組都變換,而不是僅僅變換成績stus[j + 1] = stus[j];stus[j] = temp;}}}}
}
總結(jié)
以上是生活随笔為你收集整理的Java实现一个学生类Student的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。