{"id":724,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java Interface\u5173\u952e\u5b57","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/724.html","title":{"rendered":"Java Interface\u5173\u952e\u5b57"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java Interface\u5173\u952e\u5b57<\/h1>\n<\/p><\/div>\n
\n Java\u4e2d\u7684
\n Interface<\/strong>\u662f\u7c7b\u7684\u84dd\u56fe\u3002\u5b83\u5177\u6709\u9759\u6001\u5e38\u91cf\u548c\u62bd\u8c61\u65b9\u6cd5\u3002\n <\/div>\n
\n Java\u4e2d\u7684\u63a5\u53e3\u662f\u5b9e\u73b0
\n \u62bd\u8c61 \u7684\u4e00\u79cd\u673a\u5236\u3002 Java\u63a5\u53e3\u4e2d\u53ea\u80fd\u6709\u62bd\u8c61\u65b9\u6cd5\uff0c\u800c\u65b9\u6cd5\u4e3b\u4f53\u5219\u4e0d\u80fd\u3002\u5b83\u7528\u4e8e\u5b9e\u73b0\u62bd\u8c61\u548cJava\u4e2d\u7684\u7ee7\u627f\u3002\n <\/div>\n
\n \u6362\u53e5\u8bdd\u8bf4\uff0c\u53ef\u4ee5\u8bf4\u63a5\u53e3\u53ef\u4ee5\u5177\u6709\u62bd\u8c61\u65b9\u6cd5\u548c\u53d8\u91cf\u3002\u5b83\u4e0d\u80fd\u5177\u6709\u65b9\u6cd5\u4e3b\u4f53\u3002\n <\/div>\n
\n Java\u63a5\u53e3\u4e5f
\n \u8868\u793aIS-A\u5173\u7cfb<\/strong>\u3002\n <\/div>\n
\n \u5b83\u4e0d\u80fd\u50cf\u62bd\u8c61\u7c7b\u4e00\u6837\u5b9e\u4f8b\u5316\u3002\n <\/div>\n
\n \u81eaJava 8\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a5\u53e3\u4e2d\u5177\u6709
\n \u9ed8\u8ba4\u548c\u9759\u6001\u65b9\u6cd5<\/strong>\u3002\n <\/div>\n
\n \u81eaJava 9\u5f00\u59cb\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a5\u53e3\u4e2d\u5177\u6709
\n \u79c1\u6709\u65b9\u6cd5<\/strong>\u3002\n <\/div>\n

\u4e3a\u4ec0\u4e48\u4f7f\u7528Java\u63a5\u53e3\uff1f<\/h2>\n
\n \u4f7f\u7528\u63a5\u53e3\u7684\u4e3b\u8981\u539f\u56e0\u4e3b\u8981\u6709\u4e09\u4e2a\u3002\u5b83\u4eec\u5728\u4e0b\u9762\u7ed9\u51fa\u3002\n <\/div>\n

\u5b83\u7528\u4e8e\u5b9e\u73b0\u62bd\u8c61\u3002<\/span>
\n \u901a\u8fc7\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u652f\u6301\u591a\u91cd\u7ee7\u627f\u7684\u529f\u80fd\u3002<\/span>
\n \u53ef\u7528\u4e8e\u5b9e\u73b0\u677e\u6563\u8026\u5408\u3002<\/span>
\n Java Interface\u5173\u952e\u5b57_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\u5982\u4f55\u58f0\u660e\u63a5\u53e3\uff1f<\/h2>\n
\n \u4f7f\u7528interface\u5173\u952e\u5b57\u58f0\u660e\u63a5\u53e3\u3002\u5b83\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u62bd\u8c61\uff1b\u8868\u793a\u63a5\u53e3\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u90fd\u7528\u7a7a\u4e3b\u4f53\u58f0\u660e\uff0c\u5e76\u4e14\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6240\u6709\u5b57\u6bb5\u5747\u4e3apublic\uff0cstatic\u548cfinal\u3002\u5b9e\u73b0\u63a5\u53e3\u7684\u7c7b\u5fc5\u987b\u5b9e\u73b0\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u6240\u6709\u65b9\u6cd5\u3002\n <\/div>\n

\u8bed\u6cd5: <\/h3>\n
\n
 interface <
interface_name>
{
    \/\/ declare constant fields
    \/\/ declare methods that abstract
\/\/ by default.}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java8 Interface\u6539\u8fdb<\/h2>\n
\n \u7531\u4e8eJava 8\uff0c\u63a5\u53e3\u53ef\u4ee5\u5177\u6709\u9ed8\u8ba4\u65b9\u6cd5\u548c\u9759\u6001\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u5bf9\u6b64\u8fdb\u884c\u8ba8\u8bba\n <\/div>\n

\u7f16\u8bd1\u5668\u5185\u90e8\u52a0\u6cd5<\/h2>\n

\n
\n \u6ce8\u610f: <\/span> Java\u7f16\u8bd1\u5668\u5728\u63a5\u53e3\u65b9\u6cd5\u4e4b\u524d\u6dfb\u52a0\u4e86\u516c\u5171\u5173\u952e\u5b57\u548c\u62bd\u8c61\u5173\u952e\u5b57\u3002\u800c\u4e14\uff0c\u5b83\u5728\u6570\u636e\u6210\u5458\u4e4b\u524d\u6dfb\u52a0\u4e86public\uff0cstatic\u548cfinal\u5173\u952e\u5b57\u3002\n <\/div>\n<\/h3>\n
\n \u6362\u53e5\u8bdd\u8bf4\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cInterface\u5b57\u6bb5\u662fpublic\uff0cstatic\u548cfinal\uff0c\u65b9\u6cd5\u662fpublic\u548cabstract\u3002\n <\/div>\n

Java Interface\u5173\u952e\u5b57_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c2\u5f20 <\/p>\n


\n

\u7c7b\u4e0e\u63a5\u53e3\u4e4b\u95f4\u7684\u5173\u7cfb<\/h3>\n
\n \u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4e00\u4e2a\u7c7b\u6269\u5c55\u4e86\u53e6\u4e00\u4e2a\u7c7b\uff0c\u4e00\u4e2a\u63a5\u53e3\u6269\u5c55\u4e86\u53e6\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u4e00\u4e2a
\n \u7c7b\u5b9e\u73b0\u63a5\u53e3<\/strong>\u3002\n <\/div>\n

Java Interface\u5173\u952e\u5b57_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c3\u5f20 <\/p>\n


\n

<\/p>\n

Java\u63a5\u53e3\u793a\u4f8b<\/h2>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0cPrintable\u63a5\u53e3\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u5176\u5b9e\u73b0\u5728A6\u7c7b\u4e2d\u3002\n <\/div>\n
\n
 interface printable{
    void print(<\/span>);
}
class A6 implements <\/span>printable{
    public void <\/span>print(<\/span>){
        System.out.println<\/span><\/span>(\"Hello\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        A6 obj = new <\/span>A6();
        obj.print<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
Hello<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

Java\u63a5\u53e3\u793a\u4f8b: Drawable <\/h2>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0cDrawable\u63a5\u53e3\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\u3002\u5b83\u7684\u5b9e\u73b0\u7531Rectangle\u548cCircle\u7c7b\u63d0\u4f9b\u3002\u5728\u5b9e\u9645\u60c5\u51b5\u4e0b\uff0c\u63a5\u53e3\u662f\u7531\u5176\u4ed6\u4eba\u5b9a\u4e49\u7684\uff0c\u4f46\u662f\u5176\u5b9e\u73b0\u662f\u7531\u4e0d\u540c\u7684\u5b9e\u73b0\u63d0\u4f9b\u7a0b\u5e8f\u63d0\u4f9b\u7684\u3002\u800c\u4e14\uff0c\u5b83\u88ab\u5176\u4ed6\u4eba\u4f7f\u7528\u3002\u4f7f\u7528\u8be5\u63a5\u53e3\u7684\u7528\u6237\u5c06\u5b9e\u73b0\u90e8\u5206\u9690\u85cf\u3002\n <\/div>\n
\n \u6587\u4ef6: TestInterface1.java\n <\/div>\n
\n
 \/\/Interface declaration: by first userinterface Drawable{
    void draw();
}
\/\/Implementation: by second userclass Rectangle implements <\/span>Drawable{
    public void <\/span>draw(){
        System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
    }
}
class Circle implements <\/span>Drawable{
    public void <\/span>draw(){
        System.out.println<\/span><\/span>(\"drawing circle\"<\/span>);
    }
}
\/\/Using interface: by third userclass TestInterface1{
    public <\/span>static void <\/span>main(String args[]){
        Drawable d=new Circle();
        \/\/In real scenario, object is provided by method e.g. getDrawable<\/span>()d.draw<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
drawing circle<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

Java\u63a5\u53e3\u793a\u4f8b: \u94f6\u884c<\/h2>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u53e6\u4e00\u4e2a\u63d0\u4f9bBank\u63a5\u53e3\u5b9e\u73b0\u7684Java\u63a5\u53e3\u793a\u4f8b\u3002\n <\/div>\n
\n \u6587\u4ef6: TestInterface2.java\n <\/div>\n
\n
 interface Bank{
    float<\/span> rateOfInterest();
}
class SBI implements <\/span>Bank{
    public <\/span>float<\/span> rateOfInterest(){
        return <\/span>9.15f;
    }
}
class PNB implements <\/span>Bank{
    public <\/span>float<\/span> rateOfInterest(){
        return <\/span>9.7f;
    }
}
class TestInterface2{
    public <\/span>static void <\/span>main(String[] args){
        Bank b=new SBI();
        System.out.println<\/span><\/span>(\"ROI: \"<\/span>+b.rateOfInterest<\/span>());
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
ROI: 9.15<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n

<\/p>\n

Java\u5728\u63a5\u53e3\u4e2d\u7684\u591a\u91cd\u7ee7\u627f<\/h2>\n
\n \u5982\u679c\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e86\u591a\u4e2a\u63a5\u53e3\uff0c\u6216\u8005\u4e00\u4e2a\u63a5\u53e3\u6269\u5c55\u4e86\u591a\u4e2a\u63a5\u53e3\uff0c\u5b83\u88ab\u79f0\u4e3a\u591a\u91cd\u7ee7\u627f\u3002\n <\/div>\n

Java Interface\u5173\u952e\u5b57_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c4\u5f20 <\/p>\n

\n
 interface Printable{
    void print(<\/span>);
}
interface Showable{
    void show();
}
class A7 implements <\/span>Printable,Showable{
    public void <\/span>print(<\/span>){
        System.out.println<\/span><\/span>(\"Hello\"<\/span>);
    }
    public void <\/span>show(){
        System.out.println<\/span><\/span>(\"Welcome\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        A7 obj = new <\/span>A7();
        obj.print<\/span>();
        obj.show<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
\n
Output:Hello \n
Welcome<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

<\/p>\n

Q)Java\u4e2d\u7684\u7c7b\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\uff0c\u4f46\u662f\u63a5\u53e3\u53ef\u4ee5\u5b9e\u73b0\u591a\u91cd\u7ee7\u627f\uff0c\u4e3a\u4ec0\u4e48\uff1f<\/h2>\n
\n \u6b63\u5982\u6211\u4eec\u5728\u7ee7\u627f\u4e00\u7ae0\u4e2d\u6240\u89e3\u91ca\u7684\uff0c\u7531\u4e8e\u6a21\u68f1\u4e24\u53ef\uff0c\u5728class\u4e2d\u4e0d\u652f\u6301\u591a\u91cd\u7ee7\u627f\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u6ca1\u6709\u6b67\u4e49\uff0c\u56e0\u6b64\u5728\u63a5\u53e3\u7684\u60c5\u51b5\u4e0b\u53d7\u652f\u6301\u3002\u8fd9\u662f\u56e0\u4e3a\u5176\u5b9e\u73b0\u662f\u7531\u5b9e\u73b0\u7c7b\u63d0\u4f9b\u7684\u3002\u4f8b\u5982:\n <\/div>\n
\n
 interface Printable{
    void print(<\/span>);
}
interface Showable{
    void print(<\/span>);
}
class TestInterface3 implements <\/span>Printable, Showable{
    public void <\/span>print(<\/span>){
        System.out.println<\/span><\/span>(\"Hello\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        TestInterface3 obj = new <\/span>TestInterface3();
        obj.print<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
Hello<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n
\n \u5982\u4e0a\u4f8b\u6240\u793a\uff0cPrintable\u548cShowable\u63a5\u53e3\u5177\u6709\u76f8\u540c\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u5176\u5b9e\u73b0\u7531TestTnterface1\u7c7b\u63d0\u4f9b\uff0c\u56e0\u6b64\u6ca1\u6709\u6b67\u4e49\u3002\n <\/div>\n
\n

\u63a5\u53e3\u7ee7\u627f<\/h2>\n
\n \u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f46\u662f\u4e00\u4e2a\u63a5\u53e3\u6269\u5c55\u4e86\u53e6\u4e00\u4e2a\u63a5\u53e3\u3002\n <\/div>\n
\n
 interface Printable{
    void print(<\/span>);
}
interface Showable extends <\/span>Printable{
    void show();
}
class TestInterface4 implements <\/span>Showable{
    public void <\/span>print(<\/span>){
        System.out.println<\/span><\/span>(\"Hello\"<\/span>);
    }
    public void <\/span>show(){
        System.out.println<\/span><\/span>(\"Welcome\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        TestInterface4 obj = new <\/span>TestInterface4();
        obj.print<\/span>();
        obj.show<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
Hello \n
Welcome<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

\u63a5\u53e3\u4e2d\u7684Java 8\u9ed8\u8ba4\u65b9\u6cd5<\/h2>\n
\n \u7531\u4e8eJava 8\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a5\u53e3\u4e2d\u4f7f\u7528\u65b9\u6cd5\u4e3b\u4f53\u3002\u4f46\u662f\u6211\u4eec\u9700\u8981\u5c06\u5176\u8bbe\u4e3a\u9ed8\u8ba4\u65b9\u6cd5\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50:\n <\/div>\n
\n \u6587\u4ef6: TestInterfaceDefault.java\n <\/div>\n
\n
 interface Drawable{
    void draw();
    default void <\/span>msg(){
        System.out.println<\/span><\/span>(\"default <\/span>method\"<\/span>);
    }
}
class Rectangle implements <\/span>Drawable{
    public void <\/span>draw(){
        System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
    }
}
class TestInterfaceDefault{
    public <\/span>static void <\/span>main(String args[]){
        Drawable d=new Rectangle();
        d.draw<\/span>();
        d.msg<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
drawing rectangle \n
default method<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

\u63a5\u53e3\u4e2d\u7684Java 8\u9759\u6001\u65b9\u6cd5<\/h2>\n
\n \u7531\u4e8eJava 8\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u63a5\u53e3\u4e2d\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u3002\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f8b\u5b50:\n <\/div>\n
\n \u6587\u4ef6: TestInterfaceStatic.java\n <\/div>\n
\n
 interface Drawable{
    void draw();
    static int cube(int x){
        return <\/span>x*x*x;
    }
}
class Rectangle implements <\/span>Drawable{
    public void <\/span>draw(){
        System.out.println<\/span><\/span>(\"drawing rectangle\"<\/span>);
    }
}
class TestInterfaceStatic{
    public <\/span>static void <\/span>main(String args[]){
        Drawable d=new Rectangle();
        d.draw<\/span>();
        System.out.println<\/span><\/span>(Drawable.cube<\/span>(3));
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa:\n <\/div>\n
\n
\n
drawing rectangle \n
27<\/code><\/pre>\n<\/p><\/div>\n<\/p><\/div>\n

<\/p>\n

Q)\u4ec0\u4e48\u662f\u6807\u8bb0\u6216\u6807\u8bb0\u63a5\u53e3\uff1f<\/h2>\n
\n \u6ca1\u6709\u6210\u5458\u7684\u63a5\u53e3\u79f0\u4e3a\u6807\u8bb0\u6216\u6807\u8bb0\u7684\u63a5\u53e3\uff0c\u4f8b\u5982\u53ef\u5e8f\u5217\u5316\uff0c\u53ef\u514b\u9686\uff0c\u8fdc\u7a0b\u7b49\u3002\u5b83\u4eec\u7528\u4e8e\u5411JVM\u63d0\u4f9b\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\uff0c\u4ee5\u4fbfJVM\u53ef\u4ee5\u6267\u884c\u4e00\u4e9b\u6709\u7528\u7684\u64cd\u4f5c\u3002\u64cd\u4f5c\u3002\n <\/div>\n
\n
 \/\/How Serializable interface <\/span>is written?public <\/span>interface Serializable{
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

Java\u4e2d\u7684\u5d4c\u5957\u63a5\u53e3<\/h3>\n
\n \u6ce8\u610f: \u4e00\u4e2a\u63a5\u53e3\u53ef\u4ee5\u5177\u6709\u53e6\u4e00\u4e2a\u79f0\u4e3a\u5d4c\u5957\u63a5\u53e3\u7684\u63a5\u53e3\u3002\u6211\u4eec\u5c06\u5728\u5d4c\u5957\u7c7b\u4e00\u7ae0\u4e2d\u8be6\u7ec6\u5b66\u4e60\u5b83\u3002\u4f8b\u5982:\n <\/div>\n
\n
 interface printable{
    void print(<\/span>);
    interface MessagePrintable{
        void msg();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java Interface\u5173\u952e\u5b57zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/724"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}