接口的学习笔记

接口的学习笔记

什么是接口?

概念:接口相当于特殊的抽象类,定义方式丶组成部分与抽象类的类似.interface关键字定义接口.
微观概念:接口是一种能力和约定.
1.接口的定义:代表了某种能力.
2.方法的定义:能力的具体要求.
interface A{
void a();
}

接口有什么特点:

1.没有构造方法,不能创建对象.
2.只能定义:公开静态常量丶公开抽象方法.
3.java为单继承,当父类的方法种类无法满足子类需求时,可实现接口扩充子类能力
4.接口支持多实现,可为类扩展多种能力
案例演示:自定义接口
public interface MyInterface{
//公开的静态常量
//public static final String NAME="接口1";
String NAME="接口1";
//公开的抽象方法
//public abstract void method1();
void method;
}

实现类:
public class Imp1 implements MyInterface{
public void method(){
System.out.println("method");
}
}

测试类:
public class Test{
public static void main(String[] args){
//new MyInterface1():
MyInterface1 myInterface1=new Imp1();
myInterface1.method;
}
}
案例演示:实现类实现多个接口.
interface Flyable{
void fly();
}
interface Firable{
void fire();
}
class Person implements Flyable,Fireable{
String name;
int age;
public Person(){
}
public Person(String name,int age){
super();
this.name=name;
this.age=age;

}
public void fly(){
System.out.println(name+"开始飞了...");
}
public void fire(){
System.out.println(name+"可以喷火了...");
}
}
public class Test{
public static void main(String[] args){
Person person = new Person("小明",20);
person.fly();
person.fire();
}
}

接口和抽象类的区别?`

相同点:
1.可编译成字节码文件.
2.不能创建对象
3.可以作为引用类型.
4.具备Object类中所定义的方法.
不同点:
1.所有属性都是公开静态常量,隐式使用public static final修饰.
2.所有方法都是公开抽象方法,隐式使用public abstract修饰.
3.没有构造方法丶动态代码块丶静态代码块.

接口和抽象类还有一个很大的区别就是抽象类可以提供某些方法的部分实现,但是接口却不可以(就是interface中只能定义方法,而不能有方法的实现,而在abstract class中则可以既有方法的具体实现,又有没有具体实现的抽象方法),这是抽象类的一个优点,只要像里面加入一个新的具体方法,其子类就一下子可以拥有这个方法,但是接口却不行,像接口里面加入一个新方法,所有实现这个接口的类就无法通过编译了.

接口实现多态
接口实现多态:使用接口作为方法的参数和返回值,实际赋值实现类对象实现多态.

//案例演示:
interface Runnable{
//跑
void run();
}
interface Swimable{
//游泳
void swim();
}
abstract class Animal{
public void eat(){
System.out.println("吃");
}
}
class Dog extends Animal implements Runnable,Swimable{
public void run(){
System.out.println("狗狗跑步");
}
public void swim(){
System.out.println("狗狗游泳");
}
}
public class Test{
public static void main(String[] args){
Dog d = new Dog();
Animal a = new Dog;
Runnable runnable = new Dog;
Swimable swimable = new Dog;
a.eat();
runnable.run();
swimable.swim();
}
}

接口常见关系

类与类:
1.单继承
2.extends父亲名称
类与接口:
1.多实现
2.implements接口名称1,接口名称2,接口名称n
接口与接口
1.多继承
2.extends 父接口1,父接口2,父接口n

常量接口
将多个常用于表示状态或固定值的变量,以静态常量的形式定义在接口中统一管理,提高代码可读性。

interface ConstInterface{
String CONST1 = "aaa";
String CONST2 = "bbb";
String CONST3 = "ccc";
}
public class TestConstInterface{
public static void main(String[] args){
if(ConstInterface.CONST1.equala("aaa")){
System.out.println();
}
}
}

回调原理
接口回调:先有接口的使用者,后有接口的实现者。

//案例演示
interface InkBox {
void inkbox();
}
class Printer {
public void inkbox(InkBox inkbox) {
inkbox.inkbox();
	}
}
class Blackink implements InkBox{
public void inkbox() {
System.out.println("黑白墨盒");
		
	}

}
class Colorink implements InkBox {
public void inkbox() {
System.out.println("彩色墨盒");
		
	}

}
public class Test {
public static void main(String[] args) {
Printer printer = new Printer();
printer.inkbox(new Blackink());
printer.inkbox(new Colorink());
	}

}

接口好处

1.程序的耦合度降低。
2.更自然的使用多态。
3.设计与实现完全分离。
4.更容易搭建程序框架。
5更容易更换具体实现。
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值