Java面向对象笔记 —— 接口

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dyw_666666/article/details/82808480

// ===笔记部分=== //

/*

类:是某一类事物的抽象,是某类对象的蓝图.

接口:事物或动物 功能的抽象

接口的继承

*/

// ===代码部分=== //

//定义了Animal接口
interface Animal {
	void breathe();
	void run();
}

//定义了LandAnimal接口,并继承了Animal接口
interface LandAnimal extends Animal {  //接口继承接口
	void liveOnland();  //定义抽象方法liveOnland()
}

//Dog类实现了Animal接口
class Dog implements Animal {
        public void breathe() {
	    System.out.println("狗在呼吸");
        }
	
	public void run() {
	    System.out.println("狗在跑");
	}
	
	public void liveOnland() {
	    System.out.println("狗在陆地上生活");
	}
}

//定义测试类
public class Test {
	public static void main(String[] args) {
	    Dog dog=new Dog();
	    dog.breathe();
	    dog.run();
	    dog.liveOnland();
	}
}

// ===笔记部分=== //

/*

多接口继承

我们把每个类中的这种实现的功能拆出来

分析:如何造一个生物实现了eat() + run() + cry() + think()

*/

// ===代码部分=== //

package temp;

interface animal {
	public void eat();
}

interface monkey {
	public void run();
	public void cry();
}

interface wisdom {
	public void think();
}

interface bird {
	public void fly();
}

class Human implements animal,monkey,wisdom {
	public void eat() {
		System.out.println("吃");
	}
	
	public void run() {
		System.out.println("跑");
	}
	
	public void cry() {
		System.out.println("哭");
	}
	
	public void think() {
		System.out.println("思考");
	}
}

//定义测试类
public class Test {
	public static void main(String[] args) {
		Human human=new Human();
		human.eat();
		human.run();
		human.cry();
		human.think();
	}
}

猜你喜欢

转载自blog.csdn.net/dyw_666666/article/details/82808480