没有不进步的人生,只有不进取的人 !
今天我学习了JAVA课程的 部分关于接口的知识。
中国加油!!!武汉加油!!!千锋加油!!!我自己加油!!!
接口是一种标准
将多个常用于表示状态和固定值的变量,以形态常量的形式定义在接口中统一管理,提高代码的可读性。
模块与模块之间的关联程度越松散,耦合越低,越好。
C8.3 有如下代码:
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是抽象的,则需要实现哪些方法?
ma,mb,mc,md
II. 把下面的代码补充完整:
public class TestClassE{
public static void main(String[] args){
IC ic=new ClassE();
//调用 ma 方法
_____________
//调用 mb 方法
_____________
//调用 mc 方法
_____________
//调用 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
C8.4 有如下代码:
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
C8.5 关于接口和抽象类,下列说法正确的是:ACDE
A. 抽象类可以有构造方法,接口没有构造方法
B. 抽象类可以有属性,接口没有属性
C. 抽象类可以有非抽象方法,接口中都是抽象方法
D. 抽象类和接口都不能创建对象
E. 一个类最多可以继承一个抽象类,但是可以实现多个接口
C8.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 TestLemp{
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
Red Light shine in Red
Yellow Light shine in Yellow
Green Light shine in Green
C8.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
本题的重点在于利用接口,把多态用在返回值类型上。getTeacher方法返回的对象是JavaTeacher接口类型,在getTeacher方法内部创建JavaTeacher实现类的对象,并作为返回值返回。
C8.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 方法
for(int i=0;i
C8.9 在原有的C6.17基础之上修改代码:
package tset17;
public class TestEmployee {
public static void main(String[] args) {
Employee[] e=new Employee[4];
e[0]=new SalariedEmployee("普通员工",1,4000);
e[1]=new HourlyEmployee("小时工",2,100,161);
e[2]=new SalesEmployee("提成销售",3,40000,0.12);
e[3]=new BasePlusSalesEmployee("底薪销售",4,1000,0.09,2000);
System.out.println(e[3].getBaseSalary());
for(int i=0;i
package tset17;
public interface Overtime {
double getOverTimeSalary();
}
package tset17;
class SalariedEmployee extends Employee implements Overtime{
private double salary;
public SalariedEmployee(String name,int birthday,double salary){
super(name,birthday);
this.salary=salary;
}
public double getSalary(int month){
double money=salary+super.getSalary(month);
return money;
}
public double getOverTimeSalary(){
return 2000;
}
}
package tset17;
class BasePlusSalesEmployee extends SalesEmployee implements Overtime{
private double baseSalary;
public BasePlusSalesEmployee(String name, int birthday, double monthlySales, double royalth,double baseSalary) {
super(name, birthday, monthlySales, royalth);
this.baseSalary=baseSalary;
}
public double getSalay(int month){
double money=0;
money=super.getSalary(month)+this.baseSalary;
return money;
}
public double getOverTimeSalary(){
return 2000;
}
public double getBaseSalary(){
return baseSalary;
}
}
C8.10 有下列代码:
interface ServiceInterface{
void doService1();
void doService2();
void doService3();
}
abstract class AbstractService implements ServiceInterface{
public void duService1(){}
public void duService2(){}
public void duService3(){}
}
需要一个实现 ServiceInterface 接口的类 MyService
I. 第一种方式可以让 MyService 实现 ServiceInterface 接口,即:
class MyService implements ServiceInterface
II. 第二种方式可以让 MyService 继承 AbstractService 类,即:
class MyService extends AbstractService
这两种方式的区别是:II是从父类继承的接口,AbstractService起到承接的作用
C8.11 验证哥德巴赫猜想:
输入一个大于6的偶数,输出这个偶数能被分解为哪两个质数的和。
package TestGoldbach;
interface MathTool {
public boolean isPrime(int n);
}
package TestGoldbach;
//接口的实现
public class Impl implements MathTool{
public boolean isPrime(int n){
for(int i=2;i
package TestGoldbach;
//接口的使用
import java.util.Scanner;
public class TestSplit {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入大于6的偶数n:");
int n=input.nextInt();
MathTool t=new Impl();
if(n>6 && n%2==0){
for(int i=2;i