好久不见哟,这一弹主要就是讲述Java里面的接口,这个内容是很重要的内容,这里是作为一个开始最初的讲述,在后面的多态仍然会继续讲述。这一弹的主要内容就是下面的三个方面:
(0)定义接口
(1)实现接口
(2)接口继承
具体的代码操作如下所示—>
package com.csdn;
public class Demo10 {
public static void main(String[] args) {
// Animal a = new Animal();
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.play();
Cat cat = new Cat();
cat.eat();
cat.sleep();
cat.play();
FlyFish ff = new FlyFish();
ff.fly();
ff.swim();
Mobile m = new Mobile();
m.play();
m.playVideo();
}
}
interface Player{
public void play();
}
interface VidoPlayer extends Player{
public void playVideo();
}
class Mobile implements VidoPlayer{
public void play() {
System.out.println("play...");
}
public void playVideo() {
System.out.println("play video...");
}
}
class FlyFish implements Bird,Fish{
public void swim() {
System.out.println("swim...");
}
public void fly() {
System.out.println("fly...");
}
}
interface Bird{
public void fly();
}
interface Fish{
public void swim();
}
//更好的实现程序的抽象过程
interface Animal{
public void eat();
public void sleep();
public void play();
}
class Dog implements Animal{
public void eat() {
System.out.println("dog eat...");
}
public void sleep() {
System.out.println("dog sleep...");
}
public void play() {
System.out.println("dog play...");
}
}
class Cat implements Animal{
public void eat() {
System.out.println("cat eat...");
}
public void sleep() {
System.out.println("cat sleep...");
}
public void play() {
System.out.println("cat play...");
}
}