import java.io.*;
import java.util.*;
abstract class Employee{
float n,m,t;
Scanner in =new Scanner(System.in);
abstract float earning();
}
final class Boss extends Employee{
public float earning(){
System.out.print("请输入Boss的每周工资:");
float a=in.nextFloat();
return a;
}
}
final class CommissionWorker extends Employee{
public float earning(){
System.out.print("请输入CommissionWorker的每周基本工资:");
float n=in.nextFloat();
System.out.print("请输入CommissionWorker的销售额:");
float m=in.nextFloat();
System.out.print("请输入CommissionWorker的销售额的权数:");
float c=in.nextFloat();
t=n+m*c;
return t;
}
}
final class PieceWorker extends Employee{
public float earning(){
System.out.print("请输入PieceWorker的生产产品数目:");
float n=in.nextFloat();
System.out.print("请输入PieceWorker每件产品的权数:");
float m=in.nextFloat();
t=n*m;
return t;
}
}
final class HourlyWorker extends Employee{
public float earning(){
System.out.print("请输入HourlyWorker的工作时间:");
float n=in.nextFloat();
System.out.print("请输入HourlyWorker的工作时间相关权数:");
float m=in.nextFloat();
return t=n*m;
}
}
public class Pay1{
public static void main(String[] args)throws IOException{
int a,b;
Boss ba=new Boss();
CommissionWorker bb=new CommissionWorker();
PieceWorker bc=new PieceWorker();
HourlyWorker bd=new HourlyWorker();
System.out.println("请输入您要进行的操作:1.Boss 2.CommissionWorker 3.PieceWorker 4.HourlyWorker");
Scanner in =new Scanner(System.in);
int i =in.nextInt();
switch(i){
case 1:System.out.println("Boss的每周工资为:"+ba.earning()+"元。");
break;
case 2:System.out.println("CommissionWorker的工资为每周"+(bb.earning())+"元。");
break;
case 3:System.out.println("PieceWorker的工资为每周"+(bc.earning())+"元。");
break;
case 4:System.out.println("HourlyWorker的工资为每周"+(bd.earning())+"元。");
break;
default:System.exit(1);
}
}
}
请问一下高手用final修饰的类有什么用,或者为什么要用final来修饰类呢,就向我的这个程序那样?
----------------解决方案--------------------------------------------------------
final和c++里的const差不多 表示常量
----------------解决方案--------------------------------------------------------
那这里是表示什么意思呢
----------------解决方案--------------------------------------------------------
final修饰的类不能被继承.
final修饰的方法不能被子类重写
----------------解决方案--------------------------------------------------------
final类不能被继承,所以其中不能有没实现的方法.它可以防止方法的重写.
----------------解决方案--------------------------------------------------------
final修饰方法表示最终方法,它不能被继承
----------------解决方案--------------------------------------------------------