自學Java測試代碼 - 簡單地Student類

來源:互聯網
上載者:User

標籤:public   inf   void   style   code   getc   bsp   ack   div   

2017-08-23  23:45:38

writer:pprp

寫這個還蠻開心的

package test;public class Student {    //建立成員變數    String name = "";    String code = "";    String sex = "";    double achievement;    //建構函式    public Student(String name)    {        this.name = name;    }    //初始化函數    void set(String name, String code, String sex)    {        this.name = name;        this.code = code;        this.sex = sex;    }    //得到變數    public String getname()    {        return name;    }    public String getcode()    {        return code;    }    public String getsex()    {        return sex;    }    //設定初始化    public void setachi(double achievement)    {        this.achievement = achievement;    }    public double getachi()    {        return achievement;    }    public void print()    {        System.out.println("student:" + name + " code" + code + " sex" + sex);    }    public String tostring()    {        String infor = "student name: " + name + " code " + code + "sex" + sex;        return infor;    }    public static void main(String[] args) {        // TODO Auto-generated method stub        //new一個對象        Student st1 = new Student("name1");        Student st2 = new Student("name2");        Student st3 = new Student("name3");        Student st4 = new Student("name4");        Student st5 = new Student("name5");        Student st6 = new Student("name6");        Student st7 = new Student("name7");        Student st8 = new Student("name8");        Student st9 = new Student("name9");        Student st10 = new Student("name10");        //設定一個學生類的對象數組,將所有的對象放到數組內        Student[] st = new Student[]                {st1,st2,st3,st4,st5,                st6,st7,st8,st9,st10};        //設定器        st1.set("name1","1","man");        st2.set("name2","2", "female");        st3.set("name3","3", "man");        st4.set("name4","4","man");        st5.set("name5","5","female");        st6.set("name6","6","male");        st7.set("name7","7","female");        st8.set("name7","7","male");        st9.set("name8","8","female");        st10.set("name10","10","female");                System.out.println(st1.tostring());        System.out.println(st2.tostring());        System.out.println(st3.tostring());        System.out.println(st4.tostring());        System.out.println(st5.tostring());        System.out.println(st6.tostring());        System.out.println(st7.tostring());        System.out.println(st8.tostring());        System.out.println(st9.tostring());        System.out.println(st10.tostring());                st1.setachi(Math.random() * 100);        st2.setachi(Math.random() * 100);        st3.setachi(Math.random() * 100);        st4.setachi(Math.random() * 100);        st5.setachi(Math.random() * 100);        st6.setachi(Math.random() * 100);        st7.setachi(Math.random() * 100);        st8.setachi(Math.random() * 100);        st9.setachi(Math.random() * 100);        st10.setachi(Math.random() * 100);                st1.print();st2.print();st3.print();        st4.print();st5.print();st6.print();        st7.print();st8.print();st9.print();        st10.print();                for(int i = 0 ; i < st.length ; i++)            for(int j = 0 ; j < st.length ; j++)            {                if(st[i].achievement < st[j].achievement)                {                    Student tmp;                    tmp =  st[i];                    st[i] = st[j];                    st[j] = tmp;                }            }                System.out.println("best grader is " + st[9].name + " grade: " +         st[9].achievement + "sex is " + st[9].sex);        System.out.println("lowest grader is " + st[9].name + " grade: " +         st[9].achievement + "sex is " + st[9].sex);            }}

 

自學Java測試代碼 - 簡單地Student類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.