java寫一個類,並測試它

來源:互聯網
上載者:User

標籤:com   bsp   遍曆   需求   [1]   ***   賦值   length   類對象   

這是Student類
package com.ma_0002;/** * 建立一個學生類,重寫toString()方法 * @author TP * */public class Student {/************成員變數區****************//**學生姓名*/String name;/**學生年齡*/int age;/************構造方法區****************/public Student() {super();}public Student(String name, int age) {super();this.name = name;this.age = age;}/************get和set方法區****************/public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}/************重寫了toString()方法****************/@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}}

  



這是測試類別StudentTest
package com.ma_0002;/** * Student測試類別 * @author TP * */public class StudentTest {/* * 需求: * 建立幾個Student對角,並把它們放入一個數組中,然後輸出 *  * 分析: *  1.用Student構造方法建立對象 *  2.建立一個Student[]數組來放對象 *  3.輸出  */public static void main(String[] args) {//建立一個學生類對象Student s = new Student("張大",1);Student s2 = new Student("張二",2);Student s3 = new Student("張三",3);Student s4 = new Student("張四",4);Student s5 = new Student("張五",5);Student s6 = new Student("張六",6);//建立一個Student[]數組來放對象Student[] stuArr = new Student[6];//給數組賦值stuArr[0]  = s;stuArr[1]  = s2;stuArr[2]  = s3;stuArr[3]  = s4;stuArr[4]  = s5;stuArr[5]  = s6;//遍曆輸出數組元素for (int i = 0; i < stuArr.length; i++) {System.out.println("s"+i+" : "+stuArr[i]);}}}

  執行結果:

s0 : Student [name=張大, age=1]
s1 : Student [name=張二, age=2]
s2 : Student [name=張三, age=3]
s3 : Student [name=張四, age=4]
s4 : Student [name=張五, age=5]
s5 : Student [name=張六, age=6]

  

 

java寫一個類,並測試它

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.