《Java编程 第四章》由会员分享,可在线阅读,更多相关《Java编程 第四章(62页珍藏版)》请在金锄头文库上搜索。
1、面向对象特性,封装性继承性多态性,封装性,类定义: class className public | protected | private static final transient volatile type variableName; /成员变量 public | protected | private static final | abstract native synchronized returnType methodName(paramList) throws exceptionList statements /成员方法 ,权限修饰词(Modifier),同一个类 同一个包 不
2、同包的子类 不同包非子类private * Default * *(friendly)protected * * *public * * * *,继承性,通过继承实现代码复用 根类: java.lang.Object父类包括直接或者间接被继承的类Java不支持多重继承子类不能访问父类中访问权限为private的成员变量和方法。子类可以重写父类的方法,及命名与父类同名的成员变量,创建子类,格式:class SubClass extends SuperClass ,成员变量的隐藏和方法的重写,class SuperClass int x; void setX( ) x=0; class SubCl
3、ass extends SuperClass int x; /hide x in SuperClass void setX( ) /override method setX() x=5; . ,重写,子类中重写的方法和父类中被重写的方法要具有相同的名字,相同的参数表和相同的返回类型,super,super用来引用当前对象的父类(1) 访问父类被隐藏的成员变量,如: super.variable; (2)调用父类中被重写的方法,如: super.Method(paramlist);(3)调用父类的构造函数,如: super(paramlist);,super,class SuperClass i
4、nt x; SuperClass( ) x=3; System.out.println(“in SuperClass : x=“ +x); void doSomething( ) System.out.println(“in SuperClass.doSomething()”); ,super,class SubClass extends SuperClass int x; SubClass( ) super( ); /call constructor of superclass x=5; /super( ) 要放在方法中的第一句 System.out.println(“in SubClass
5、 :x=“+x); void doSomething( ) super.doSomething( ); /call method of superclassSystem.out.println(“in SubClass.doSomething()”);,super,System.out.println(“super.x=”+super.x+“sub.x=”+x) public class inheritance public static void main(String args) SubClass subC=new SubClass(); subC.doSomething(); ,supe
6、r,运行结果c: java inheritancein SuperClass: x=3in SubClass: x=5in SuperClass.doSomething()in SubClass.doSomething()super.x=3 sub.x=5,多态性,静态多态性(编译时多态 由方法重载实现动态多态性(运行时多态) 由方法重写实现 调用规则:根据实例的类型,模拟考题,Question 57)Given the following codeclass Base class Agg extends Base public String getFields() String name =
7、 Agg; return name; public class Avfpublic static void main(String argv) Base a = new Agg(); /Here ,模拟考题,What code placed after the comment /Here will result in calling the getFields method resulting in the output of the string Agg?1) System.out.println(a.getFields();2) System.out.println(a.name);3)
8、System.out.println(Base) a.getFields();4) System.out.println( (Agg) a).getFields();,模拟考题,Answer to Question 57) 4) System.out.println( (Agg) a).getFields();,重写方法的调用规则,例6.9class A void callme( ) System.out.println(“Inside As callme() method”); class B extends A void callme( ) System.out.println(“Insi
9、de Bs callme() Method”) ,重写方法的调用规则,public class Dispatch public static void main(String args) A a=new B(); /B a=new A(); a.callme( ); ,重写方法的调用规则,运行结果c: java Dispatch Inside Bs callme() method,方法重写应遵循的原则,(1)改写后的方法不能比被重写的方法有更严格的访问权限(2)改写后的方法不能比重写的方法产生更多的例外,方法重写应遵循的原则,class Parent public void function(
10、 ) class Child extends Parent private void function( ) public class OverriddenTest public static void main(String args) Parent p1=new Parent(); Parent p2=new Child(); p1.function(); p2.function(); ,对象状态的确定(instanceof),Manager和Contractor都是Employee的子类public void method(Employee e) if (e instanceof Man
11、ager) /do something as a Manager else if ( e instanceof Contractor) /do something as a Contractor else /do something else ,模拟考题,Question 6).Which of the following statements are true?1) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface. 2
12、) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class,模拟考题,3) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain 4) The inst
13、anceof operator can be used to determine if one reference is of the same class as another reference thus,模拟考题,Answer to Question 6) 2) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class,final关键字,(1) final修饰变量,则成为常量 final type variableName; 修饰成员变量时,定义时同时给出初始值,而修饰局部变量时不做要求。(2)final修饰方法,则该方法不能被子类重写 final returnType methodName(paramList) ,