欢迎来到天天文库
浏览记录
ID:52241044
大小:66.80 KB
页数:19页
时间:2020-03-25
《面向对象编程的高级概念Java IBM.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、面向对象编程的高级概念2005IBMSoftwareInstituteIBMSoftwareInstitute主要内容°类的继承°抽象类和接口°Java语言的包°内部类与嵌套类课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——继承的实现classEmployee{°继承使得子类可以利Stringname;用父类中定义的方法和变量,就像它们属publicvoidshowName(){System.out.println(name);于子类本身一样。}}classManagerextendsEmplo
2、yee{Stringdepartment;publicvoidshowInfo(){showName();System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——域的隐藏classEmployee{°子类重新定义一个与Stringname;从父类那里继承来的Stringdepartment;域变量完全相同的变publicvoidshowName(){System.out.println(name);量成为域的隐藏}
3、}classManagerextendsEmployee{Stringdepartment;publicvoidshowInfo(){showName();System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——方法的覆盖classEmployee{°覆盖使得在子类中可Stringname;以重新定义父类中已有的方法,从而使子publicvoidshowInfo(){System.out.println(name)
4、;类具有自己的行为。}}classManagerextendsEmployee{Stringdepartment;publicvoidshowInfo(){System.out.println(name);System.out.println(“Managerof”+department);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——this与superclassCircle{°this和super是用来指代父doubler;类和子类的对象的关键字。Circle(intr){°this:
5、代表了当前对象的一this.r=r;}个引用publicdoublearea(){°super:是当前对象的直接returnr*r*3.14;父类对象的引用}°二者还可以调用当前对象或}父类对象的构造函数classappleextendsfruits{publicapple(intprice){super(price);super.var=someValue;super.method(someParaList);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——继承、隐藏和覆盖示例public
6、classMySubClassextendsMyClass{publicStrings="amemberofMySubClass";publicvoidtest(){super.test();System.out.println(s);}publicstaticvoidmain(String[]args){MySubClassmsc=newMySubClass();System.out.println(msc.s);amemberofMySubClassmsc.test();amemberofMyClass}amemberofMySubClass}clas
7、sMyClass{protectedStrings="amemberofMyClass";protectedvoidtest(){System.out.println(s);}}课程大标题2005IBMSoftwareInstituteIBMSoftwareInstitute类的继承——父子类对象转换原则classSuperClass{°子类对象可以被视为是其父intx;类的一个对象}°父类对象不能被当作是某一classSubClassextendsSuperClass{个子类的对象inty;charch;}publicclassUseSuperSub{p
8、ublicstaticvoidmain(String[]args)
此文档下载收益归作者所有