本实例实现计算机专业和会计专业的学生成绩统计,首先创建哈伊尔课程基础类,然后分别用计算机专业类和会计专业列继承课程技术类,最后在main方法中分别调用。实现代码如下:
import java.util.Scanner;
public class ExtendsDemo1 {
public static void mian (String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name=sc.next();
System.out.println("请选择学生专业:");
System.out.println("1.会计");
System.out.println("2.计算机");
int choose=sc.nextInt();
if(choose==1){//选择会计专业
System.out.println("请输入数学成绩:");
double maths=sc.nextDouble();
System.out.println("请输入英语成绩:");
double english=sc.nextDouble();
System.out.println("请输入会计基础成绩:");
double accountBase=sc.nextDouble();
System.out.println("请输入会计英语成绩:");
double accountEnglish=sc.nextDouble();
Accounting account=new Accounting(name,"会计",maths,english,accountBase,accountEnglish);
System.out.println("学生信息为:");
System.out.println(account.toString());//调用accounting类中的toString()方法
}
else if(choose==2){//选择计算机专业
System.out.println("请输入数学成绩:");
double maths=sc.nextDouble();
System.out.println("请输入英语成绩:");
double english=sc.nextDouble();
System.out.println("请输入数据库成绩:");
double datebase=sc.nextDouble();
System.out.println("请输入Java成绩:");
double javaProgram=sc.nextDouble();
Computer computer=new Computer(name,"计算机",maths,english,datebase,javaProgram);
System.out.println("学生信息为:");
System.out.println(computer.toString());//调用computer类中的toString()方法
}
else{
System.out.priintln("专业输入错误!");
}
}
}
//基础信息类
class BaseCourse{//构造函数
private String name;//姓名
private String major;//专业
private double maths;//数学
private double english;//英语
public BaseCourse(String name,String major,double english){
this.name=name;
this.major=major;
this.maths=naths;
this.english=english;
}
public double getMaths(){
return maths;
}
public void setMaths(double maths){
this.maths=maths;
}
public void setEnglish(double english){
this.english=english;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getMajor(String name){
return major;
}
public void setMajor(Stirng major){
this.major=major;
}
}
//计算机专业
class Computer extends BaseCourse{
private double datebase;//数据库
private double javaProgram;//java 编程
public Computer(String name,String major,double maths,double english,double datebase,doouble javaProgram){
super(name,major,maths,english);//调用父类的构造方法
this.datebase=datebase;
this.javaProgram=javaProgram;
}
public doublegetDatebase(){
return datebase;
}
public void setDatebase(double datebase){
this.database=database;
}
public void setJavaProgram(double javaProgram){
this.javaProgram=javaProgram;
}
///重写toString()方法,用于输出属性值的以及科目的总分
public String toString(){
return this.getName()+":"+this.getMajor()+":总分为:"+(this.getMaths()+this.getEnglish()+this.getDatebase()+this.getJavaProgram());
}
//会计专业
class Accounting extends BaseCourse{
private doubleaccountEnglish;//会计英语
public Accounting(String name,String major,double maths,double english,double accountBase,double accountEnglish){
super(name,major,maths,english);//调用父类的构造方法
this.accountBase=accountBase;
this.accountEnglish=accountEnglish;
}
public double getAccountbase(){
return accountBase;
}
public void setAccountbase(double accountBase){
this.accountBase=accountBase;
}
public double getAccountEnglish(){
return accountEnglish;
}
public void setAccountEnglish(double accountEnglish){
this.accountEnglish=accountEnglish;
}
//重写toString()方法,用于输出属性值以及科目的总分
publicString toString(){
return this.getName()+":"+this.getMajor()+":总分为:"+(this.getMaths()+this.getEnglish()+this.getAccountBase()+this.getAcocountEnglish());
}
}
}