3 抽象类和接口
3.1 抽象类
3.1.1 抽象类是什么
[图片上传失败...(image-fea46a-1593175324581)]类和类之间具有共同的特征进一步抽象成了一个类。不能创建实例对象。
抽象类也属语应用数据类型。
3.1.2 如何定义
[修饰符] abstract class 类名{}
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n23" mdtype="fences">public abstract public Person{
}</pre>
3.1.3 注意
抽象类不能被实例化
abstract 和 final 不能同时修饰类
抽象类有构造方法,子类可以调用super()
抽象类中不一定有抽象方法,但是抽象方法必须出现在抽象类中。
非抽象类继承抽象类必须实现抽象方法。
抽象方法 没有实现的方法,需要子类重写。
3.1.3 使用抽象类的好处
面向抽象编程:可以降低程序的耦合度。
OCP 原则: Open close principle 对扩展开放,对修改关闭
3.2 接口
3.2.1 基础语法
也是一种引用数据类型
完全抽象。(接口是特殊的抽象类)
-
定义
- [修饰符列表] interface 接口名{}
-
接口支持多继承
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;" spellcheck="false" cid="n79" mdtype="fences">public interface A extend B,C{
}</pre> -
接口中只有抽象方法和常量。
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;" spellcheck="false" cid="n86" mdtype="fences">public interface A{
public abstract void sum();
}</pre>其中 修饰符可以省略,默认public abstract 和 public static final
抽象方法不能带有方法体。
3.2.2 普通类实现接口
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n151" mdtype="fences">interface DoSome{
int sum(int a , int b );
int sub(int a , int b );
}
class DoSomeImpl implements{
public int sum(int a, int b ){
return a+b;
}
public int sum(int a, int b){
return a-b;
}
}</pre>
普通类要实现所有接口中的所有方法。
3.2.3 接口实现多态
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n161" mdtype="fences">class DoSomeImpl2 implements DoSome{
....
}
public class MainClass{
public static void main(String[] args){
DoSome dosome = new DoSomeImpl();
dosome = new DoSomeImpl2();
}
}</pre>
3.2.4 一个类实现多个接口
java 只支持类的单继承,和c++有区别
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n155" mdtype="fences">class DoSomeImpl implements DoSome,DoSome2{
....
}</pre>
3.2.5 父类和接口可以同时存在
extend 在前, implements 在后
3.2.6 接口在开发中的作用
多态,面向抽象编程,不面向具体编程,降低耦合度,对拓展开放,对修改关闭
面向抽象编程(面向接口编程):接口可插拔。
3.2.7 使用接口的一个例子
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n187" mdtype="fences">public interface FoodMenu {
void soup();
void beef();
}
</pre>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n185" mdtype="fences">import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class ChineseCooker implements FoodMenu {
@Override
public void soup() {
System.out.println("中餐师傅做的汤");
}
@Override
public void beef() {
System.out.println("中餐师傅做的牛肉");
}
}
</pre>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n191" mdtype="fences">public class AmericanCooker implements FoodMenu {
@Override
public void soup() {
System.out.println("西方汤");
}
@Override
public void beef() {
System.out.println("西方牛肉");
}
}
</pre>
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n193" mdtype="fences">public class Customer {
private FoodMenu foodMenu;
public Customer(FoodMenu foodMenu) {
this.foodMenu = foodMenu;
}
public FoodMenu getFoodMenu() {
return foodMenu;
}
public void setFoodMenu(FoodMenu foodMenu) {
this.foodMenu = foodMenu;
}
public void order(){
foodMenu.soup();
foodMenu.beef();
}
public static void main(String[] args) {
// Customer customer = new Customer(new ChineseCooker());
// customer.order();
Customer customer = new Customer(new AmericanCooker());
customer.order();
}
}
</pre>
3.3 is-a, has-a,like-a
is-a 继承
has-a 类的组合,属性的方式
like-a 类实现接口
3.4 抽象类和接口的区别
接口是完全抽象,抽象类是半抽象
抽象类有构造方法,接口没有
接口可以多继承,抽象类只能单继承
类可以实现多个接口,但是只能继承一个抽象类
接口中只能出现常量和抽象方法