3.1
class ClassE implements ID{
public void ma() {}
public void mb() {}
public void mc() {}
public void md() {}
}
3.2
public class Test03 {
public static void main(String[] args) {
IC ic = new ClassE();
ID id = (ID)ic;
id.ma();
id.mb();
id.mc();
id.md();
}
}
3.3
true
true
true
true
true
true
true
true
true
ABCDE
Red Light shine in Red
Yellow Lighy shine in Yellow
Green Light shine in Green
TeacherA teach Java
TeacherB teach Java
public class Test08 {
public static void main(String[] args) {
Animal as[] = new Animal[3];
as[0] = new Dog();
as[1] = new Cat();
as[2] = new Wolf();
for(int i=0;i<as.length;i++) {
as[i].eat();
}
for(int i=0;i<as.length;i++) {
if(as[i] instanceof Pet) {
Pet as1 = (Pet)as[i];
as1.play();
}
}
}
}
abstract class Animal{
public abstract void eat();
}
interface Pet{
void play();
}
class Dog extends Animal implements Pet{
public void eat() {
System.out.println("Dog eat Bones");
}
public void play() {
System.out.println("Play with Dog");
}
}
class Cat extends Animal implements Pet{
public void eat() {
System.out.println("Cat eat fish");
}
public void play() {
System.out.println("Play with Cat");
}
}
class Wolf extends Animal{
public void eat() {
System.out.println("Wolf eat neat");
}
}
public class Test09 {
public static void main(String[] args) {//主方法
Employee[] as = new Employee[] {new SalariedEmployee("a",1,3000.0),
new HourlyEmployee("b",2,20.0,180),
new SalesEmployee("c",3,40000.0,0.2),
new BasePlusSalesEmployee("d",4,35000.0,0.2,3000.0)};
for(int i=0;i<as.length;i++) {
System.out.println(as[i].getName()+"的3月份的工资为:"+as[i].getSalary(3));
}
System.out.println("公司本月加班费总共发了:" + allovertimepay(as));
}
public static double allovertimepay(Employee[] a) {
int num1=0;
int num2=0;
for(int i=0;i<a.length;i++) {
if(a[i] instanceof SalariedEmployee) {
num1++;
}
if(a[i] instanceof BasePlusSalesEmployee) {
num2++;
}
}
return num1*2000.0+num2*1000.0;
}
}
interface OvertimePay1{
double overtimepay = 2000.0;
}
interface OvertimePay2{
double overtimepay = 1000.0;
}
class Employee{//员工父类
private String name;//姓名(私有属性)
private int month;//生日月份(私有属性)
public Employee() {}//无参构造
public Employee(String name, int month) {//有参构造
this.name = name;
this.month = month;
}
//属性访问
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getSalary(int month) {//生日月判断
if(month == this.month) {
return 100;
}else {
return 0;
}
}
}
class SalariedEmployee extends Employee implements OvertimePay1{//拿固定工资的员工,继承Employee
private double salar;//固定工资(私有属性)
public SalariedEmployee() {//无参构造
}
public SalariedEmployee(String name, int month,double salar) {//有参构造
super(name,month);//调用父类有参构造
this.salar = salar;//子类属性赋值
}
public double getSalary(int month) {//月薪计算
return this.salar+super.getSalary(month)+overtimepay;
}
//属性访问
public double getSalar() {
return salar;
}
public void setSalar(double salar) {
this.salar = salar;
}
}
class HourlyEmployee extends Employee{//按小时拿工资的员工,继承Employee
private double hourlySalar;//每小时的工资(私有属性)
private int hours;//每月工作小时数(私有属性)
public double getSalary(int month) {//月薪计算
if(this.hours>160) {
return hourlySalar*160+(hours-160)*hourlySalar*1.5+super.getSalary(month);
}else {
return hourlySalar*+super.getSalary(month);
}
}
public HourlyEmployee() {//无参构造
}
public HourlyEmployee(String name, int month,double hourlySalar,int hours) {//有参构造
super(name,month);//调用父类有参构造
this.hourlySalar = hourlySalar;//子类属性赋值
this.hours = hours;//子类属性赋值
}
//属性访问
public double getHourlySalar() {
return hourlySalar;
}
public void setHourlySalar(double hourlySalar) {
this.hourlySalar = hourlySalar;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
}
class SalesEmployee extends Employee{//按提成拿工资的员工,继承Employee
private double sales;//月销售额(私有属性)
private double rate;//提成率(私有属性)
public double getSalary(int month) {//月薪计算
return sales*rate+super.getSalary(month);
}
public SalesEmployee() {//无参构造
}
public SalesEmployee(String name, int month,double sales,double rate) {//有参构造
super(name,month);//调用父类有参构造
this.sales = sales;//子类属性赋值
this.rate = rate;//子类属性赋值
}
//属性访问
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
class BasePlusSalesEmployee extends SalesEmployee implements OvertimePay2{//按固定底薪和销售提成拿工资的员工,继承SalesEmployee
private double baseSalary;//固定底薪(私有属性)
public double getSalary(int month) {//月薪计算
return baseSalary+super.getSalary(month)+overtimepay;
}
public BasePlusSalesEmployee() {//无参构造
}
public BasePlusSalesEmployee(String name, int month,double sales,double rate,double baseSalary) {//有参构造
super(name,month,sales,rate);//调用父类有参构造
this.baseSalary = baseSalary;//子类属性赋值
}
//属性访问
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
}
10.
第一种方式是MyService类直接实现了ServiceInterface接口,需实现接口中的抽象方法
第二种方式是MyService类直接继承了AbstractService类,而AbstractService类中已经实现过了ServiceInterface接口中的方法,所以MyService类可以直接继承到,不需要再实现
public class Test11 {
public static void main(String[] args) {
MathTool a = new A();
m2(26,a);//1.调用工具方法
}
public static void m2(int n,MathTool tool) {//2.验证哥德巴赫猜想(工具)
for(int i=1;i<=n/2;i++) {
int a=i;
int b=n-i;
if(tool.isPrime(a) && tool.isPrime(b)) {
System.out.println(n+"="+a+"+"+b);
}
}
}
}
interface MathTool{//3.接口
public abstract boolean isPrime(int a);
}
class A implements MathTool{//4.实现类(判断质数)
public boolean isPrime(int a) {
boolean b = true;
for(int i=2;i<a;i++) {
if(a%i==0) {
b=false;
return b;
}
}
return b;
}
}