{"id":719,"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 \u805a\u5408","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/719.html","title":{"rendered":"Java \u805a\u5408"},"content":{"rendered":"
\n
class Employee{ \n
int id; \n
String name; \n
Address address;\/\/Address is a class \n
... \n
} \n
<\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u96c7\u5458\u5177\u6709\u5b9e\u4f53\u5f15\u7528\u5730\u5740\uff0c\u56e0\u6b64\u5173\u7cfb\u4e3a\u96c7\u5458HAS-A\u5730\u5740\u3002\n <\/div>\n\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u805a\u5408\uff1f<\/h2>\n
\u7528\u4e8e\u4ee3\u7801\u53ef\u91cd\u7528\u6027\u3002<\/span> <\/p>\n\u805a\u5408\u7684\u7b80\u5355\u793a\u4f8b<\/h2>\n
<\/p>\n
\n \u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728Circle\u7c7b\u4e2d\u521b\u5efa\u4e86Operation\u7c7b\u7684\u5f15\u7528\u3002\n <\/div>\n\n class Operation{
int square(int n){
return <\/span>n*n;
}
}
class Circle{
Operation op;
\/\/aggregation double <\/span>pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square<\/span>(radius);
\/\/code reusability (i.e. delegates the method call). return <\/span>pi*rsquare;
}
public <\/span>static void <\/span>main(String args[]){
Circle c=new Circle();
double result=c.area<\/span>(5);
System.out.println<\/span><\/span>(result);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u8f93\u51fa\uff1a<\/strong>\n <\/div>\n\n78.5<\/code><\/pre>\n<\/p><\/div>\n\u4ec0\u4e48\u65f6\u5019\u4f7f\u7528\u805a\u5408\uff1f<\/h2>\n
\u5f53\u6ca1\u6709is-a\u5173\u7cfb\u65f6\uff0c\u901a\u8fc7\u805a\u5408\u4e5f\u53ef\u4ee5\u6700\u597d\u5730\u5b9e\u73b0\u4ee3\u7801\u91cd\u7528\u3002<\/span>
\n \u53ea\u6709\u5728\u5173\u7cfb-a\u5728\u6240\u6d89\u53ca\u5bf9\u8c61\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u4e2d\u90fd\u4fdd\u6301\u4e0d\u53d8\u65f6\uff0c\u624d\u5e94\u4f7f\u7528\u7ee7\u627f\u3002\u5426\u5219\uff0c\u805a\u5408\u662f\u6700\u4f73\u9009\u62e9\u3002<\/span> <\/p>\n\n Address.java\n <\/div>\n\n public <\/span>class Address {
String city,state,country;
public <\/span>Address(String city, String state, String country) {
this<\/span>.city = city;
this<\/span>.state = state;
this<\/span>.country <\/span>= country;
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Emp.java\n <\/div>\n\n public <\/span>class Emp {
int id;
String name;
Address address;
public <\/span>Emp(int id, String name,Address address) {
this<\/span>.id = id;
this<\/span>.name = name;
this<\/span>.address=address;
}
void display(){
System.out.println<\/span><\/span>(id+\" \"<\/span><\/span><\/span>+name);
System.out.println<\/span><\/span>(address.city+\" \"+address.state+\" \"+address.country);
}
public <\/span>static void <\/span>main(String[] args) {
Address address1=new Address(\"gzb\"<\/span>,\"UP\"<\/span><\/span>,\"india\"<\/span><\/span>);
Address address2=new Address(\"gno\"<\/span>,\"UP\",\"india\");
Emp e=new Emp(111,\"varun\"<\/span>,address1);
Emp e2=new Emp(112,\"arun\"<\/span>,address2);
e.display<\/span>();
e2.display<\/span>();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u8f93\u51fa\uff1a<\/strong>\n <\/div>\n\n 111 varun
gzb UP india
112 arun
gno UP india
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u805a\u5408zh-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-719","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/719"}],"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=719"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}