按照題目要求編寫的用javaBean規(guī)范設(shè)計的學(xué)生類Student的Java程序如下
成都創(chuàng)新互聯(lián)長期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為礦區(qū)企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè),礦區(qū)網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
需要創(chuàng)建user.java.test包,把Student.java文件和Test.java文件放入包中,編譯Student.java文件并且編譯運行Test.java文件得到運行結(jié)果
Student.java文件代碼如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "學(xué)號:"+no+",姓名:"+name+",成績:"+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代碼如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","張三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","趙六",73.5);
sArray[4]=new Student("005","孫七",66);
System.out.println("這些學(xué)生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
//哈哈,感覺這道題真心好啊。不知道樓主是從哪里看到的。
//首先這道題樓主要明白以下兩點:
//1:繼承時,子類會隱藏父類相同的方法,要調(diào)用父類方法就必須使用super關(guān)鍵字。
//2:向上轉(zhuǎn)型時,子類會丟失和父類不同的方法,可以使用父類的不同名的所有方法。
public?class?PolyDemo09{
public?static?void?main(String[]?args){
A?a1?=?new?A();
A?a2?=?new?B();//B類型向上轉(zhuǎn)型丟失與A類不同方法
B?b?=?new?B();?
C?c?=?new?C();
D?d?=?new?D();
System.out.println("⑴????"?+?a1.show(b));//B類的父類是A,所以A?and?A
System.out.println("⑵????"?+?a1.show(c));?//C類父類的父類是A,D和他是同級。所以A?and?A
System.out.println("⑶????"?+?a1.show(d));//D類方法有,所以不會向上轉(zhuǎn)型,所以A?and?D
System.out.println("⑷????"?+?a2.show(b));?/*注意這時候a2的兩個方法其實是
public?String?show(D?obj)?{
return?("A?and?D");
}
public?String?show(A?obj)?{
return?("B?and?A");
}??B的父類是A,所以B?and?A?
*/
/**/???????????????????????????????????????????????????????????
System.out.println("⑸????"?+?a2.show(c));//C的父類的父類是A,所以B?and?A;
System.out.println("⑹????"?+?a2.show(d));??//D有對應(yīng)方法,所以A?and?D
System.out.println("⑺????"?+?b.show(b));?/*這個就是繼承了,繼承除了隱藏父類中和子類同名的方法外,在子類中可以直接使用父類的方法。所以B?and?B
所以就變成了
public?String?show(D?obj)?{
return?("A?and?D");
}
public?String?show(B?obj)?{
return?("B?and?B");
}
public?String?show(A?obj)?{
return?("B?and?A");
*/
System.out.println("⑻????"?+?b.show(c));?//C?的父類是B,所以B?and?B
System.out.println("⑼????"?+?b.show(d));//D有相應(yīng)方法,所以A?and?D
}
}
class?A?{
public?String?show(D?obj)?{
return?("A?and?D");
}
public?String?show(A?obj)?{
return?("A?and?A");
}
}
class?B?extends?A?{
public?String?show(B?obj)?{
return?("B?and?B");
}
public?String?show(A?obj)?{
return?("B?and?A");
}
}
class?C?extends?B?{
}
class?D?extends?B?{
}
創(chuàng)建一個名字為“ReportCard”的類,然后用下邊的內(nèi)容全部替換掉,你會成為全班最亮的仔。
import java.util.HashMap;
/**
* 學(xué)生成績單
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("張三", "070602213");
reportCard.set("語文", 80.0);
reportCard.set("數(shù)學(xué)", 59.5);
reportCard.set("英語", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("數(shù)據(jù)庫", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "語文分數(shù):" + reportCard.get("語文"));
System.out.println(reportCard.getStudentName() + "數(shù)學(xué)考核結(jié)果:" + (reportCard.isPassed("數(shù)學(xué)") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否掛科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 學(xué)生姓名
private String studentName;
// 學(xué)生學(xué)號
private String studentNumber;
// 成績單
private HashMapString, CourseResult cards = new HashMap();
public ReportCard() {
}
public ReportCard(String studentName, String studentNumber) {
this.studentName = studentName;
this.studentNumber = studentNumber;
}
public Double get(String courseName){
CourseResult courseResult = cards.get(courseName);
return courseResult == null ? Double.NaN : courseResult.getStudentScore();
}
public void set(String courseName, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, studentScore);
cards.put(courseName, courseResult);
}
public void set(String courseName, double passMark, Double studentScore){
CourseResult courseResult = new CourseResult(courseName, passMark, studentScore);
cards.put(courseName, courseResult);
}
public boolean isPassed(String courseName){
return cards.get(courseName).isPassed();
}
public boolean isAllPassed(){
for(CourseResult cr : cards.values()){
if ( ! cr.isPassed()) {
return false;
}
}
return true;
}
public String getStudentName() {
return studentName;
}
public String getStudentNumber() {
return studentNumber;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
/**
* 課程
*/
class Course{
// 課程名稱
protected String courseName;
// 及格分
protected double passMark = 60;
public Course(String courseName, Double passMark) {
this.courseName = courseName;
if ( passMark != null) {
this.passMark = passMark;
}
}
}
/**
* 課程成績
*/
class CourseResult extends Course{
// 學(xué)生成績
private Double studentScore;
public CourseResult(String courseName, Double studentScore) {
this(courseName, null, studentScore);
}
public CourseResult(String courseName, Double passMark, Double studentScore) {
super(courseName, passMark);
this.studentScore = studentScore == null ? Double.NaN : studentScore;
}
public boolean isPassed(){
return studentScore = passMark;
}
public String getCourseName() {
return courseName;
}
public double getPassMark() {
return passMark;
}
public Double getStudentScore() {
return studentScore;
}
}
public?class?test1{
int?counter=0;
public?static?void?main(String[]args){
int?count=0;
test1[]m4a=new?test1[20];
int?x=0;
while(x9)
{
//由于counter不是static的,所以每次new?test1()的時候,counter均為0
m4a[x]=new?test1();
//所以這里m4a[0]到m4a[8]的counter均為1
m4a[x].counter=m4a[x].counter+1;
//這里一共被加了九次?沒懸念吧因為循環(huán)了九次
count=count+1;
//這里函數(shù)進去,在??5的時候?返回了1,大于等于5的時候返回0,所以這里一共被加了5次,沒懸念吧
count=count+m4a[x].maybeNew(x);
x=x+1;
}
//所以這里出現(xiàn)的結(jié)果就是?9+5=14??和?1
System.out.println(count+"?"+m4a[1].counter);
}
public?int?maybeNew(int?index)
{
if(index5)
{
test1?m4=new?test1();
m4.counter=m4.counter+1;
return?1;
}
return?0;
}
}