Java【13_1】final、初始化块、继承(测试题)
测试题
1、简述final修饰符的功能
① 修饰类 该类不能被继承
② 修饰变量 该变量就是常量(一旦被初始化,就不可以修改)
③ 修饰方法 该方法不能被重写
2、写出程序结果 (仔细认真)
public class MyClass {
static int x,y;
static{
int x=5;
x--; //x是局部变量
}
static{
x--; //x=-1
}
public static void main(String[] args) {
x--; //x=-2
myMethod();
System.out.println(x+y+ ++x);//0 + -2 + 1 =-1
}
public static void myMethod() {
y=x++ + ++x;//y = -2 + 0 y=-2 x=0
}
}
3、案例:分析运行结果
public class Test06 {
public static void main(String[] args) {
Sub s = new Sub();
}
}
class Base{
Base(){
this.method(100); //看实际类型
}
{
System.out.println("base");
}
public void method(int i){
System.out.println("base : " + i);
}
}
class Sub extends Base{
Sub(){
super();
super.method(70);
}
{
System.out.println("sub");
}
public void method(int j){ //重载的方法
System.out.println("sub : " + j);
}
}
D:\javademo\day13_am>javac Test06.java
D:\javademo\day13_am>java Test06
base
sub : 100
sub
base : 70
4、案例:分析运行结果
class HelloA{
public HelloA(){
System.out.println("HelloA");
}
{
System.out.println("I'm A Class");
}
static{
System.out.println("static A");
}
}
public class HelloB extends HelloA{
public HelloB(){
System.out.println("HelloB");
}
{
System.out.println("I'm B Class");
}
static{
System.out.println("static B");
}
public static void main(String[] args) {
new HelloB();
}
}
D:\javademo\day13_am>javac HelloB.java
D:\javademo\day13_am>java HelloB
static A
static B
I'm A Class
HelloA
I'm B Class
HelloB
5、按下面步骤实现功能:
5.1、声明一个父类Employee员工类型,有属性,姓名(String)
有方法,public abstract double earning() 用于返回实发工资
public String getInfo():显示姓名和实发工资
5.2、声明一个子类SalaryEmployee正式工,继承父类Employee,增加属性,薪资,工作日天数,请假天数
重写方法,public double earning()返回实发工资,实发工资 = 薪资 - 薪资/工作日天数 * 请假天数,
5.3、声明一个子类HourEmployee小时工,继承父类Employee
有属性,工作小时数,每小时多少钱
重写方法,public double earning()返回实发工资, 实发工资 = 每小时多少钱 * 小时数
5.4、声明一个子类Manager经理,继承SalaryEmployee,增加属性:奖金比例
重写方法,public double earning()返回实发工资,实发工资 = (薪资 - 薪资/工作日天数 * 请假天数)*(1+奖金比例)
5.5、你现在是财务,需要查看每个人的实发工资,并查看工资总额。
声明一个员工数组,存储各种员工,并遍历显示他们的姓名和实发工资,并计算工资总额
示例代码:
//员工
public abstract class Employee {
private String name;
public abstract double earning();
public String getInfo(){
return name+"的工资:"+earning();
}
public Employee(String name) {
this.name = name;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//正式工
public class SalaryEmployee extends Employee {
private double salary;
private int workDay;
private int offDay;
@Override
public double earning() {
return salary-salary/workDay*offDay;
}
public SalaryEmployee(String name, double salary, int workDay, int offDay) {
super(name);
this.salary = salary;
this.workDay = workDay;
this.offDay = offDay;
}
public SalaryEmployee() {
}
//子类独有方法
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getWorkDay() {
return workDay;
}
public void setWorkDay(int workDay) {
this.workDay = workDay;
}
public int getOffDay() {
return offDay;
}
public void setOffDay(int offDay) {
this.offDay = offDay;
}
}
//小时工
public class HourEmployee extends Employee {
private int times;
private double money;
@Override
public double earning() {
return times*money;
}
public HourEmployee(String name, int times, double money) {
super(name);
this.times = times;
this.money = money;
}
public HourEmployee() {
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
//经理
public class Manager extends SalaryEmployee {
private double bonus;
@Override
public double earning() {
return super.earning()*(1+bonus);
}
public Manager(String name, double salary, int workDay, int offDay, double bonus) {
super(name, salary, workDay, offDay);
this.bonus = bonus;
}
public Manager() {
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
//财务计算工资
public class Test1 {
public static void main(String[] args) {
//创建一个数组,进行员工的存储
Employee[] employees=new Employee[4];
employees[0]=new SalaryEmployee("张三",50000,22,2);
employees[1]=new HourEmployee("张三",20,100);
employees[2]=new Manager("老李你要老婆不要",60000,22,2,0.5);
employees[3]=new Manager("老许",70000,22,1,0.5);
double sum=0;
for (int i = 0; i < employees.length; i++) {
System.out.println(employees[i].getInfo()); //属性看类型,方法看对象!
sum+=employees[i].earning();//多态
}
System.out.println(sum);
}
}
D:\javademo\day13_am>javac Test1.java
D:\javademo\day13_am>java Test1
张三的工资:45454.545454545456
张三的工资:2000.0
老李你要老婆不要的工资:81818.18181818182
老许的工资:100227.27272727274
229500.0