【JavaSE_07】Java中类和对象-封装特性--练习
生活随笔
收集整理的這篇文章主要介紹了
【JavaSE_07】Java中类和对象-封装特性--练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.編寫封裝一個學生類,有姓名,有年齡,有性別,有英語成績,數學成績,語文成績,一個學生類,我們關注姓名,年齡,學歷等信息,要求年齡必須在19-40歲之間,默認為19,學歷必須是大專,本科,研究生這幾個值的范圍內,默認為大專。創建對象,測試其相關方法
類的創建:
/*** 3.編寫一個學生類,我們關注姓名,年齡,學歷等信息,* 要求年齡必須在19-40歲之間,默認為19,學歷必須是大專,* 本科,研究生這幾個值的范圍內,默認為大專。創建對象,測試其相關方法* @author Administrator**/ public class Student {private String name;private int age;private String education;//set getpublic void setName(String name){this.name=name;//這里必須加this,因為出現了局部變量和成員變量重名,必須指明name是哪個,下面同理}public String getName(){return this.name;}public void setAge(int age){if(age<19||age>40){this.age=19;}else{this.age=age;} }public int getAge(){return this.age;}public void setEducation(String education){if(education.equals("大專")||education.equals("本科")||education.equals("研究生")){this.education=education;}else{this.education="大專";}}public String getEducation(){return this.education;}}類的測試:
public class StudentTest {public static void main(String[] args){//1,創建對象Student student=new Student();//2,賦值student.setName("張三");student.setAge(28);student.setEducation("本科");//3,獲取System.out.println(student.getName());System.out.println(student.getAge());System.out.println(student.getEducation());System.out.println();//再創建一個對象.用于驗證不符合條件情況Student student2=new Student();student2.setName("李四");student2.setAge(10);student2.setEducation("博士");System.out.println(student2.getName());System.out.println(student2.getAge());System.out.println(student2.getEducation());} }2.封裝方法,求總分,平均分,最高分,最低分,以及打印學生的信息。創建對象,測試其相關方法
類的創建:
/*** 4.封裝一個學生類,有姓名,有年齡,有性別,有英語成績,數學成績,* 語文成績,封裝方法,求總分,平均分,最高分,最低分,* 以及打印學生的信息。創建對象,測試其相關方法* @author Administrator**/ public class Student2 {private String name;private int age;private String sex;private double english;private double math;private double chinese;//set getpublic void setName(String name){this.name=name;}public String getName(){return this.name;}public void setAge(int age){this.age=age;}public int getAge(){return this.age;}public void setSex(String sex){this.sex=sex;}public String getSex(){return this.sex;}public void setEnglish(double english){this.english=english;}public double getEnglish(){return this.english;}public void setMath(double math){this.math=math;}public double getMath(){return this.math;}public void setChinese(double chinese){this.chinese=chinese;}public double getChinese(){return this.chinese;}//構造函數實現直接給屬性賦值public Student2(){}public Student2(String name,int age,String sex,double english,double math,double chinese){this.setName(name);this.setAge(age);this.setSex(sex);this.setEnglish(english);this.setMath(math);this.setChinese(chinese);}//getSumpublic double getSum(){return this.english+this.math+this.chinese;}//getAveragepublic double getAverage(){return this.getSum()/3;}//getMaxpublic double getMax(){double max=this.english>this.math?this.english:this.math;max=max>this.chinese?max:this.chinese;return max;}//getMinpublic double getMin(){double min=this.english<this.math?this.english:this.math;min=min<this.chinese?min:this.chinese;return math;}}類的測試:
public class Student2Test {public static void main(String[] args){Student2 student=new Student2("張飛",99,"男",88,77,79);System.out.println(student.getName()+"成績分析如下:");System.out.println("總分:"+student.getSum());System.out.println("平均分:"+student.getAverage());System.out.println("最高分分:"+student.getMax());System.out.println("最低分:"+student.getMin());System.out.println();//也可以和上一個例題一個個賦值Student2 student2=new Student2();student2.setName("劉備");student2.setAge(78);student2.setEnglish(99);student2.setMath(88);student2.setChinese(89);System.out.println(student2.getName()+"成績分析如下:");System.out.println("總分:"+student2.getSum());System.out.println("平均分:"+student2.getAverage());System.out.println("最高分分:"+student2.getMax());System.out.println("最低分:"+student2.getMin());} }3.編寫一個隨機點名系統,兩個功能,一個是抽取學員回答問題,一個是記錄學員被命中的次數
類的創建:
package day09; /*** 編寫一個隨機點名系統,兩個功能,* 一個是抽取學員回答問題,一個是記錄學員被命中的次數* @author Administrator**/ public class NameSystem {private String name;private int times;//構造方法public NameSystem(){}public NameSystem(String name,int times){this.setName(name);this.setTimes(times);}//set getpublic void setName(String name){this.name=name;}public void setTimes(int times){this.times=times;}public String getName(){return this.name;}public int getTimes(){return this.times;} }類的調用:
package day09; import java.util.Scanner; public class NameSystemTest {public static void main(String[] args){//1,創建對象/*NameSystem[] persons=new NameSystem[5];persons[0].setName("張三");persons[0].setTimes(0);//動態賦值persons[0]=new NameSystem("小李",0);persons[1]=new NameSystem("小張",0);persons[2]=new NameSystem("小王",0);persons[3]=new NameSystem("小明",0);persons[4]=new NameSystem("小剛",0);*///靜態賦值NameSystem[] persons={new NameSystem("小李",0),new NameSystem("小張",0),new NameSystem("小王",0),new NameSystem("小明",0),new NameSystem("小剛",0)};Scanner input=new Scanner(System.in);String isContinue;//2,點名do{int index=(int)(Math.random()*persons.length);System.out.println("本次抽中的學員是:"+persons[index].getName());persons[index].setTimes(persons[index].getTimes()+1);System.out.println("繼續請輸入y,其他任意鍵退出");isContinue=input.next();}while(isContinue.equals("y"));System.out.println("本次點名的結果統計:");for(NameSystem p:persons){System.out.println(p.getName()+":"+p.getTimes());}//關閉io流input.close();} }總結
以上是生活随笔為你收集整理的【JavaSE_07】Java中类和对象-封装特性--练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【JavaSE_07】Java中类和对象
- 下一篇: 【JavaSE_08】Java中stat