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