收藏 分享(赏)

course05-对象和类1.ppt

上传人:dreamzhangning 文档编号:3325375 上传时间:2018-10-13 格式:PPT 页数:51 大小:418KB
下载 相关 举报
course05-对象和类1.ppt_第1页
第1页 / 共51页
course05-对象和类1.ppt_第2页
第2页 / 共51页
course05-对象和类1.ppt_第3页
第3页 / 共51页
course05-对象和类1.ppt_第4页
第4页 / 共51页
course05-对象和类1.ppt_第5页
第5页 / 共51页
点击查看更多>>
资源描述

1、JAVA语言程序设计,第五讲 类和对象,面向对象概述 编程语言的发展 面向对象的五个基本概念 对象 (object) 消息 (message) 类 (class) 继承 (inheritance) 多态性 (polymorphism),类的定义格式 类的修饰符 class 类名 extends 父类名 implements 接口名 类的修饰符 public: Declares that the class can be used by any class regardless of its package (无任何限制) 无修饰: a class can be used only by oth

2、er classes in the same package (仅仅能在同一个包中的其他类引用) abstract: Declares that the class cannot be instantiated (宣布该类不能被实例化) final: Declares that the class cannot be subclassed (宣布该类不能有子类),类的描述,类成员的访问,* 指子类与父类在同一个包中的情况,protected (保护变量/保护方法) 容许类本身、子类(有一定限制)以及同一个包中所有类访问(先构造对象,再访问),类成员的访问,class A protected i

3、nt x;protected void print() class B void test() A a = new A();a.x = 100;a.print(); ,package abc; class A protected int x;protected void print() package xyz; import abc.A; class B extends A void test(A a, B b) a.x = 100;a.print();b.x = 100;b.print(); ,/illegal /illegal /legal /legal,方法参数,成员方法,形参和实参,D

4、:java Test 3 4 4 5 3 4 2 3,Pass by Value In Java methods, arguments are passed by value. When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type,

5、 pass-by-value means that the method cannot change the object reference, but can invoke the objects methods and modify the accessible variables within the object.,java.util.Vector类 public void addElement(Object obj),注意1: 类型匹配 注意2: 基本类型与复合类型 参数传递的结果不同,例,成员方法,u=3; v=2;,u=5; v=10;,输出结果: 3 4 5 11 12 13

6、14 15,方法的重载(overload) 方法名相同,但方法的参数不同 能不能根据方法返回值的不同实现重载? void f() int f() int x = f(); f();,成员方法,方法的重写(overriding) 子类重写父类的方法,成员方法,class Father void display( ) ; ,class Son extends Father void display() ; ,Father f = new Father(); f.display();,Son s = new Son(); s.display();,方法的递归调用(recursive) 方法对自身的调

7、用 f(n)=i=n1i=1+2+3+4+(n-1)+n 递归变换 f(n)=n+f(n-1) f(n-1)=(n-1)+f(n-2) f(1)=1,成员方法,方法的递归调用(recursive) public class Test static int f(int i) if (i = 1) return 1;else return i+f(i-1);public static void main(String args) int i = Integer.parseInt(args0);System.out.println(“累加结果:”+f(i); ,成员方法,递归方法的出口,i + f(

8、i-1),i-1 + f(i-2),i-2 + f(i-3),i-3 + f(i-4),i-4 + + 1,第六讲 对象和类(续),对象的创建 对象的使用 对象的释放 对象的访问,对象的创建,对象成员(变量和方法) 静态(static)成员: 属于类 实例成员: 属于对象 创建对象/实例化对象new 例1: Apple a = new Apple(); (创建对象) 例2: Apple a; (对象的说明)a = new Apple(); (实例化对象) 对象的实例化通过构造方法(constructor)来实现 构造方法的名字与类名相同 构造方法没有返回值 构造方法可以有多个,构成方法的重载(

9、overload),例: 对象的实例化和初始化,输出结果:20 20 50 10 20,对象的创建,再谈方法的重载(overload),对象的创建,class Tree int height;Tree() prt(“Planting a seeding”);height = 0;Tree(int i) prt(“Creating new Treethat is ” + i + “ feet tall”);height = i;,void info() prt(“Tree is ” + height +“ feet height”); void info(String s) prt(s + “:

10、 Tree is ” +height +“ feet height”); static void prt(String s) System.out.println(s); ,for (int i =0; i 5; i+) Tree t = new Tree(i);t.info();t.info(“my tree”); new Tree();,默认构造方法 例 class Apple int color; Apple a = new Apple(); 对象实例的判断: null 例 Apple a;if (a = null) System.out.println(“Day dream”);,对象

11、的创建,运行时系统自动赋予 一个空构造函数 如 Apple() ,再谈构造方法,对象的创建,class Cmethod Cmethod (boolean b) public static void main (String args) Cmethod c1 = new Cmethod();Cmethod c2 = new Cmethod(false);,class Cmethod Cmethod (boolean b) public static void main (String args) /Cmethod c1 = new Cmethod();Cmethod c2 = new Cmeth

12、od(false);,class Cmethod Cmethod (boolean b) Cmethod () public static void main (String args) Cmethod c1 = new Cmethod();Cmethod c2 = new Cmethod(false);,运行时系统自动赋予一个空构造方法, 仅仅当该类没定义构造方法的情况下,第六讲 对象和类(续),对象的创建 对象的使用 对象的释放 对象的访问,对象的使用,通过对象引用对象的成员变量和成员方法,class Qangle int a, h;Qangle () a = 10; h = 20;Qan

13、gle(int x, int y) a = x; h = y;Qangle(Qangle r) a = r.width(); h = r.height();void set(int x, int y) a=x; h =y; ,Qangle q1=new Qangle q1.set(30, 40);q1.a = 30; q1.h = 40;,目的相同 第一方式更安全、 更面向对象(数据封装)直接操纵变量会导致 未知的破坏,对象的使用,引用对象的变量 格式: 对象名.变量名 引用对象的方法 格式: 对象名.方法名 例1 Vector v = new Vector(); v.addElement(“

14、hello world”); 例2 int a= 1, 2, 3, 4, 5; int size = a.length; 例3 System.out.println();,第六讲 对象和类(续),对象的创建 对象的使用 对象的释放 对象的访问,对象的释放,将对象从内存中清除 内存的管理(枯燥、容易出错) 垃圾回收(Garbage Collection) 垃圾搜集器(Garbage Collector) 周期性地释放不再被引用的对象,自动完成 手动完成,System.gc(); public static void gc() - Runs the garbage collector.,The J

15、ava platform allows you to create as many objects as you want (limited, of course, by what your system can handle), and you dont have to worry about destroying them. The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage col

16、lection.,第六讲 对象和类(续),对象的创建 对象的使用 对象的释放 对象的访问,对象的访问,访问对象的私有(private)成员 通过定义一个公共方法来实现,class Student private String name;private String id;Student(String s1, String s2) name = s1; id = s2;String getName() return name;void setName(String s) name=s; ,Student st = new Student(“aloha”, “001”); String n = s

17、t.getName(); st.setName(“csma”); n = st.getName(); ,对象的访问,对象作为方法的参数 访问权限修饰符 方法返回类型 方法名(参数) throws 异常名方法体; 参数: 类型 变量名, 类型: 基本数据类型/复合类型(对象) 参数的传递 Pass by value,例 对象用作方法的参数,对象的访问,对象的访问,对象的访问 对象作为方法的返回值 访问权限修饰符 方法返回类型 方法名(参数) throws 异常名方法体; 返回类型 有返回值: 基本数据类型/复合类型(对象) 无返回值: void,对象的访问,对象作为方法的返回值 例: 求两点坐标

18、之间的中点坐标 思路: (x1, y1) 和(x2, y2)(x, y) x=(x1+x2)/2, y=(y1+y2)/2Spot s1 = new Spot(2, 3); Spot s2 = new Spot(4, 5); Spot s = s1.midSpot(s2);,例 对象用作方法的返回值,对象的访问,对象的访问,数组: 类型相同的一列元素 简单的数组,public class ArrayDemo public static void main(String args) int anArray = new int10; for (int i = 0; i anArray.length

19、; i+) anArrayi = i;System.out.print(anArrayi + “ “);System.out.println(); ,对象的访问,对象数组,class Test public static void main(String args) int a = new int5;for (int i = 0; i a.length; i+) System.out.println(ai);String s = new String5;for (int i = 0; i s.length; i+) System.out.println(si); ,System.out.pri

20、ntln(si.length();,对象的访问,对象数组,class Student private String name;private String id;Student (String s1, String s2) name = s1; id = s2; String getName() return name;void setName (String s) name=s; void display () System.out.println(name + “ ”+id); ,Student st = new Student10; for (int i = 0; i st.length

21、; i+) sti = new Student(); for (int i = 0; i st.length; i+) sti.display(); ,对象的访问,对象作为另一个对象的成员变量 一个对象中包含另一个对象,组合关系,class Student private String name;private String id;Student (String s1, String s2) name = s1; id = s2; String getName() return name; void setName (String s) name=s; ,对象的访问,对象作为另一个对象的成员变

22、量,class MobilePhone private String type;private Watch w;MobilePhone (String s) type = s; void setWatch(Watch a) w = a; long getTime () return w.getTime(); ,class Watch long getTime() return System.currentTimeMillis(); ,MobilePhone mp = new MobilePhone(“nokia”); Watch w = new Watch(); mp.setWatch(w);

23、 long l = mp.getTime();,public static long currentTimeMillis() the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC,对象的访问,关键词 this this指当前对象 应用1: 加强程序可读性(this可有可无),对象的访问,例1: Demo1.java (调用变量),public class Demo1 double x, y;Demo1(double i, double j) thi

24、s.x=i; this.y=j;double ave() return (x+y)/2; public static void main(String args) Demo1 d = new Demo1(3, 4);System.out.println(d.ave(); ,对象的访问,例2: Demo2.java (调用方法),class Demo2 int x, y, z;Demo2(int a, int b) x =a; y=b; this.sort(a, b); void sort(int a, int b) int t;if (x y) t=x; x=y; y=t ,对象的访问,例3:

25、 Demo3.java (调用方法),class Demo3 int x, y;Demo3(int a, int b) x =a; y=b;void change (int i, int j) x=i; y=j; this.sort();void sort() int t;if (x y) t=x; x=y; y=t ,对象的访问,关键词 this this指当前对象 应用2: 对同一个对象执行多次方法调用,class Leaf int i = 0;Leaf increment() i+;return this;void print() System.out.println(“i=” + i)

26、; ,Leaf x = new Leaf(); x.increment().increment().increment().print();,int i=10;,对象的访问,关键词 this this指当前对象 应用3: 在一个构造函数中调用另一个构造函数,class Flower String name;int price;Flower () this(“tulip“, 10);Flower (String s, int i) name = s;price = i;,void print () System.out.println(name+ “ “ + price);public stat

27、ic void main(Stringargs)Flower f = new Flower();f.print();,内部类(inner class),内部类(inner class)是存在于一类中的类。它是Java语言1.1版后的一种强大且优异的功能。内部类亦有人称为嵌套类(nested classes)。内部类有4种: . 静态成员类 (static member class)指在一类中被定义且用static修饰的类(也可是接口),其地位就像是一个类方法一般,可存取所在之类的静态成员(包括静态字段,静态方法)。因其地位存在于内部顶层的位置,故又称内部顶层类(nested top-level

28、 classes)。接口只能被定义成类中的一种静态成员,而不能为非静态成员。 . 成员类 (member class)指在一类中被定义的类,但不是用static修饰的类,其地位就像是一个实例方法一般,可存取所在之类的所有字段与方法。,. 局部类 (local class)指存在于一程序区块里的内部类。其地位就像局部变量一样,只在该区块内被使用。但接口不能被定义成局部类。 . 匿名类 (anonymous class)指没有名称的局部类。常用于事件处理时,简化事件倾听者及事件处理者的声明。接口同样不能被定义成这种类。,内部类在类中直接定义的内部类,内部类可以直接访问内部它的类的成员,包括priv

29、ate成员,但是内部类的成员却不能被内部它的类直接访问。 在内部类对象保存了一个对外部类对象的引用,当内部类的成员方法中访问某一变量时,如果在该方法和内部类中都没有定义过这个变量,内部类中对this的引用会被传递给那个外部类对象的引用。,内部类在类中直接定义的内部类,如果用static修饰一个内部类,这个类就相当于是一个外部定义的类,所以static的内部类中可声明static成员,但是,非static的内部类中的成员是不能声明为static的。static的内部类不能再使用外层封装类的非static的成员变量,这个道理不难想象!所以static内部类很少使用。,内部类 在类中直接定义的内部类

30、,内部类如何被外部引用,class Outerprivate int size=10;public class Innerpublic void doStuff()System.out.println(+size); public class TestInnerpublic static void main( String args)Outer outer = new Outer();Outer.Inner inner = outer.new Inner();inner.doStuff();,内部类在方法中定义的内部类,内部类并非只能在类里定义,也可以在几个程序块的范围之内定义内部类。例如,在

31、方法中,或甚至在for循环体内部,都可以定义内部类 。 在方法中定义的内部类只能访问方法中的final类型的局部变量,用final定义的局部变量相当于是一个常量,它的生命周期超出方法运行的生命周期。,11.3.5内部类和匿名类,为了简化事件源和监听器之间的关系,java的事件处理中引入和使用了内部类(Inner Class)和匿名类(Anonymous Class),内部类是被定义于另一个类中的类,即类的嵌套。由于内部类可以访问外部类的成员方法和变量,包括私有的成员。所有实现监听器时,采用内部类,匿名类变成非常容易实现其功能。 下面我们通过例子来体会通过内部类和主类两种方法实现监听器的不同之处

32、。,11.3.5内部类和匿名类,比较两种实现方法,首先,用主类做接收器,由于实现了多个监听器接口,所以各个接口中的方法,全部要在主类中实现,多种方法堆砌在一起,加大了程序阅读和维护的难度。相反,用内部类和匿名类做接收器,每个接收器就是一个独立的类,不仅阅读方便,并且由于减少了同主类的耦合性,从而使代码的重用性大大提高了。其次,观察第二种实现方法里的actionPerformed方法,由于有多个组件都可能发生actionEvent事件,所有在处理前首先要判断传递过来事件的事件源是哪一个组件,显然在程序的执行效率上,同第一种方法也是有差距的。通过比较,我们认为在多个方面第一种方法都要优于第二种,所

33、以用内部类和匿名类来进行事件处理是我们推荐的好的实现事件处理的方法。,案例,/InnerClass.java import java.awt.*; import java.awt.event.*; public class InnerClass extends FrameButton b1=new Button(“Button 1“);Button b2=new Button(“Button 2“);public InnerClass() setLayout(new FlowLayout();b1.addActionListener(new b1L();b2.addActionListener(new b2L();add(b1); add(b2); setSize(200,200);setVisible(true);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(1););/用匿名类实现窗口关闭 。,第六讲 结束 !,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报