extends与implements的区别:java
extends 是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,可是能够用接口来实现,这样就用到了implements,继承只能继承一个类,但implements能够实现多个接口,用逗号分开就好了。spa
好比: .net
class A extends B implements C,D,E {} (class 子类名 extends 父类名 implenments 接口名)blog
- public inerface Runner
- {
- int ID = 1;
- void run ();
- }
- interface Animal extends Runner
- {
- void breathe ();
- }
- class Fish implements Animal
- {
- public void run ()
- {
- System.out.println("fish is swimming");
- }
- public void breather()
- {
- System.out.println("fish is bubbing");
- }
- }
- abstract LandAnimal implements Animal
- {
-
- public void breather ()
- {
- System.out.println("LandAnimal is breathing");
- }
- }
- class Student extends Person implements Runner
- {
- ......
- public void run ()
- {
- System.out.println("the student is running");
- }
- ......
- }
-
- interface Flyer
- {
- void fly ();
- }
-
- class Bird implements Runner , Flyer
- {
- public void run ()
- {
- System.out.println("the bird is running");
- }
- public void fly ()
- {
- System.out.println("the bird is flying");
- }
- }
-
- class TestFish
- {
- public static void main (String args[])
- {
- Fish f = new Fish();
- int j = 0;
- j = Runner.ID;
- j = f.ID;
- }
- }