java语言程序设计(基础篇)第七章.pptVIP

  • 21
  • 0
  • 约1.67万字
  • 约 100页
  • 2018-03-28 发布于河南
  • 举报
java语言程序设计(基础篇)第七章

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 第7章 对象和类 在前一部分(2到6章),我们学习了编程基础,学会使用基本数据类型、控制语句、方法以及数组,这些都是所有的面向过程的语言都具有的特征,但, Java,是个面向对象的语言,不但具有面向过程语言的要素,也具有抽象、封装、继承、多态等特征以实现强大的灵活性、模块化、以及可重用性以开发软件,在这一部分,我们将学习如何定义、扩展以及使用类与对象 Objectives To understand objects and classes and use classes to model objects (§7.2). To learn how to declare a class and how to create an object of a class (§7.3). To understand the roles of constructors and use constructors to create objects (§7.3). To use UML graphical notations to describe classes and objects (§7.3). To distinguish between object reference variables and primitive data type variables (§7.4). To use classes in the Java library (§7.5). To declare private data fields with appropriate get and set methods to make class easy to maintain (§7.6-7.8). To develop methods with object arguments (§7.9). To understand the difference between instance and static variables and methods (§7.10). To determine the scope of variables in the context of a class (§7.11). To use the keyword this as the reference to the current object that invokes the instance method (§7.12). To store and process objects in arrays (§7.13). To apply class abstraction to develop software (§7.14). To declare inner classes (§7.17 Optional). 面向对象编程的概念 (OO Programming Concepts) 7.2. 为对象定义类 例如一个圆对象(circle),有数据域圆半径(radius)(标识了圆的属性),圆的行为就是其面积可以通过方法getArea计算而得。同类型的对象使用一个公共的类来定义。类就是定义对象的数据和方法的模板.。一个对象是类的实例(instance)。你可以生成一个类的很多个实例,产生一个实例也叫做实例化(instantiation).术语对象、实例通常可互用,类和对象的关系类似于书版和从书版印刷出很多的书。 下面是个圆的例子。 对象 Classes 类 UML类图(UML Class Diagram) 7.3 构造子 Circle() { } Circle(double newRadius) { radius = newRadius; } 构造子是特殊的方法,其特殊性: 使用构造子生成对象 new ClassName(); Example: new Circle(); new Circle(5.0); 默认构造子 注意 有个常见错误,在构造子之前加上void关键词作为返回类型。 如:public void Circle() { } 则 Circle() 就不是构造子了。 7.4. 通过引用变量存取对象 新产生的对象保存在内存中,如何存取之呢? 对象通过对象引用变量(指向对象的引用)来存取的 声明对象引用变量 要引用一个对象,要把此对象赋予一个引用变量. 声明引用变量,语法: ClassName

文档评论(0)

1亿VIP精品文档

相关文档