知识内容
- 接口的多态:多种不同类型的引用指向同一对象时,表示看待对象的视角不同;不同引用所能看到的对象范围不同,只能调用自身类型所声明的范围。
- 常见关系:
类与类:单继承、extends 父类名称
类与接口:多实现、implements 接口名称1 , 接口名称2 , 接口名称n
接口与接口:多继承、extends 父接口1 , 父接口2 , 父接口n - 常量接口:将常用于表示状态或固定值的变量,以静态常量的形式定义在接口中。
- 接口是一种标准
回调原理:现有接口的使用者,后有接口的实现者
(1)标准:接口
(2)工具:接口的使用者
(3)接口的实现者:程序员
(4)工具的调用者:程序员
今日练习
- 有如下代码:
interface IA{
void ma();
}
interface IB extends IA{
void mb();
}
interface IC{
void mc();
}
interface ID extends IB,IC{
void md();
}
(1)如果有一个类ClassE实现ID接口,如果不希望ClassE是抽象的,则需要实现哪些方法?
参考答案:
public void mb() {}
public void ma() {}
public void mc() {}
public void md() {}
(2)把下面的代码补充完整
public class TestClassE {
public static void main(String[] args) {
IC ic = new ClassE();
//调用ma方法
//调用mb方法
//调用mc方法
//调用md方法
}
}
答:
ClassE c1 = (ClassE)ic;
c1.ma();
c1.mb();
ic.mc();
c1.md();
(3)写出下面代码的输出结果
public class TestClassE {
public static void main(String[] args) {
IC ic = new ClassE();
System.out.println(ic instanceof IA);
System.out.println(ic instanceof IB);
System.out.println(ic instanceof IC);
System.out.println(ic instanceof ID);
System.out.println(ic instanceof ClassE);
}
}
答:
true
true
true
true
true
- 有如下代码:
interface IA{
void ma();
}
interface IB{
void mb();
}
class MySuper implements IA{
public void ma() {}
}
class MySub extends MySuper implements IB{
public void mb() {}
}
public class TestMain {
public static void main(String[] args) {
MySuper ms = new MySub();
System.out.println(ms instanceof IA);
System.out.println(ms instanceof IB);
System.out.println(ms instanceof MySuper);
System.out.println(ms instanceof MySub);
}
}
问:该程序输出结果是什么?
参考答案:
true
true
true
true
-
关于接口和抽象类,下列说法正确的是:
A . 抽象类可以有构造方法,接口没有构造方法
B . 抽象类可以有属性,接口没有属性
C . 抽象类可以有非抽象方法,接口中都是抽象方法
D . 抽象类和接口都不能创建对象
E . 一个类最多可以继承一个抽象类,但可以实现多个接口
答:ABCDE -
写出下面代码的输出结果
interface Light{
void shine();
}
class Redlight implements Light{
public void shine() {
System.out.println("Red Light shine in Red");
}
}
class Yellowlight implements Light{
public void shine() {
System.out.println("Yellow Light shine in Yellow");
}
}
class Greenlight implements Light{
public void shine() {
System.out.println("Green Light shine in Green");
}
}
class Lamp{
private Light light;
public void setLight(Light light){
this.light = light;
}
public void on(){
light.shine();
}
}
public class TestLamp {
public static void main(String[] args) {
Light[] Is = new Light[3];
Is[0] = new Redlight();
Is[1] = new Yellowlight();
Is[2] = new Greenlight();
Lamp lamp = new Lamp();
for(int i = 0 ; i < Is.length ; i++){
lamp.setLight(Is[i]);
lamp.on();
}
}
}
答:
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green
- 写出下面代码的执行的结果
interface JavaTeacher{
void teach();
}
class TeacherA implements JavaTeacher{
public void teach() {
System.out.println("TeacherA teach Java");
}
}
class TeacherB implements JavaTeacher{
public void teach() {
System.out.println("TeacherB teach Java");
}
}
class School{
public static JavaTeacher getTeacher(int i){
if(i==0)
return new TeacherA();
else
return new TeacherB();
}
}
public class TestSchool {
public static void main(String[] args) {
JavaTeacher jt = School.getTeacher(0);
jt.teach();
jt = School.getTeacher(10);
jt.teach();
}
}
答:
TeacherA teach Java
TeacherB teach Java
- 代码填空
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 Bones");
}
public void play() {
System.out.println("play with Cat");
}
}
class Wolf extends Animal{
public void eat() {
System.out.println("Wolf eat meat");
}
}
public class TestMain {
public static void main(String[] args) {
Animal as[] = new Animal[3];
as[0] = new Dog();
as[1] = new Cat();
as[2] = new Wolf();
//调用as数组中所有动物的eat方法
//1
//调用as数组中所有动物的play方法
//2
}
}
//1处应该填写的代码_____________
//2处应该填写的代码_____________
答:
1处的代码:
for(int i = 0; i < as.length ; i++){
as[i].eat();
}
2处的代码:
for(int i = 0; i < as.length ; i++){
if(as[i] instanceof Pet)
((Pet) as[i]).play();
}
- 在原有的Chap6中的17题基础上修改代码
公司给SalariedEmployee每月另外发放2000元加班费,给BasePlusSalesEmployee发放1000元加班费,改写原有代码,加入逻辑。并写一个方法,打印出本月公司总共发放了多少加班费。
答:
package Q8.T11;
interface OverTimeFire{
public static final double SALARIED_EMPLOYEE = 2000.0;
public static final double BASE_PLUS_SALAS_EMPLOYEE = 1000.0;
}
public class TestEmployee implements OverTimeFire {
public static void main(String[] args) {
Employee[] es = new Employee[4];
es[0] = new SalariedEmployee("John", 5, 5000);
es[1] = new HourlyEmployee("Tom", 10, 25, 170);
es[2] = new SalesEmployee("Lucy", 7, 200000, 0.03);
es[3] = new BasePlusSalesEmployee("James", 8, 1000000, 0.02, 5000);
for(int i = 0; i<es.length; i++){
System.out.println(es[i].getSalary(5));
}
double sum = 0;
for(int i = 0; i<es.length; i++){
if(es[i] instanceof SalariedEmployee)
sum = sum + SALARIED_EMPLOYEE;
if(es[i] instanceof BasePlusSalesEmployee)
sum = sum + BASE_PLUS_SALAS_EMPLOYEE;
}
System.out.println("本月总共加班费:"+sum);
}
}
class Employee{
private String name;
private int birthMonth;
public Employee(String name,int birthMonth){
this.name=name;
this.birthMonth=birthMonth;
}
public String getName(){
return name;
}
public double getSalary(int month){
if (this.birthMonth==month) return 100;
else return 0;
}
}
class SalariedEmployee extends Employee implements OverTimeFire{
private double salary;
public SalariedEmployee(String name,int birthMonth,double salary){
//把name,birthMonth两个参数传给父类,设置父类属性
super(name,birthMonth);
this.salary=salary;
}
public double getSalary(int month){
//调用父类的getSalary方法(判断是否生日),并加上月工资
return salary+super.getSalary(month) + SALARIED_EMPLOYEE ;
}
}
class HourlyEmployee extends Employee{
private double salaryPerHour;
private int hours;
public HourlyEmployee(String name, int birthMonth, double salaryPerHour, int hours) {
super(name, birthMonth);
this.salaryPerHour = salaryPerHour;
this.hours = hours;
}
public double getSalary(int month){
double result=0;
if (hours>160) result=160*this.salaryPerHour+(hours-160)*this.salaryPerHour*1.5;
else result=this.hours*this.salaryPerHour;
return result+super.getSalary(month);
}
}
class SalesEmployee extends Employee{
private double sales;
private double rate;
public SalesEmployee(String name, int birthMonth, double sales, double rate) {
super(name, birthMonth);
this.sales = sales;
this.rate = rate;
}
public double getSalary(int month) {
return this.sales*this.rate+super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee implements OverTimeFire{
private double basedSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double sales, double rate, double basedSalary) {
super(name, birthMonth, sales, rate);
this.basedSalary = basedSalary;
}
public double getSalary(int month) {
return this.basedSalary+super.getSalary(month) + BASE_PLUS_SALAS_EMPLOYEE;
}
}
- 有下列代码:
interface ServiceInterface{
void doService1();
void doService2();
void doService3();
}
abstract class AbstractService implements ServiceInterface{
public void doService1(){}
public void doService2(){}
public void doService3(){}
}
需要一个实现ServiceInterface接口的类MyService。
(1)第一种方式可以让MyService实现ServiceInterface接口,即:
class MyService implements ServiceInterface
(2)第二种方式可以让MyService实现AbstractService类,即:
class MyService extends ServiceInterface
请问:这两种方式有什么区别?AbstractService类有什么作用?
答:
第一种情况需要重写接口内所有的抽象方法
第二种情况不需要重写抽象方法
- 验证哥德巴赫猜想
输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和。
如 10=3+7 12=5+7
要求:两个人一组合作完成。一个人负责把一个整数n拆分分成两个整数的和,另一个人负责写一个函数,判断某一个整数a是否质数。
答
import java.util.Scanner;
interface MathTool{
boolean isPrime(int n);
}
//接口实现者
class MathToolImpl implements MathTool{
public boolean isPrime(int n) {
for(int i = 2; i<= Math.sqrt(n); i++){
if (n % i == 0) return false;
}
return true;
}
}
//接口的使用者
public class TestGoldBach {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
MathTool mt = new MathToolImpl();
for(int i = 2; i<=n/2; i++){
if (mt.isPrime(i) && mt.isPrime(n - i)){
System.out.println(n + "=" + i + "+" + (n - i));
}
}
}
}
打卡时间
There is often only one reason for your confusion, which is that you think too much and do too little at an age when you should be working hard.
——迷茫的原因往往只有一个,那就是在本该拼命去努力的年纪,想得太多,做得太少。