对象和类解析
2
What is Object(什么是对象)
An object represents an entity in the real world that can be distinctly identified.(对象代表现实世界中可以明确标识的一个实 体)
For example, a student, a desk, a circle, a button can all be viewed as objects. All the objects share two characteristics(所有对象都有两方面 的特性): State(状态) Behavior (行为)
3
对象的状态-变量
每个对象的每个属性都拥有特定值,例如:王小红 和朱小雨的体重不一样
姓名:王小红
收银员小王
体重:60kg 年龄:35岁
4
对象的行为—方法
方法——对象执行的操作,描述对象的行为
收银 收银员王小红 打单
方法
刷卡
5
What is Object?
A software object maintains its state in
Objectives(学习目标)
To understand objects and classes(理解对象和类) To learn how to declare a class and how to create an object of a class.(学习如何定义类和创建类的对象) To understand the roles of constructors and use constructors to initialize objects.(理解构造方法扮演的角色 ,会使用构造方法初始化对象) To distinguish between object reference variables and primitive data type variables(区分引用变量和基本数据类型 变量) To declare private data fields with appropriate get and set methods to make class easy to maintain (声明私有成员变量 ,并为变量提供get和set方法)
8
Member variable 成员变量 Daod
Constructors(构造方法)
Constructors are a special kind of methods that are invoked to initialize objects.(构造方法是一类特殊的方法,用来初始化新对 象) class Circle{ double radius = 1; Circle() { } Circle(double newRadius) { radius = newRadius; } //… }
one or more variables (member variable/ data fields/ properties). (软件上的对象使 用变量来存储状态)
A software object implements its
behavior with methods. (软件上的对象使 用方法来实现它的行为)
9
Constructors(构造方法)
Constructors must have the same name as the class itself. (构造方法的名称必须和类名相同) Constructors do not have a return type—not even void.( 构造方法没有返回类型,连void也没有) Constructors are invoked using the new operator when an object is created. (构造方法在创建对象时用new关键字调用) Constructors play the role of initializing objects.(构造方法 的作用是初始化新对象) A constructor with no parameters is referred to as a no-arg constructor. (没有参数的构造方法称为无参构造方法)
1
Objectives(学习目标)
To use the keyword this as the reference to the current object that invokes the instance method(会使用关键字this作为当前对 象的引用) To understand the difference between instance and static variables and methods .(理解静态变量和实例变量,静态方法和 实例方法的区别) To develop methods with object arguments (能创建以对象作为 参数的方法) To store and process objects in arrays(会处理对象数组) To understand the access level to class and members.(理解Java 类、成员的访问级别)
6
类
顾客类 轿车类 收银员类 …
“人”类 特征(状态、属性) 姓名 性别 年龄 体重 行为(方法) 衣 食 住 行 小布什 普京
类概括了同类对象共有的性质:属性和方法 类是模板,比如说:“人” 对象是类的一个实例,比如:“小布什”
克林顿 ……
7
创建类
class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; } }