3
I.
ma() , mb() , mc() , md()
II.
public class TestClassE{
public static void main(String[] args){
Ic ic = new ClassE();
((ClassE) ic).ma();
((ClassE) ic).mb();
ic.mc();
((ClassE) ic).md();
}
}
III.
true
true
true
true
true
4
ture
true
true
true
5
ACDE
6
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green
7
TeacherA teach Java
TeacherB teach Java
8
//1
for(int i = 0; i < as.length, i++){
as[i].eat();
}
//2
for(int i = 0; i < as.length, i++){
((Pet)as[i]).play();
}
9
package company;
public class TestEmployee {
public static void main(String[] args) {
Employee[] all = new Employee[5];
all[0] = new SalariedEmployee("tom",7,1000);
all[1] = new BasePlusSalesEmployee("mike", 8, 4000, 0.5, 1000);
all[2] = new HourlyEmployee("jim", 1, 15, 170);
all[3] = new SalesEmployee("annie", 7, 5000, 0.7);
getExtraPay(all,7);
}
public static void getExtraPay(Employee[] all , int month){
double extraPay = 0;
for(int i = 0; i < all.length; i++){
if(all[i] instanceof ExtraPay){
extraPay += ((ExtraPay)all[i]).extraPay();
}
}
System.out.println(extraPay);
}
}
abstract class Employee{
private String name;
private int month;
public Employee(String name, int month) {
super();
this.name = name;
this.month = month;
}
public double getSalary(int month){
if(this.month == month){
return 100;
}
return 0;
}
}
class SalariedEmployee extends Employee implements ExtraPay{
private double salary;
public SalariedEmployee(String name, int month, double salary) {
super(name, month);
this.salary = salary;
}
public double getSalary(int month) {
salary += super.getSalary(month) + this.extraPay();
return salary;
}
@Override
public double extraPay() {
return 2000;
}
}
class HourlyEmployee extends Employee{
private double hourlySalary;
private int hours;
public HourlyEmployee(String name, int month, double hourlySalary, int hours) {
super(name, hours);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getSalary(int month){
double salary;
if(hours <= 160){
salary = hours * hourlySalary + super.getSalary(month);
return salary;
}
salary = 160 * hourlySalary + (hours - 160) * hourlySalary * 1.5 + super.getSalary(month);
return salary;
}
}
class SalesEmployee extends Employee{
private double sales;
private double rate;
public SalesEmployee(String name, int month, double sales, double rate) {
super(name, month);
this.sales = sales;
this.rate = rate;
}
public double getSalary(int month){
double salary;
salary = rate * sales +super.getSalary(month);
return salary;
}
}
class BasePlusSalesEmployee extends SalesEmployee implements ExtraPay{
private double baseSalary;
public BasePlusSalesEmployee(String name, int month, double sales, double rate, double baseSalary) {
super(name, month, sales, rate);
this.baseSalary =baseSalary;
}
public double getSalary(int month){
double salary;
salary = super.getSalary(month) + baseSalary + this.extraPay();
return salary;
}
@Override
public double extraPay() {
return 1000;
}
}
interface ExtraPay{
double extraPay();
}
10
区别:第一种MyService需覆盖ServiceInterface接口中包含的所有方法,第二种则不需要。
AbstractService作用:为MyService提供默认方法。
11
import java.util.Scanner;
public class TestT11 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一个大于6的整数");
int num = input.nextInt();
split(num);
}
public static void split(int num){
if(num > 6 && num % 2 == 0){
for(int i = 2; i < num; i++){
if(check(i) && check(num-i)){
System.out.println(num + "=" + i + "+" + (num - i));
return;
}
}
}
System.out.println("输入有误");
}
public static boolean check(int numi){
for(int i = 2; i < numi; i++){
if(numi % i == 0){
return false;
}
}
return true;
}
}