面向对象编程的高级概念Java IBM.pdf
上传人:qw****27 上传时间:2024-09-12 格式:PDF 页数:19 大小:66KB 金币:15 举报 版权申诉
预览加载中,请您耐心等待几秒...

面向对象编程的高级概念Java IBM.pdf

面向对象编程的高级概念JavaIBM.pdf

预览

免费试读已结束,剩余 9 页请下载文档后查看

15 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

面向对象编程的高级概念2005IBMSoftwareInstituteIBMSoftwareInstitute主要内容ƒ类的继承ƒ抽象类和接口ƒJava语言的包ƒ内部类与嵌套类课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——继承的实现classEmployee{ƒ继承使得子类可以利Stringname;用父类中定义的方法和变量,就像它们属publicvoidshowName(){System.out.println(name);于子类本身一样。}}classManagerextendsEmployee{Stringdepartment;publicvoidshowInfo(){showName();System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——域的隐藏classEmployee{ƒ子类重新定义一个与Stringname;从父类那里继承来的Stringdepartment;域变量完全相同的变publicvoidshowName(){System.out.println(name);量成为域的隐藏}}classManagerextendsEmployee{Stringdepartment;publicvoidshowInfo(){showName();System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——方法的覆盖classEmployee{ƒ覆盖使得在子类中可Stringname;以重新定义父类中已有的方法,从而使子publicvoidshowInfo(){System.out.println(name);类具有自己的行为。}}classManagerextendsEmployee{Stringdepartment;publicvoidshowInfo(){System.out.println(name);System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——this与superclassCircle{ƒthis和super是用来指代父doubler;类和子类的对象的关键字。Circle(intr){ƒthis:代表了当前对象的一this.r=r;个引用}publicdoublearea(){ƒsuper:是当前对象的直接returnr*r*3.14;父类对象的引用}ƒ二者还可以调用当前对象或}父类对象的构造函数classappleextendsfruits{publicapple(intprice){super(price);super.var=someValue;super.method(someParaList);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——继承、隐藏和覆盖示例publicclassMySubClassextendsMyClass{publicStrings="amemberofMySubClass";publicvoidtest(){super.test();System.out.println(s);}publicstaticvoidmain(String[]args){MySubClassmsc=newMySubClass();System.out.println(msc.s);amemberofMySubClassmsc.test();amemberofMyClass}amemberofMySubClass}classMyClass{protectedStrings="amemberofMyClass";protectedvoidtest(){System.out.println(s);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——父子类对象转换原则classSuperClas