1. Interface
Package Com.yfs.javase;public interface IDemo1 {//interface interface public/*abstract*/void Method1 ();//default abstract method public} Class Child1 implements IDemo1 {@Overridepublic void method1 () { //Implement Interface method System.out.println ("Call Method1 ()");}}
2. Interface Example 1
Package Com.yfs.javase;public interface IFly {public void fly ();
3. Implementing the interface
Package Com.yfs.javase;public class Bird implements IFly {@Overridepublic void Fly () {System.out.println ("Bird in flight ....");}} Class Wuya extends Bird {@Overridepublic void Fly () {System.out.println ("Raven flying ....");}} Class Butterfly implements IFly {@Overridepublic void Fly () {System.out.println ("butterfly in flight .....");}}
4.IRun interface
Package Com.yfs.javase;public interface Irun {int a = 10;//default public static finalpublic void Run ();} /*public*/interface Iswim {}class Horse implements Irun {@Overridepublic void run () {System.out.println ("Horse running ..." + a);}}
5. Interface Multiple Inheritance
The package com.yfs.javase;//interface can inherit more public interface ITest extends IFly, Irun, Iswim {}
6.Person
Package Com.yfs.javase;public class Person {private String name;private int age;private char sex;public person () {}public Person (String name) {this.name = name;} Public person (String name, int age, char sex) {this.name = Name;this.age = Age;this.sex = sex;} public void Introduce () {System.out.println ("I am ..."); Public String toString () {return ' name: ' + name + ' Age: ' + ages + ' Gender: ' + sex;} public void Speak () {System.out.println (name +) did you work? ");} public void Sleep () {System.out.println (name +) did you sleep? ");} public void Eat () {System.out.println (name +) did you eat it? ");} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Char Getsex () {return sex;} public void Setsex (char sex) {this.sex = sex;}}
7. Superman heir, implement Irun,ifly interface
Package Com.yfs.javase;public Class Superman extends person implements Irun, IFly {@Overridepublic void run () {System.out. println ("Superman is running ...."); @Overridepublic void Fly () {System.out.println ("Superman is flying ...");}}
8. Interface Testing
Package Com.yfs.javase;public class Test1 {public static void main (string[] args) {IFly f1 = null;//new IFly ();//interface cannot be instantiated F1 = new Bird (); F1.fly (); f1 = new Butterfly (); F1.fly (); Callmethod (new Wuya ()); Irun r1 = new Horse (); R1.run (); Callmethod (new Superman ());} public static void Callmethod (IFly f) {f.fly ();}}
9. Test 2
Package Com.yfs.javase;public class Test2 {public static void main (string[] args) {Superman S1 = new Superman (); s1.setname ("Tom"); S1.speak (); S1.run (); Callmethod (New Horse ()); Callmethod (s1);} public static void Callmethod (Irun r) {R.run ();}}
Java Novice Note 21 interface