资源预览内容
第1页 / 共21页
第2页 / 共21页
第3页 / 共21页
第4页 / 共21页
第5页 / 共21页
第6页 / 共21页
第7页 / 共21页
第8页 / 共21页
第9页 / 共21页
第10页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
复习和总结,Java面向对象编程,总结,封装性: 对象和类的概念 Java中的类 类成员及其访问控制* 构造方法* 重载方法* static变量,方法和初始代码块* this引用的用途 final类,方法和变量* abstract类和方法,接口* 内部类 package和import关键字*,总结,继承性 : extends关键字* super关键字* 继承的优点和缺点* 继承和组合的关系,总结,多态性 : 方法覆盖(override) * 多态行为(polymorphism)* 对象类型转换(Up_casting,Down_casting) * Instanceof *,从“多态”的角度: 以多态为中心的面向对象编程相关元素,练习:基本类型作为方法参数,练习:数组类型作为方法参数,练习:引用类型作为方法参数,class Circle double radius; Circle(double newRadius) radius = newRadius; ,Show the output of the following program:,public class Test public static void main(String args) Circle circle1 = new Circle(1); Circle circle2 = new Circle(2); swap1(circle1, circle2); System.out.println(After swap1: circle1 = + circle1.radius + circle2 = + circle2.radius); swap2(circle1, circle2); System.out.println(After swap2: circle1 = + circle1.radius + circle2 = + circle2.radius); public static void swap1(Circle x, Circle y) Circle temp = x; x = y; y = temp; public static void swap2(Circle x, Circle y) double temp = x.radius; x.radius = y.radius; y.radius = temp; ,练习:类成员访问控制,package p1; public class A ? int i; ? void m() ,package p1; public class B extends A public void ml() System.out.println(i); m(); public static void main(String args) ,下面两个类都在同一包下,“?”分别是“default ”,“public”,“private”,”protected”时右边是否可以编译成功?,下面两个类在不同包下,“?”分别是“default”,“public”,“private”,”protected”时右边是否可以编译成功?,package p1; public class A ? int i; ? void m() ,package p2; import p1.*; public class B extends A public void ml() System.out.println(i); m(); public static void main(String args) ,练习:构造方法,代码(a)的输出是什么;指出代码(b)的编译错误,并修改。,public class AmbiguousOverloading public static void main(String args) System.out.println(max(1, 2); public static double max(int num1, double num2) if (num1 num2) return num1; else return num2; public static double max(double num1, int num2) if (num1 num2) return num1; else return num2; ,练习:方法重载,指出代码的编译错误,并修改。,Add the static keyword in the place of ? if appropriate.,public class Test private int count; public ? void main(String args) . public ? int getCount() return count; public ? int factorial(int n) int result = 1; for (int i = 1; i = n; i+) result *= i; return result; ,练习:static关键字,1 public class Foo public static void main(String args) method1(); public void method1() method2(); public static void method2() System.out.println(What is radius + c.getRadius(); Circle c = new Circle(); ,练习:static关键字,练习:重载(overloading)vs覆盖(overriding),class Circle double radius; public boolean equals(Circle c) return this.radius = c.radius; ,public class TestEq public static void main(String args) Object c1 = new Circle(); Object c2 = new Circle(); System.out.println(c1.equals(c2); ,class Circle double radius; public boolean equals(Object c) return this.radius = (Circle)c).radius; ,True or False?,练习:static关键字/继承/多态 综合,类Test的输出是什么。,class A int i = 1; static int j = 3; public void m() i = 5; public static void m1() j = 7; ,class B extends A int i = 2; static int j = 4; /int k = 10; public void m() i = 6; public static void m1() j = 8; ,public class test public static void main (String args) B b = new B(); System.out.println(b.i); System.out.println(b.j); A a = new B(); System.out.println(a.i); System.out.println(a.j); a.m(); a.m1(); System.out.println(a.i); System.out.println(a.j); /System.out.println(B)a).i); /System.out.println(B)a).j); ,练习:instanceof和类类型转换,Fruit fruit = new GoldenDelicious() Orange orange = new Orange();,练习:抽象类,哪些抽象类的定义是正确的?,练习:接口,哪些接口的定义是正确的?,练习:接口,右边代码语句是否合法?*可以是哪些方法?,interface A void funA1(); void funA2(); ,interface B void funB1(); void funB2(); ,class CAB implements A,B void funA1() void funA2() void funB1() void funB2() ,public class testMain public static void main(String args) A a = new CAB(); / a.*(); B b = new CAB(); / b.*(); CAB cab = new CAB(); / cab.*(); B b1 (B)a; / b1.*(); CAB cab1 = (CAB)b; / cab1.*(); ,
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号