{"id":723,"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 \u62bd\u8c61\u7c7b","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/723.html","title":{"rendered":"Java \u62bd\u8c61\u7c7b"},"content":{"rendered":"


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

\n

Java \u62bd\u8c61\u7c7b<\/h1>\n<\/p><\/div>\n
\n \u4f7f\u7528abstract\u5173\u952e\u5b57\u58f0\u660e\u7684\u7c7b\u5728Java\u4e2d\u88ab\u79f0\u4e3a\u62bd\u8c61\u7c7b\u3002\u5b83\u53ef\u4ee5\u5177\u6709\u62bd\u8c61\u65b9\u6cd5\u548c\u975e\u62bd\u8c61\u65b9\u6cd5(\u4e0e\u4e3b\u4f53\u6709\u5173\u7684\u65b9\u6cd5)\u3002\n <\/div>\n
\n \u5728\u5b66\u4e60Java\u62bd\u8c61\u7c7b\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u9996\u5148\u4e86\u89e3Java\u4e2d\u7684\u62bd\u8c61\u3002\n <\/div>\n
\n

Java\u4e2d\u7684\u62bd\u8c61<\/h3>\n
\n \u62bd\u8c61<\/strong>\u662f\u4e00\u4e2a\u9690\u85cf\u5b9e\u73b0\u7ec6\u8282\u5e76\u4ec5\u5411\u7528\u6237\u663e\u793a\u529f\u80fd\u7684\u8fc7\u7a0b\u3002\n <\/div>\n
\n \u53e6\u4e00\u79cd\u65b9\u5f0f\uff0c\u5b83\u4ec5\u5411\u7528\u6237\u663e\u793a\u57fa\u672c\u5185\u5bb9\u5e76\u9690\u85cf\u5185\u90e8\u7ec6\u8282\uff0c\u4f8b\u5982\uff0c\u5728\u60a8\u952e\u5165\u6587\u672c\u5e76\u53d1\u9001\u6d88\u606f\u7684\u4f4d\u7f6e\u53d1\u9001SMS\u3002\u60a8\u4e0d\u77e5\u9053\u6709\u5173\u6d88\u606f\u4f20\u9012\u7684\u5185\u90e8\u5904\u7406\u3002\n <\/div>\n
\n \u62bd\u8c61\u4f7f\u60a8\u53ef\u4ee5\u4e13\u6ce8\u4e8eobject\u6240\u505a\u7684\u5de5\u4f5c\u5b83\u662f\u5982\u4f55\u505a\u5230\u7684\u3002\n <\/div>\n

\u5b9e\u73b0\u62bd\u8c61\u7684\u65b9\u6cd5<\/h3>\n
\n \u5728Java\u4e2d\u6709\u4e24\u79cd\u5b9e\u73b0\u62bd\u8c61\u7684\u65b9\u6cd5\n <\/div>\n

\u62bd\u8c61\u7c7b(0\u5230100\uff05)<\/span>
\n \u63a5\u53e3(100\uff05)<\/span> <\/p>\n


\n

Java\u4e2d\u7684\u62bd\u8c61\u7c7b<\/h3>\n
\n \u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u7684\u7c7b\u79f0\u4e3a
\n \u62bd\u8c61\u7c7b<\/strong>\u3002\u5b83\u53ef\u4ee5\u5177\u6709\u62bd\u8c61\u548c\u975e\u62bd\u8c61\u65b9\u6cd5\u3002\u5b83\u9700\u8981\u6269\u5c55\u5e76\u5b9e\u73b0\u5176\u65b9\u6cd5\u3002\n <\/div>\n
\n \u8981\u8bb0\u4f4f\u7684\u8981\u70b9<\/strong>\n <\/div>\n

\u5fc5\u987b\u4f7f\u7528abstract\u5173\u952e\u5b57\u58f0\u660e\u62bd\u8c61\u7c7b\u3002<\/span>
\n \u5b83\u53ef\u4ee5\u5177\u6709\u62bd\u8c61\u548c\u975e\u62bd\u8c61\u65b9\u6cd5\u3002<\/span>
\n \u65e0\u6cd5\u5b9e\u4f8b\u5316\u3002<\/span>
\n \u5b83\u53ef\u4ee5\u5177\u6709\u6784\u9020\u51fd\u6570\u548c\u9759\u6001\u65b9\u6cd5\u3002<\/span>
\n \u5b83\u53ef\u4ee5\u5177\u6709\u6700\u7ec8\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u5f3a\u5236\u5b50\u7c7b\u4e0d\u66f4\u6539\u65b9\u6cd5\u7684\u4e3b\u4f53\u3002<\/span>
\n Java \u62bd\u8c61\u7c7b_https:\/\/bianchenghao6.com_\u3010Java \u57fa\u7840\u6559\u7a0b\u3011_\u7b2c1\u5f20 <\/p>\n

\n \u62bd\u8c61\u7c7b\u7684\u793a\u4f8b<\/strong>\n <\/div>\n
\n
 abstract class <\/span>A{
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

Java\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5<\/h3>\n
\n \u88ab\u58f0\u660e\u4e3a\u62bd\u8c61\u4e14\u6ca1\u6709\u5b9e\u73b0\u7684\u65b9\u6cd5\u88ab\u79f0\u4e3a\u62bd\u8c61\u65b9\u6cd5\u3002\n <\/div>\n
\n \u62bd\u8c61\u65b9\u6cd5\u793a\u4f8b<\/strong>\n <\/div>\n
\n
 abstract void <\/span>printStatus();
\/\/no method body and abstract
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u5177\u6709\u62bd\u8c61\u65b9\u6cd5\u7684Abstract\u7c7b\u793a\u4f8b<\/h3>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0cBike\u662f\u4e00\u4e2a\u4ec5\u5305\u542b\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u8fd0\u884c\u7684\u62bd\u8c61\u7c7b\u3002\u5b83\u7684\u5b9e\u73b0\u7531Honda\u7c7b\u63d0\u4f9b\u3002\n <\/div>\n
\n
 abstract class <\/span>Bike{
    abstract void <\/span>run();
}
class <\/span>Honda4 extends <\/span>Bike{
    void run(){
        System.out.println<\/span>(\"running safely\"<\/span>);
    }
    public <\/span>static void <\/span>main(String args[]){
        Bike obj = new <\/span>Honda4();
        obj.run<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 running safely
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u4e86\u89e3Abstract\u7c7b\u7684\u771f\u5b9e\u60c5\u51b5<\/h3>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0cShape\u662f\u62bd\u8c61\u7c7b\uff0c\u5176\u5b9e\u73b0\u7531Rectangle\u548cCircle\u7c7b\u63d0\u4f9b\u3002\n <\/div>\n
\n \u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4e0d\u4e86\u89e3\u5b9e\u73b0\u7c7b(\u5bf9\u6700\u7ec8\u7528\u6237\u9690\u85cf)\uff0c\u800c\u5b9e\u73b0\u7c7b\u7684\u5bf9\u8c61\u7531
\n \u5de5\u5382\u65b9\u6cd5<\/strong>\u63d0\u4f9b\u3002\n <\/div>\n
\n \u5de5\u5382\u65b9\u6cd5<\/strong>\u662f\u4e00\u79cd\u8fd4\u56de\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\n <\/div>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u5982\u679c\u60a8\u521b\u5efaRectangle\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5219\u5c06\u8c03\u7528Rectangle\u7c7b\u7684draw()\u65b9\u6cd5\u3002\n <\/div>\n
\n \u6587\u4ef6: TestAbstraction1.java\n <\/div>\n
\n
 abstract class <\/span>Shape{
    abstract void <\/span>draw();
}
\/\/In real scenario, implementation is provided by others i.e. unknown by end userclass <\/span>Rectangle extends <\/span>Shape{
    void draw(){
        System.out.println<\/span>(\"drawing rectangle\"<\/span>);
    }
}
class <\/span>Circle1 extends <\/span>Shape{
    void draw(){
        System.out.println<\/span>(\"drawing circle\"<\/span>);
    }
}
\/\/In real scenario, method is called by programmer or userclass <\/span>TestAbstraction1{
    public <\/span>static void <\/span>main(String args[]){
        Shape s=new Circle1();
        \/\/In a real scenario, object is provided through method, e.g., getShape<\/span>() methods.draw<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 drawing circle
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

java\u4e2dAbstract\u7c7b\u7684\u53e6\u4e00\u4e2a\u793a\u4f8b<\/h3>\n
\n \u6587\u4ef6: TestBank.java\n <\/div>\n
\n
 abstract class <\/span>Bank{
    abstract int <\/span>getRateOfInterest();
}
class <\/span>SBI extends <\/span>Bank{
    int <\/span>getRateOfInterest(){
        return <\/span>7;
    }
}
class <\/span>PNB extends <\/span>Bank{
    int <\/span>getRateOfInterest(){
        return <\/span>8;
    }
}
class <\/span>TestBank{
    public <\/span>static void <\/span>main(String args[]){
        Bank b;
        b=new SBI();
        System.out.println<\/span>(\"Rate of Interest <\/span>is: \"<\/span><\/span>+b.getRateOfInterest<\/span>()+\" %\"<\/span><\/span>);
        b=new PNB();
        System.out.println<\/span>(\"Rate of Interest <\/span>is: \"+b.getRateOfInterest<\/span>()+\" %\");
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Rate of Interest <\/span>is: 7 %Rate of Interest <\/span>is: 8 %
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u5177\u6709\u6784\u9020\u51fd\u6570\uff0c\u6570\u636e\u6210\u5458\u548c\u65b9\u6cd5\u7684\u62bd\u8c61\u7c7b<\/h3>\n
\n \u62bd\u8c61\u7c7b\u53ef\u4ee5\u5177\u6709\u6570\u636e\u6210\u5458\uff0c\u62bd\u8c61\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e3b\u4f53(\u975e\u62bd\u8c61\u65b9\u6cd5)\uff0c\u6784\u9020\u51fd\u6570\uff0c\u751a\u81f3main()\u65b9\u6cd5\u3002\n <\/div>\n
\n \u6587\u4ef6: TestAbstraction2.java\n <\/div>\n
\n
 \/\/Example of an abstract <\/span>class <\/span>that has abstract <\/span>and non-abstract methods abstract <\/span>class <\/span>Bike{
    Bike(){
        System.out.println<\/span>(\"bike is created\"<\/span>);
    }
    abstract void <\/span>run();
    void changeGear(){
        System.out.println<\/span>(\"gear changed\"<\/span>);
    }
}
\/\/Creating a Child class <\/span>which inherits abstract <\/span>class <\/span>class <\/span>Honda extends <\/span>Bike{
    void run(){
        System.out.println<\/span>(\"running safely..\"<\/span>);
    }
}
\/\/Creating a Test class <\/span>which calls abstract <\/span>and non-abstract methods class <\/span>TestAbstraction2{
    public <\/span>static void <\/span>main(String args[]){
        Bike obj = new <\/span>Honda();
        obj.run<\/span>();
        obj.changeGear<\/span>();
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 bike is created running safely.. gear changed
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\n
\n \u6ce8\u610f: <\/span>\u89c4\u5219: \u5982\u679c\u7c7b\u4e2d\u5b58\u5728\u62bd\u8c61\u65b9\u6cd5\uff0c\u5219\u8be5\u7c7b\u5fc5\u987b\u662f\u62bd\u8c61\u65b9\u6cd5\u3002\n <\/div>\n<\/h4>\n
\n
 class <\/span>Bike12{
    abstract void <\/span>run();
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 compile time error
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\n
\n \u6ce8\u610f: <\/span>\u89c4\u5219: \u5982\u679c\u8981\u6269\u5c55\u5177\u6709\u62bd\u8c61\u65b9\u6cd5\u7684\u62bd\u8c61\u7c7b\uff0c\u5219\u5fc5\u987b\u63d0\u4f9b\u8be5\u65b9\u6cd5\u7684\u5b9e\u73b0\u6216\u4f7f\u8be5\u7c7b\u62bd\u8c61\u3002\n <\/div>\n<\/h4>\n
\n

\u62bd\u8c61\u7c7b\u7684\u53e6\u4e00\u79cd\u771f\u5b9e\u60c5\u51b5<\/h3>\n
\n \u62bd\u8c61\u7c7b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63d0\u4f9binterface\u7684\u67d0\u4e9b\u5b9e\u73b0\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6700\u7ec8\u7528\u6237\u53ef\u80fd\u4e0d\u4f1a\u88ab\u5f3a\u5236\u91cd\u5199\u63a5\u53e3\u7684\u6240\u6709\u65b9\u6cd5\u3002\n <\/div>\n
\n \u6ce8\u610f:<\/b> \u5982\u679c\u60a8\u662fJava\u7684\u521d\u5b66\u8005\uff0c\u8bf7\u5148\u5b66\u4e60\u63a5\u53e3\u5e76\u8df3\u8fc7\u6b64\u793a\u4f8b\u3002\n <\/div>\n
\n
 interface A{
    void a();
    void b();
    void c();
    void d();
}
abstract class <\/span>B implements <\/span>A{
    public void <\/span>c(){
        System.out.println<\/span>(\"I am c\"<\/span>);
    }
}
class <\/span>M extends <\/span>B{
    public void <\/span>a(){
        System.out.println<\/span>(\"I am a\"<\/span>);
    }
    public void <\/span>b(){
        System.out.println<\/span>(\"I am b\"<\/span>);
    }
    public void <\/span>d(){
        System.out.println<\/span>(\"I am d\"<\/span>);
    }
}
class <\/span>Test5{
    public <\/span>static void <\/span>main(String args[]){
        A a=new M();
        a.a<\/span>();
        a.b<\/span>();
        a.c<\/span>();
        a.d<\/span>();
    }
} <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Output:I am a I am b I am c I am d
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u62bd\u8c61\u7c7bzh-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-723","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/723"}],"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=723"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}