首页 > 大数据 > 数据分析 > java 接口,接口的特性,接口实现多态,面向接口编程
package cn.zy.cellphone;/**接口是一种引用数据类型。使用interface声明接口,形式
* 形式:public interface 接口名称{}
* 接口不能拥有构造方法,不能用于创建对象
*接口可以多继承。一个接口可以继承多个其他接口
*列如:public interface Broadcast extends Network,Picture{}
*Broadcast接口就具备了A、B接口中定义的抽象方法。 */public interface Broadcast { /**接口中可以声明属性。接口中定义的所有变量都是static final类型的。
* public static final String version = "1.0";
public int count = 10; // 默认是static final类型
一般很少在接口中声明属性。 */
package cn.zy.cellphone;public interface Picture {public void picture();//照相}
package cn.zy.cellphone;public interface Network {public void network();//网络}
package cn.zy.cellphone;/**一个类只能继承一个父类,同时实现多个接口。继承在前,实现在后。
* public class Photo2 extends Photo implements Network,Picture,Broadcast {}
* 实现类实现接口,必须实现接口中定义的抽象方法。
*方法即行为,表示一种功能,接口定义了一套功能,实现类必须实现这些功能 –> 实现类的能力得到拓展。 *///实现类(子类)public class Photo2 extends Photo implements Network,Picture,Broadcast { private String name;
public String getName() { return name;
} public void setName(String name) { this.name = name;
}
public Photo2(String brand, String type, String name) { super(brand, type); this.setName (name);
}
/**接口中定义的一些系列方法表示的是一种种的能力。接口让实现类实现这些能力,实现类的能力得到拓展和升级。
实现类根据自身特性实现接口中定义的方法。
* 特殊情况:如果一个抽象父类定义了和接口同名的抽象方法,实现类实现的是抽象父类的抽象方法。 */
public void broadcast() {
System.out.println(this.getName()+"的手机可以播放");
} public void picture() {
System.out.println(this.getName()+super.getBrand()+super.getType()+"的手机可以照相");
}
public void network() {
System.out.println(this.getName()+super.getBrand()+super.getType()+"的手机可以播放上网");
} /**实现类(子类)Photo2 手机原本只有下面几个功能,通过接口增加了上面几个功能
* 实现类实现接口,必须实现接口中定义的抽象方法。
*方法即行为,表示一种功能,接口定义了一套功能,实现类必须实现这些功能 –> 实现类的能力得到拓展。 */
public void sendInfo() {
System.out.println(super.getBrand()+super.getType()+"的手机发信息");
}
public void call() {
System.out.println(super.getBrand()+super.getType()+"的手机可以打电话");
}
public void info() {
System.out.println(super.getBrand()+super.getType()+"的手机可以发短信");
}
}
package
cn.zy.cellphone;
//
实现类(子类)
public
class
Photo1
extends
Photo
implements
Broadcast {
private
String name;
public
String
getName
() {
return
name;
}
public
void
setName
(String name) {
this.name =
name;
}
public
Photo1
() {
super
();
}
public
Photo1
(String brand, String type,String name) {
super
(brand,type);
this
.setName(name);
}
public
void
broadcast
() {
System.out.println(
this.getName()+
"的手机可以播放"
);
}
public
void
sendInfo
(){
System.out.println(
super.getBrand()+
super.getType()+
"的手机发信息"
);
}
public
void
call
(){
System.out.println(
super.getBrand()+
super.getType()+
"的手机可以打电话"
);
}
public
void
info
(){
System.out.println(
super.getBrand()+
super.getType()+
"的手机可以发短信"
);
}
}
package cn.zy.cellphone;//父类public abstract class Photo { private String brand; private String type; public String getBrand() { return brand;
} public void setBrand(String brand) { this.brand = brand;
} public String getType() { return type;
} public void setType(String type) { this.type = type;
} public Photo() { super();
} public Photo(String brand, String type) { super(); this.brand = brand; this.type = type;
} public abstract void sendInfo(); public abstract void call(); public abstract void info();
}
package
cn.zy.cellphone;
//
执行类
public
class
Test {
public
static
void
main
(String[] args) {
//
同一引用类型
Broadcast broadcast1=
new Photo1(
"诺基亚",
"a01",
"张三"
);
broadcast1.broadcast();
Photo photo=
new Photo1(
"诺基亚",
"a01",
"张三"
);
photo.sendInfo();
photo.call();
photo.info();
//
同一引用类型
Broadcast broadcast2=
new Photo1(
"苹果X",
"11S",
"李四"
);
broadcast2.broadcast();
//
实例不同, 对同一方法的执行结果不同
Picture picture1=
new Photo2(
"苹果X",
"11S",
"李四"
);
picture1.picture();
Network network1=
new Photo2(
"苹果X",
"11S",
"李四"
);
network1.network();
//
多态
Photo photo1=
new Photo1(
"苹果X",
"11S",
"李四"
);
photo1.sendInfo();
photo1.call();
photo1.info();
/**
* 接口实现多态: 接口类型 引用 实现类对象
继 承实现多态: 父类类型 引用 子类对象
接口定义的方法被实现类实现,通过接口引用实现类时,调用接口中的方法时,执行的是实现类实现的方法。
实现类对象具备接口中定义的能力 是一种has a 关系
子类对象是一种父类类型 是一种is a 关系
现在的Photo1同Photo2既是实现类也是子类
*/
}
}
接口 表示一种 约定 ( 协议 ), 约定 ( 规范 ) 了 实现类应该具备 (has a) 的 能力。
理解:
=> 实现 类必须实现接口中所有的方法,所以接口规范了实现类的行为
=> 接口 约定了实现类应该具备的行为。
面向 接口编程
所谓 面向接口编程,在 程序设计时,只关心 实现类具备 什么能力,而不关心实现类如何实现这个能力。面向 接口编程时,面向接口的约定而不考虑接口的具体实现。
也就是 说,面向接口编程时,接口的定义方只关系实现类是否具备接口 所定义的能力,而实现类如何被的该能力接口定义方 一点都不关心
//接口实现多态 电脑类(通过接口把3种零件组成电脑,而且零件随时可以更换)(cpu接口 4Hz 5Hz 。。。。(随时更换大小不同的cpu) )
(硬盘接口 3000GB 4000Gb(随时更换大小不同的硬盘))
(内存接口 30GB 40Gb(随时更换大小不同的内存))
package cn.computer;//电脑public class Computer { private Cpu cpu; // 预留cpu接口
private Caliche caliche; // 预留硬盘接口
private InternalStroage internalStroage; //预留内存接口public Cpu getCpu() { return cpu;
}public void setCpu(Cpu cpu) { this.cpu = cpu;
}public Caliche getCaliche() { return caliche;
}public void setCaliche(Caliche caliche) { this.caliche = caliche;
}public InternalStroage getInternalStroage() { return internalStroage;
}public void setInternalStroage(InternalStroage internalStroage) { this.internalStroage = internalStroage;
}public Computer() { super();
}public Computer(Cpu cpu, Caliche caliche, InternalStroage internalStroage) { super(); this.cpu = cpu;
this.caliche = caliche;
this.internalStroage = internalStroage;
} //把各个部件通过接口组装成一个完整的电脑public void print(String name) {
System.out.println(name+"计算机");
System.out.println("cpu:"+getCpu().Hz());
System.out.println("硬盘容量:"+getCaliche().big2());
System.out.println("内存容量:"+this.getInternalStroage().big());
}
}
package cn.computer;//cpu接口public interface Cpu { public String Hz(); //这是接口实现多态的形式: public 你要返回的变量类型 方法名()
}
package
cn.computer;
//
实现类
public
class
Realize1
implements
Cpu{
public
String
Hz
(){
//
public 你要返回的变量类型 方法名()
return
"3.8GHz";
//
返回值
}
}
package cn.computer;//内存接口public interface InternalStroage { public String big();
}
package
cn.computer;
//
实现类
public
class
Realize2
implements
InternalStroage{
public
String
big
(){
return
"4GB"
;
}
}
package cn.computer;//硬盘接口public interface Caliche { public String big2();
}
package
cn.computer;
//
实现类
public
class
Realize2
implements
InternalStroage{
public
String
big
(){
return
"4GB"
;
}
}
package
cn.computer;
public
class
Test {
public
static
void
main
(String[] args) {
Cpu cpu=
new Realize1();
//
把一种3.8Hz的cpu赋予给cpu接口
Caliche caliche=
new Realize3();
//
把3000GB的硬盘赋予给硬盘接口
InternalStroage internalStroage=
new Realize2();
//
把4GB的内存赋予给内存接口
Computer computer=
new Computer(cpu,caliche,internalStroage);
//
把cpu,硬盘,内存给电脑类组合(地址)
computer.print(
"华硕");
//
最后得到了完整的电脑
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70026910/viewspace-2946577/ ,如需转载,请注明出处,否则将追究法律责任。