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;
- }
- }