您的位置:首页 > 编程语言 > Java开发

java中的接口和抽象类

2016-04-10 15:45 375 查看
最近看了一篇介绍接口和抽象类的文章,发现还不错。以下是对文章的总结和自己写的一个事例。总结的可能有些粗糙,建议直接看原文,文章地址:http://www.jiagoushuo.com/article/1000074.html#rd?sukey=ecafc0a7cc4a741b8d173e3714b0d37aed2569a28a62b5eb357b1573f19c80eef74baa1c870e39b9068f6b8bb5f6709c

抽象类和普通类的区别



抽象类和接口的区别



事例

根据自己的理解写的一个实例。
现在有个鸟类,鸟的属性有种类,颜色,重量等,鸟的行为有吃,飞。但是有的鸟类只会奔跑不会飞(鸵鸟)。这时如果我们在一个Birds类中去定义飞和奔跑的行为,分别让只会飞的鸟和只会奔跑的鸟都去继承这个类,好像有些不太妥当。我们可以这样解决:定义一个抽象类,属性包括种类、颜色、重量,行为包括吃。在定义一个Fly接口,包含一个方法fly()。在定义一个Run接口,包含一个run()方法。如果只是会飞的鸟我们就继承Birds类,实现Fly接口。如果只是会奔跑的鸟我们就实现Birds类,实现Run接口。如果继承Birds类,实现了Fly和Run接口,那就是又会飞又会奔跑的鸟。实现代码如下:

package com.zxy.Test20160410;

public abstract class Birds {

private String type;
private String color;
private double weight;

public Birds(String type, String color, double weight){
this.setType(type);
this.setColor(color);
this.setWeight(weight);
}

public abstract void eat();

public void print(){
System.out.println("种类:"+this.getType());
System.out.println("颜色:"+this.getColor());
System.out.println("重量:"+this.getWeight());
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}
}


package com.zxy.Test20160410;

public interface Fly {
void fly();
}

package com.zxy.Test20160410;

public interface Run {
void run();
}


package com.zxy.Test20160410;

/**
* 蜂鸟
* @author hsoft3
*
*/
public class HummingBird extends Birds implements Fly {

public HummingBird(String type, String color, double weight){
super(type, color, weight);
}

@Override
public void fly() {
System.out.println("我是"+this.getType()+",我会fly。");
}

@Override
public void eat() {
System.out.print("我是"+this.getType()+",我吃花蜜。");
}
}

package com.zxy.Test20160410;

/**
* 鸵鸟
* @author hsoft3
*
*/
public class OstrichBirds extends Birds implements Run {

public OstrichBirds(String type, String color, double weight) {
super(type, color, weight);
}

@Override
public void run() {
System.out.println("我是"+this.getType()+",我会奔跑。");
}

@Override
public void eat() {
System.out.println("我是"+this.getType()+",我吃草。");
}

}


package com.zxy.Test20160410;

public class Test {

public static void main(String [] args){
//蜂鸟
Birds hummingBirds = new HummingBird("蜂鸟", "彩虹色", 2.3);
hummingBirds.print();
hummingBirds.eat();
Fly f = (Fly) hummingBirds;
f.fly();

//鸵鸟
OstrichBirds ostrichBirds = new OstrichBirds("鸵鸟", "灰色", 200.2);
ostrichBirds.print();
ostrichBirds.eat();
ostrichBirds.run();

System.out.println("是否会飞=>"+(hummingBirds instanceof Fly));
}
}


上例可以看见将HummingBird的实例赋给了Birds类型的变量hummingBirds,这样使用hummingBirds是调用不到fly(),需要进行强制转换。其实我们也可以创建一个FlyBirds抽象类,这类继承了Birds类,实现了Fly接口,然后HummingBird在继承FlyBird类,这样就可不需要强制类型转换就可以调用fly()方法。其实我想说的是:实际编程的情况要比这个示例复杂得多,我们需要根据自己的情况和需求,灵活运用,而不是生搬硬套。像面向对象、设计模式这类的东西需要在理解的基础上,在结合自己的情况,灵活运用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息