大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
成都创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为矿区企业提供专业的成都做网站、网站建设,矿区网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
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 "学号:"+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("这些学生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
//哈哈,感觉这道题真心好啊。不知道楼主是从哪里看到的。
//首先这道题楼主要明白以下两点:
//1:继承时,子类会隐藏父类相同的方法,要调用父类方法就必须使用super关键字。
//2:向上转型时,子类会丢失和父类不同的方法,可以使用父类的不同名的所有方法。
public class PolyDemo09{
public static void main(String[] args){
A a1 = new A();
A a2 = new B();//B类型向上转型丢失与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类方法有,所以不会向上转型,所以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有对应方法,所以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有相应方法,所以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 {
}
创建一个名字为“ReportCard”的类,然后用下边的内容全部替换掉,你会成为全班最亮的仔。
import java.util.HashMap;
/**
* 学生成绩单
*/
public class ReportCard {
public static void main(String[] args) {
ReportCard reportCard = new ReportCard("张三", "070602213");
reportCard.set("语文", 80.0);
reportCard.set("数学", 59.5);
reportCard.set("英语", 66.0);
reportCard.set("java", 80, 99.0);
reportCard.set("数据库", 80, 66.0);
reportCard.set("毛概", null);
System.out.println(reportCard.getStudentName() + "语文分数:" + reportCard.get("语文"));
System.out.println(reportCard.getStudentName() + "数学考核结果:" + (reportCard.isPassed("数学") ? "合格" : "不合格"));
System.out.println(reportCard.getStudentName() + "期末是否挂科:" + (reportCard.isAllPassed() ? "否" : "是"));
}
// 学生姓名
private String studentName;
// 学生学号
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{
// 学生成绩
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;
//这里一共被加了九次 没悬念吧因为循环了九次
count=count+1;
//这里函数进去,在 5的时候 返回了1,大于等于5的时候返回0,所以这里一共被加了5次,没悬念吧
count=count+m4a[x].maybeNew(x);
x=x+1;
}
//所以这里出现的结果就是 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;
}
}