{"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
\u62bd\u8c61\u7c7b(0\u5230100\uff05)<\/span> \u5fc5\u987b\u4f7f\u7528abstract\u5173\u952e\u5b57\u58f0\u660e\u62bd\u8c61\u7c7b\u3002<\/span> <\/body>
\n \u63a5\u53e3(100\uff05)<\/span> <\/p>\n
\n Java\u4e2d\u7684\u62bd\u8c61\u7c7b<\/h3>\n
\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 \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 <\/p>\n
abstract class <\/span>A{
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Java\u4e2d\u7684\u62bd\u8c61\u65b9\u6cd5<\/h3>\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
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 running safely
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n\u4e86\u89e3Abstract\u7c7b\u7684\u771f\u5b9e\u60c5\u51b5<\/h3>\n
\n \u5de5\u5382\u65b9\u6cd5<\/strong>\u63d0\u4f9b\u3002\n <\/div>\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 drawing circle
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n java\u4e2dAbstract\u7c7b\u7684\u53e6\u4e00\u4e2a\u793a\u4f8b<\/h3>\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 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
\/\/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 bike is created running safely.. gear changed
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n\n
class <\/span>Bike12{
abstract void <\/span>run();
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n compile time error
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n
\n\u62bd\u8c61\u7c7b\u7684\u53e6\u4e00\u79cd\u771f\u5b9e\u60c5\u51b5<\/h3>\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 Output:I am a I am b I am c I am d
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\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}]}}