{"id":721,"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 Instance\u521d\u59cb\u5316","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/721.html","title":{"rendered":"Java Instance\u521d\u59cb\u5316"},"content":{"rendered":"
\n
class <\/span>Bike7{
int <\/span>speed;
Bike7(){
System.out.println<\/span>(\"speed is \"<\/span>+speed);
}
{
speed=100;
}
public <\/span>static void <\/span>main(String args[]){
Bike7 b1=new Bike7();
Bike7 b2=new Bike7();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Output:speed is 100 speed is 100
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n java\u4e2d\u6709\u4e09\u4e2a\u53ef\u4ee5\u6267\u884c\u64cd\u4f5c\u7684\u5730\u65b9:\n <\/div>\n \u65b9\u6cd5<\/span>
\n \u6784\u9020\u51fd\u6570<\/span>
\n \u7a0b\u5e8f\u5757<\/span> <\/p>\n
\n\u9996\u5148\u8c03\u7528\u7684\u662f\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u8fd8\u662f\u6784\u9020\u51fd\u6570\uff1f<\/h2>\n\n class <\/span>Bike8{
int <\/span>speed;
Bike8(){
System.out.println<\/span>(\"constructor is invoked\"<\/span>);
}
{
System.out.println<\/span>(\"instance initializer block invoked\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
Bike8 b1=new Bike8();
Bike8 b2=new Bike8();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n instance initializer block invoked constructor is invoked instance initializer block invoked constructor is invoked
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f3c\u4e4e\u9996\u5148\u8c03\u7528\u4e86\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\uff0c\u4f46\u6ca1\u6709\u3002\u521b\u5efa\u5bf9\u8c61\u65f6\u5c06\u8c03\u7528\u5b9e\u4f8b\u521d\u59cb\u5316\u5668\u5757\u3002 Java\u7f16\u8bd1\u5668\u5728\u7b2c\u4e00\u4e2a\u8bed\u53e5super()\u4e4b\u540e\u5c06\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u590d\u5236\u5230\u6784\u9020\u51fd\u6570\u4e2d\u3002\u56e0\u6b64\uff0c\u9996\u5148\uff0c\u8c03\u7528\u6784\u9020\u51fd\u6570\u3002\u8ba9\u6211\u4eec\u901a\u8fc7\u4e0b\u56fe\u4e86\u89e3\u5b83:\n <\/div>\n\n \u6ce8\u610f: <\/span>Java\u7f16\u8bd1\u5668\u4f1a\u5728\u6bcf\u4e2a\u6784\u9020\u51fd\u6570\u4e2d\u590d\u5236\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u7684\u4ee3\u7801\u3002\n <\/div>\n
<\/p>\n
\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u7684\u89c4\u5219: <\/h2>\n\n \u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u4e3b\u8981\u6709\u4e09\u4e2a\u89c4\u5219\u3002\u5b83\u4eec\u5982\u4e0b:\n <\/div>\n \u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\u65f6\uff0c\u5c06\u521b\u5efa\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u3002<\/span>
\n \u5b9e\u4f8b\u6784\u9020\u5668\u5757\u5728\u7236\u7c7b\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u4e4b\u540e(\u5373\u5728super()\u6784\u9020\u51fd\u6570\u8c03\u7528\u4e4b\u540e)\u88ab\u8c03\u7528\u3002<\/span>
\n \u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u6309\u5b83\u4eec\u51fa\u73b0\u7684\u987a\u5e8f\u6392\u5217\u3002<\/span> <\/p>\n\u5b9e\u4f8b\u521d\u59cb\u5316\u7a0b\u5e8f\u5757\u7684\u7a0b\u5e8f\uff0c\u8be5\u7a0b\u5e8f\u5728super()\u4e4b\u540e\u8c03\u7528<\/h2>\n\n class <\/span>A{
A(){
System.out.println<\/span>(\"parent class <\/span>constructor invoked\"<\/span>);
}
}
class <\/span>B2 extends <\/span>A{
B2(){
super();
System.out.println<\/span>(\"child class <\/span>constructor invoked\"<\/span>);
}
{
System.out.println<\/span>(\"instance initializer block is invoked\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
B2 b=new B2();
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n Output:parent class <\/span>constructor invoked instance initializer block is invoked child class <\/span>constructor invoked
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n\u5b9e\u4f8b\u5757\u7684\u53e6\u4e00\u4e2a\u793a\u4f8b<\/h2>\n\n class <\/span>A{
A(){
System.out.println<\/span>(\"parent class <\/span>constructor invoked\"<\/span>);
}
}
class <\/span>B3 extends <\/span>A{
B3(){
super();
System.out.println<\/span>(\"child class <\/span>constructor invoked\"<\/span>);
}
B3(int <\/span>a){
super();
System.out.println<\/span>(\"child class <\/span>constructor invoked \"<\/span>+a);
}
{
System.out.println<\/span>(\"instance initializer block is invoked\"<\/span>);
}
public <\/span>static void <\/span>main(String args[]){
B3 b1=new B3();
B3 b2=new B3(10);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n\n parent class <\/span>constructor invoked instance initializer block is invoked child class <\/span>constructor invoked parent class <\/span>constructor invoked instance initializer block is invoked child class <\/span>constructor invoked 10
<\/span><\/code><\/pre>\n<\/p><\/div>\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java Instance\u521d\u59cb\u5316zh-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-721","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/721"}],"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=721"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}