abstract
修饰的类为抽象类 方法为抽象方法。
含有抽象方法,必被声明为抽象类。
抽象类不能实例化。
不能修饰私有方法、构造器、静态方法、final方法。
final
修饰的类不能被继承
修饰的方法不能被子类重写
标记的变量名称大写且只能被赋值一次。
接口
接口中所有方法均为公共抽象方法。pulic abstract
把共同行为特征定义在接口中。
只能被public修饰
接口中所有成员变量都是全局常量 public static final
interface是接口
implements是实现
示例代码:
父类接口
package ray;
public interface Flyer{
public abstract void takeoff();
public void fly();
void land();
}
子类实现接口
package ray;
//抽象类可以实现接口 也可以对接口中的抽象方法不予以理会
public abstract class Pet implements Flyer{
private String name;
private int age;
private double weight;
public Pet() {
}
public Pet(String name, int age, double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Pet [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}
public abstract void speak();
public abstract void eat();
}
子类继承了Pet中没被实现的接口 所以既要多态实现Pet方法也要间接实现Flyer接口
package ray;
public class Bird extends Pet {
private int flySpeed;
public Bird() {
}
public Bird(String name, int age, double weight, int flySpeed) {
super(name,age,weight);
this.flySpeed = flySpeed;
}
public int getFlySpeed() {
return flySpeed;
}
public void setFlySpeed(int flySpeed) {
this.flySpeed = flySpeed;
}
@Override
public void takeoff() {
System.out.println("翅膀一展就搞定");
}
@Override
public void fly() {
System.out.println("我喜欢自由");
}
@Override
public void land() {
System.out.println("随心所欲");
}
@Override
public void speak() {
System.out.println("吱吱吱");
}
@Override
public void eat() {
System.out.println("吃虫子");
}
@Override
public String toString() {
return super.toString() + ", 飞行速度:" + flySpeed;
}
}
测试
package ray;
public class Flyertest {
public static void main(String[] args) {
Flyer flyer = new Bird("小飞", 2, 0.2, 20);
flyer.takeoff();
flyer.fly();
flyer.land();
Pet pet = (Pet)flyer;//转型
pet.speak();
pet.eat();
}
}
结果:
翅膀一展就搞定
我喜欢自由
随心所欲
吱吱吱
吃虫子
变形
父类接口
package ray;
public interface Flyer{
public abstract void takeoff();
public void fly();
void land();
}
父类抽象
package ray;
public abstract class Pet {
private String name;
private int age;
private double weight;
public Pet() {
}
public Pet(String name, int age, double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Pet [name=" + name + ", age=" + age + ", weight=" + weight + "]";
}
public abstract void speak();
public abstract void eat();
}
子类
package ray;
public class Bird extends Pet implements Flyer{//两者都是父类 先继承在接口 用接口可以实现多继承
private int flySpeed;
public Bird() {
}
public Bird(String name, int age, double weight, int flySpeed) {
super(name,age,weight);
this.flySpeed = flySpeed;
}
public int getFlySpeed() {
return flySpeed;
}
public void setFlySpeed(int flySpeed) {
this.flySpeed = flySpeed;
}
@Override
public void takeoff() {
System.out.println("翅膀一展就搞定");
}
@Override
public void fly() {
System.out.println("我喜欢自由");
}
@Override
public void land() {
System.out.println("随心所欲");
}
@Override
public void speak() {
System.out.println("吱吱吱");
}
@Override
public void eat() {
System.out.println("吃虫子");
}
@Override
public String toString() {
return super.toString() + ", 飞行速度:" + flySpeed;
}
}
测试
package ray;
public class Flyertest {
public static void main(String[] args) {
Pet pet = new Bird("小飞", 2, 0.2, 20);
pet.speak();
pet.eat();
if(pet instanceof Flyer) {//判断左引用指向对象实体是否是右面类型的对象
Flyer flyer = (Flyer)pet;//pet与flyer虽然没联系 但依然能转型 指向的其实是小鸟
flyer.takeoff();
flyer.fly();
flyer.land();
System.out.println(flyer.toString());//flyer是接口类型没有toString方法,但flyer对象是object子类,
//可以直接输出flyer.toString()但不能调speak和eat
}
}
}
结果
吱吱吱
吃虫子
翅膀一展就搞定
我喜欢自由
随心所欲
Pet [name=小飞, age=2, weight=0.2], 飞行速度:20
继承比接口更亲密一些
接口可以继承接口用extends