接口是为了实现多态。
接口是为了实现多态。
接口是为了实现多态。
先讲一下接口回调。
interface People{
void peopleList();
}
class Student implements People{
public void peopleList(){
System.out.println("I’m a student.");
}
}
class Teacher implements People{
public void peopleList(){
System.out.println("I’m a teacher.");
}
}
public class Example{
public static void main(String args[]){
People a; //声明接口变量
a=new Student(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回调
a=new Teacher(); //实例化,接口变量中存放对象的引用
a.peopleList(); //接口回调
}
}
结果:
I’m a student.
I’m a teacher.
例子参考:https://zhidao.baidu.com/question/1923748969384870227.html
今天在公司里面写一个接口及其实现类的时候,
遇到一个现象引起了我的思考。
我做的一个后台管理里面的查询功能,
在controller层对接口的方法进行实现的时候,
我采用的是接口的引用对象来实现。
为什么不采用实现类创建引用对象来调用方法实现呢?按照正常的逻辑,
接口里面只定义了方法名称。
主要的实现还是在继承接口的实现类里面,
而我看了别人的代码,
他们也大部分都是创建接口对象来实现,
重写的方法体是在实现类里面,接口对象为什么就可以使用。
例如:
List list = new ArrayList();
//List是接口,ArrayList是它的实现类
原因:这就是JAVA多态的特点。
父类引用指向子类对象,
父类(接口)的引用就能够直接调用子类(实现类)的方法。
为什么在实际的开发过程中不采用直接创建实现类对象来调用方法呢?
我在知乎上看到这一篇文章,觉得非常收益,
这么做的主要目的还是为了能够减少开发过程中代码的修改量。
快速理解java多态的含义
自己把这篇文章归纳总结如下:
定义一个animal抽象类, 里面有两个方法。
接下里定义两个类(cat和dog)去继承animal,
cat和dog分别重写了animal中的方法。我们在调用dog里面的方法之后,
想修改为调用cat的方法。 ----
只需要将Animal animal=new Dog()改成Animal
animal=new Catl(), 只改变了一个对象。
接着继续调用animal.sing(),animal.run()。
实际的开发过程中要维护大量的代码量,
如果要换一个对象,改的代码更少。
refer
知乎 Java 中的接口有什么作用?
博客园 深入理解Java的接口和抽象类
//implA 为接口 ClassB为其实现类
implA A=new ClassB();//接口类型的引用变量A 去接收对象地址
or
ClassB A=new ClassB();//类类型的引用变量A 去接收对象地址
interface A { //接口A
//接口的方法声明必须是 public abstract ,即便不写默认也是
public void fun();
}
public class B implements A {
@Override
public void fun() {
//your coding
}
}
如果我们要使用B类对象,调用B类方法,我们很容易就会习惯的写出
A demo=new B();
用接口类型的引用变量demo,
去接收实现类B实例化出来的对象地址(这里的=是传递的地址)。
为什么不是B demo=new B();
呢,这样也不会有问题啊?
(当然A demo=newA();
是不可能的,因为接口是不能用来实例化对象的,
但可以用来声明一个接口类型的引用变量)。
先把结论丢出来吧
应该优先使用接口而不是类来引用对象,但只有存在适当的接口类型时
public class InterfaceTest {
public static void main(String[] args) {
PetInterface p = new Cat();
p.talk();
p.batheSelf();//无法调用 ,报错The method batheSelf() is undefined for the type PetInterface
}
}
interface PetInterface {
public void talk();
}
class Dog implements PetInterface {
@Override
public void talk() {
System.out.println("Bark!");
}
}
class Cat implements PetInterface {
@Override
public void talk() {
System.out.println("Meow!");
}
public void batheSelf() {
System.out.println("Cat bathing");
}
}
我们看到,
方法batheSelf()仅仅存在实现类中时,
若我们仍然使用接口来引用对象时
PetInterface p = newCat(),
那些仅仅存在实现类中的方法,
是无法直接调用的即p.batheSelf()无法调用会报错。
所以这时使用Cat p = new Cat()即类来引用是更好的。
也就是说,
使用接口类去引用对象是有前提条件的
——即实现类中全是接口类的方法的实现,没有自己单独的方法。
当实现类存在自己的方法时,使用实现类来声明变量。
【转型问题】
public static void main(String[] args) {
PetInterface p = new Cat();//向上转型 Cat->PetInterface
p.talk();
p.batheSelf();//无法调用 ,报错The method batheSelf() is undefined for the type PetInterface
}
我们说直接p.batheSelf()会报错,
这是因为向上转型的过程中Cat->PetInterface ,
对PetInterface 接口造成的唯一效应就是函数的“遗失”而非”获得”
(即遗失了实现类自己独有的函数方法batheSelf()),
而Cat向上转型至PetInterface可能会窄化其功能,
但无论如何不会窄于PetInterface接口功能。
当然也存在向下转型,
//p.batheSelf();替换为下面形式
((Cat)p).batheSelf();//向下转型,可正常调用执行