IBM HZETP IBM Learning Knowledge 20201030 2009 IBM
IBM HZ-ETP 面向对象设计思想 ——为什么要使用面向对象 彭鹏 IBM Learning & Knowledge 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Agenda § 函数 §类 § 对象 § 构造函数 2 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 函数的重载 § 函数的重载就是在同一个类中允许同时存在一个以上的同名函数,只要它们的参数 个数或类型不同即可。 如: public class Test { public static void main(String [] args){ int isum; double fsum; isum=add(3, 5); isum=add(3, 5, 6); fsum=add(3. 2, 6. 5); } public static int add(int x, int y) { reutrn x+y; } public static int add(int x, int y, int z) { return x+y+z; } public static double add(double x, double y) { return x+y; } } 5 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Practice 2 § 求下面代码输出结果 public class ETPTest 2_2 { private void change(int x) { x = 3; System. out. println(x); } public static void main(String[] args) { int x = 5; ETPTest 2_2 test 2_2 = new ETPTest 2_2(); test 2_2. change(x); System. out. println(x); } } 7 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Practice 3 § 求下面代码输入结果 public class Main { public void change(Obj obj) { obj. x = 3; } public static void main(String[] args) { Main main = new Main(); Obj obj = new Obj(); System. out. println(obj. x); main. change(obj); System. out. println(obj. x); } } class Obj { int x = 5; } 8 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Practice 4 § private float get. Var(){return x; } § 以下哪些可以重载上面这条语句 § A. float get. Var() § B. public float get. Var() § C. public double get. Var() § D. protected float get. Var() § E. public float get. Var(float f) 9 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Practice 5 § 下面这个类声明了静态变量,并且使用了静态代码块初始化变量 § public class Static. Class { § private static int i; § static { § i = 3; § } § public static void main(String[] args) { § System. out. print(Static. Class. i); § } § 请创建一个类, 使用静态代码块初始化并打印出来看是否生效 15 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 下一章节 § 对象和类 16 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP JAVA中类的声明(续) § 例: public abstract class shape{…………} class rectangle extends shape{…………} final class roundrect extends rectangle {…………………. } ? ? final可以与abstract一起使用吗? 19 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 成员变量的声明(续) § 例: class shape{ private int x; private int y; static final float Max. Area=100. 0 f; …………. . } 22 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 方法的声明(续) § 例: class shape{ private int x; static float Max. Area=100. 0 f; public void setx(int newx){x=newx; } public static void set. MA(float new. MA){ Max. Area=new. MA; } public final getx( ){return x} } 26 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 下一章节 § 对象 27 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 对象的产生 Person p 1 = new Person(); 执行完后的内存状态 28 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 对象的使用 创建新的对象之后, 我们就可以使用“ 对象名. 对象成员 ”的格式,来访问对 象的成员(包括属性和方法) class Test. Person { public static void main(String[] args) { Person p 1 = new Person(); Person p 2 =new Person(); p 1. age = -30; p 1. shout(); p 2. shout(); } } 程序运行的内存布局如下图 30 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 下一章节 § 构造函数 37 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 下一章节 § 继承 45 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 面向对象的概念——子类 § 子类是作为另一个类的扩充或修正而定义的一个类。 bicycle (super class) sub class Mountain bikes 46 面向�象��思想 Racing bikes 2020/10/30 Tandem bikes © 2009 IBM Corporation
IBM HZ-ETP 面向对象的概念——继承 § 继承使得子类可以利 用父类中定义的方法 和变量,就像它们属 于子类本身一样。 class Employee { protected String name; public void show. Info() { System. out. println(name); } } class Manager extends Employee { private String department; public void show. Info() { System. out. println(name); System. out. println(“Manager of ” + } department); } 47 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 继承的例子 49 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 面向对象的概念——覆盖 § 覆盖使得在子类中可以 重新定义父类中已有的 方法,从而是子类具有 自己的行为。 class Employee { protected String name; public void show. Info() { System. out. println(name); } } class Manager extends Employee { private String department; public void show. Info() { System. out. println(name); System. out. println(“Manager of ” + } department); } 52 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP 下一章节 § 多态 58 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Assignment 2 § 实现以下的类 § to. String() 为返回所有内容。 § 写个测试类 Test. Employee 62 面向�象��思想 2020/10/30 © 2009 IBM Corporation
IBM HZ-ETP Assignment 3 § 在之前的基础上,增加红色部分的内容。 § to. String() 为返回所有内容。 § get. Monthly. Pay() 为抽象方法。 § Sales. Employee. get. Monthly. Pay() – monthly. Pay = salary + (perfit * 20%) § Manager. get. Monthly. Pay() – monthly. Pay = salary + bonus § 写个测试类 Test. Employee 63 面向�象��思想 2020/10/30 © 2009 IBM Corporation
- Slides: 63