父類(lèi)程序:package HelloWorldf;public class HelloWorld {public int age=20;public String name;public void show() {?? ?System.out.println("姓名:"+name+"年齡:"+age+"你有會(huì)叫的功能");}public HelloWorld() {?? ??? ?System.out.println("年紀(jì):"+age);?? ?age=3;}}子類(lèi)程序:package HelloWorldf;public class HelloWorldt extends HelloWorld {}測(cè)試程序:package HelloWorldf;public class test {?? ?public static void main(String[] args) {?? ??? ?// TODO Auto-generated method stubHelloWorldt two=new HelloWorldt();two.name="dog";two.show();HelloWorld eat=new HelloWorld();?? ?}}運(yùn)行結(jié)果:年紀(jì):20姓名:dog年齡:3你有會(huì)叫的功能年紀(jì):20
1 回答

風(fēng)中打傘的小孩
TA貢獻(xiàn)73條經(jīng)驗(yàn) 獲得超20個(gè)贊
實(shí)例化子類(lèi)時(shí)先調(diào)用父類(lèi)構(gòu)造函數(shù),
System.out.println("年紀(jì):"+age);//因?yàn)閷傩宰兞砍跏贾禐?0,所以輸出年紀(jì):20
age=3;?System.out.println("年紀(jì):"+age);//此時(shí)age已經(jīng)變成3,自然打印3了
添加回答
舉報(bào)
0/150
提交
取消