總結
接口多態
多種類型的不同引用指向同一個對象是,表示看待對象的視角不同
不同類型所能看到對象的範圍不同,只能調用自身類型中所聲明的部分
類與類是單繼承關係
類與接口是多實現關係,一個類可實現多個接口
接口與接口多繼承關係 一個接口可繼承多個接口
常量接口
將多個常用於表示狀態或固定值的變量,以靜態常量的形式定義在傳統接口中統一管理,提高代碼可讀性。
接口回調原理
接口的好處
程序的耦合度降低
更自然的使用多態
設計與實現完全分離
更容易搭建程序框架
更容易更換具體實現
-----------------------------------------------------我是作業分割線
interface IA{ void ma();
}
interface IB extends IA{ void mb();
}
interface IC{ void mc();
}
interface ID extends IB, IC{ void md();
I.如果有一個類 ClassE 實現 ID 接口,如果不希望 ClassE 是抽象的,則需要實現哪些方法?
答案:需要實現public ma();public mb();public mc();public md(); 接口可以多繼承,類中要覆蓋所有方法。
II. 把下面的代碼補充完整
public class TestClassE{
public static void main(String args[]){
IC ic = new ClassE();
//調用 ma 方法
______________
//調用 mb 方法
______________
//調用 mc 方法
______________
//調用 md 方法
______________
}
}
答案:
(ClassE)ic.ma();
(ClassE)ic.mb();
ic.mc();
(ClassE)ic.md();
III. 寫出下面代碼的輸出結果
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
5.關於接口和抽象類,下列說法正確的是:
A. 抽象類可以有構造方法,接口沒有構造方法
B. 抽象類可以有屬性,接口沒有屬性
C. 抽象類可以有非抽象方法,接口中都是抽象方法
D. 抽象類和接口都不能創建對象
E. 一個類最多可以繼承一個抽象類,但是可以實現多個接口
答案:ACDE
解釋下A ,抽象類和接口都不能new對象,接口明確沒有構造方法,靜態代碼塊,動態代碼塊。但是抽象類可以有構造方法,雖然不能創建對象,但是可以用來初始化抽象類中的共性屬性,或被其他子類調用,且抽象類會被繼承,子類構造函數中默認第一行是super();,調用父類無參構造方法。所以即是我們不寫構造方法,抽象類也會默認提供一個無參構造方法,否則系統編譯不會通過。
6.寫出下面代碼的輸出結果
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[] ls = new Light[3];
ls[0] = new RedLight();
ls[1] = new YellowLight();
ls[2] = new GreenLight();
Lamp lamp = new Lamp();
for (int i = 0; i<ls.length; i++){
lamp.setLight(ls[i]);
lamp.on();
}
}
}
答案:
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green
7.寫出下面代碼執行的結果
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
8.代碼填空
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 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 處應該填入的代碼爲
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();
}
}
Animal中沒有paly方法,所以需要強轉爲子類對象再調用paly方法,並且要去確定類型轉換是否異常,因爲wolf類沒有pet接口。
9.在原有的 Chap6 中的 17 題基礎之上修改代碼
公司給 SalariedEmployee 每月另外發放 2000 元加班費,給 BasePlusSalesEmployee 發放 1000 元加班費改寫原有代碼,加入以上的邏輯。並寫一個方法,打印出本月公司總共發放了多少加班費。
package com.qf.day17.t1;
public class TestClassE {
public static void main(String[] args){
Employee[] s = new Employee[5];
s[0] = new SalarideEmployee("zhangsan",3,5000.0);
s[1] = new HourlyEmployee("lisi",2,200);
s[2] = new SalesEmployee("wangwu",6,30000);
s[3] = new BasePlusSalesEmployee("zhouliu",8,5000,35000);
s[4] = new SalesEmployee("zhaoqi",3,40000);
for(int i = 0; i <s.length;i++){
System.out.println(s[i].getName()+"的2月份工資爲"+s[i].getSalary());
}
OverTimeMoney.Money();
}
}
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 this.name;
}
public double getSalary(){
int thismonth = 2;
if(thismonth == this.month){
return 100.0;
}else{
return 0.0;
}
}
}
class OverTimeMoney{
public static void Money(){
System.out.println("本月發放的加班費共計"+(SalarideEmployee.count+BasePlusSalesEmployee.count));
}
}
class SalarideEmployee extends Employee implements OverTimePay{
public static double count;
private double monthSalary;
public SalarideEmployee(){}
public SalarideEmployee(String name,int month,double monthSalary){
super(name,month);
this.monthSalary = monthSalary;
}
public double getSalary(){
getOverTimePay();
return this.monthSalary+super.getSalary();
}
public void getOverTimePay() {
this.monthSalary += 2000.0;
SalarideEmployee.count+=2000.0;
}
}
class HourlyEmployee extends Employee{
private int hours;
private double hourSalary=20;
public HourlyEmployee(){}
public HourlyEmployee(String name,int month,int hours){
super(name,month);
this.hours = hours;
}
public double getSalary(){
if(hours <= 160){
return hours*this.hourSalary+super.getSalary();
}else{
return 160*this.hourSalary+(hours-160)*this.hourSalary*1.5+super.getSalary();
}
}
}
class SalesEmployee extends Employee{
private double monthSales;
private double royaltyRate = 0.2;
public SalesEmployee(){}
public SalesEmployee(String name,int month,double monthSales){
super(name,month);
this.monthSales = monthSales;
}
public double getSalary(){
return monthSales*royaltyRate+super.getSalary();
}
public void setMonthSales(double monthSales) {
this.monthSales = monthSales;
}
}
class BasePlusSalesEmployee extends SalesEmployee implements OverTimePay{
public static double count = 0;
private double basePlus;
public BasePlusSalesEmployee(){}
public BasePlusSalesEmployee(String name,int month,double basePlus,double monthSales){
super(name,month,monthSales);
this.basePlus = basePlus;
}
public void getOverTimePay(){
this.basePlus +=1000.0;
BasePlusSalesEmployee.count+=1000.0;
}
public double getSalary(){
getOverTimePay();
return basePlus+super.getSalary();
}
}
interface OverTimePay{
public abstract void getOverTimePay();
}
10.有下列代碼:
interface ServiceInterface{
void doService1();
void doService2();
void doService3();
}
abstract class AbstractService implements ServiceInterface{
public void doService1(){}
public void doService2(){}
public void doService3(){}
}
需要一個實現 ServiceInterface 接口的類 MyService。
I.第一種方式可以讓 MyService 實現 ServiceInterface 接口,即: class MyService implements ServiceInterface
AI.第二種方式可以讓 MyService 繼承 AbstractService 類,即:
class MyService extends AbstractService
請問:這兩種方式有什麼區別?AbstractService 類有什麼作用?
答:第一種方式是MyService類直接實現接口,第二種方式是通過繼承父類,實現父類中的方法來間接的實現接口。
AbstractService類作爲父類去實現MyService接口的方法,實現後的方法是會被子類繼承過來的。
11.驗證歌德巴赫猜想
輸入一個大於 6 的偶數,請輸出這個偶數能被分解爲哪兩個質數的和。
如 10=3+7 12=5+7
要求:兩個人一組合作完成。一個人負責把一個整數 n 拆分成兩個整數的和,另一個人負責寫一個函數,判斷某一個整數 a 是否是質數 。
package com.qf.day18.t2;
import java.util.Scanner;
public class Test11 {
public static void main(String[] args){
int a;
while(true){
System.out.println("請輸入一個大於6的偶數");
Scanner input = new Scanner(System.in);
a = input.nextInt();
if(a > 6 && a % 2 ==0){
break;
}else{
System.out.println("輸入有誤,請重新輸入");
}
}
MathTool mt = new MathToollmpl();
for(int i = 2;i < a/2;i++){
if(mt.isPrime(i)&&mt.isPrime(a-i)){
System.out.println(a+"="+i+"+"+(a-i));
}
}
}
}
interface MathTool{
boolean isPrime(int i );
}
class MathToollmpl implements MathTool{
public boolean isPrime(int i){
for(int n = 2;n <= i/2;n++){
if(i%n == 0){
return false;
}
}
return true;
}
}