CPT204-Advanced OO Programming:Objects and Classes 对象与类

发布于:2025-06-19 ⋅ 阅读:(16) ⋅ 点赞:(0)

目录

1.对象 Object

1.2统一建模语言(UML)

1.2.1UML:数据字段封装 Data Field Encapsulation

 1.3访问对象 Accessing Objects

1.4基本数据类型变量 vs对象类型变量 Variables of Primitive Data Types vs Object Types

1.5垃圾回收Garbage Collection

2.类 Classes 

2.1介绍

2.2构造器Constructors 

2.3静态变量与非静态变量 Static vs. Non-static variables

2.4静态与非静态方法Static vs. Non-static methods 

2.5.变量默认值:

2.6 Date类

2.7 Random类

2.8访问修饰符和访问器/修改器方法Visibility Modifiers and Accessor/Mutator Methods

2.8.1修饰符 Pmodifiers


1.对象 Object

1.1介绍

- 一个对象代表了现实世界中可以明确识别的实体,它与具有共同属性的对象类/模板不同。

An object represents an entity in the real world that can be distinctly identified from a class/templates of objects with common properties.

- 对象具有独特的状态和行为:An object has a unique state and behavior

        ·对象的状态由一组数据字段(属性)及其当前值组成。the state of an object consists of a set of data fields (properties) with their current values

        ·对象的行为由一组实例方法定义。the behavior of an object is defined by a set of instance methods 

1.2统一建模语言(UML)

- 统一建模语言(UML)是软件工程领域的一种通用建模语言,旨在为面向对象系统的可视化设计提供一种标准方式。The Unified Modeling Language (UML) is a general-purpose modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a object-oriented system.

 

1.2.1UML:数据字段封装 Data Field Encapsulation

- 对象数组Array of Objects

        ·对象数组是引用变量的数组(类似于之前看到过的多维数组) An array of objects is an array of reference variables (like the multi-dimensional arrays seen before) 

                >Circle[] circleArray = new Circle[10];

                >circleArray[0] = new Circle();

                >circleArray[1] = new Circle(5);

  

 1.3访问对象 Accessing Objects

- 引用对象的数据Referencing the object’s data:

        objectRefVar.data
        ·eg:myCircle.radius

- 调用对象的方法Invoking the object’s method:

        objectRefVar.methodName(arguments)
         ·eg:myCircle.getArea()

1.4基本数据类型变量 vs对象类型变量 Variables of Primitive Data Types vs Object Types

- 区别

-  复制

   

1.5垃圾回收Garbage Collection

- 之前由c1引用的对象不再被引用,它被称为垃圾。 

The object previously referenced by c1 is no longer referenced, it is called garbage

- 垃圾会被JVM自动回收,这个过程叫做垃圾回收。 

Garbage is automatically collected by the JVM, a process called garbage collection

- 在一些较早的语言中,如C和C++,人们必须显式地释放/删除不再使用的数据/对象。

In older languages, like C and C++, one had to explicitly deallocate/delete unused data/objects

2.类 Classes 

2.1介绍

- 在Java中,类是定义相同类型对象的模板 In Java classes are templates that define objects of the same type

- Java类使用:

        ·非静态/实例变量来定义数据字段 non-static/instance variables to define data fields

        ·非静态/实例方法来定义行为 non-static/instance methods to define behaviors

- 一个类提供了一种特殊类型的方法,称为构造函数,它被用来根据类构造对象。A class provides a special type of methods called constructors which are invoked to construct objects from the class

- 示例:

2.2构造器Constructors 

- 构造器的名称必须与类名相同。Constructors must have the same name as the class itself.
- 构造器没有返回类型——甚至连void也没有。Constructors do not have a return type—not even void.
- 构造器在创建对象时通过new运算符被调用——它们将对象初始化为引用变量:Constructors are invoked using the new operator when an object is created – they initialize objects to reference variables
                                      ​​​​​​​        ClassName o = new ClassName( );
一个类可以不声明构造器:如果类中没有显式声明构造器,编译器会隐式地为类声明一个无参的默认构造器,其方法体为空。A class may be declared without constructors: a no-arg default constructor with an empty body is implicitly declared in the class

2.3静态变量与非静态变量 Static vs. Non-static variables

- 静态变量和常量:Static variables and constants

        ·静态变量是整个类的全局变量,适用于该类的所有对象实例。global variables for the entire class: for all objects instances of this class 

        ·静态变量被该类的所有实例共享 Static variables are shared by all the instances of the class:


        ·例如:
          static int count = 0;
          static final double PI = 3.141592;

- 非静态变量/实例变量是对象的数据字段:Non-static/instance variables are date fields of objects
        ·例如:
          System.out.println(myCircle.radius);
          System.out.println(yourCircle.radius);

2.4静态与非静态方法Static vs. Non-static methods 

- 静态方法:Static methods

        ·被该类的所有实例共享,不与特定对象绑定。Shared by all the instances of the class - not tied to a specific object
        ·eg:double d = Math.pow(3, 2);

- 非静态方法/实例方法 Non-static/instance methods

        ·必须通过类的对象实例调用:must be invoked from an object instance of the class
        ·eg: double d1 = myCircle.getArea();
              double d2 = yourCircle.getArea();

2.5.变量默认值:

- Java不会为方法内部的局部变量分配默认值。Java assigns no default value to a local variable inside a method. 

- 数据字段有默认值(类中的全局变量)Data fields have default values

  

2.6 Date类

- 方法:

+Date() 

        构造一个表示当前时间的Date对象 Constructs a Date object for the current time

+Date(elapseTime: long) 

        构造一个表示自1970年1月1日(GMT)以来经过的毫秒数的Date对象 Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT.

+toString(): String 

        返回表示日期和时间的字符串Returns a string representing the date and time.

+getTime(): long 

        返回自1970年1月1日(GMT)以来的毫秒数 Returns the number of milliseconds since January 1, 1970, GMT.

+setTime(elapseTime: long): void

        设置对象中的新经过时间。1970年1月1日(GMT)被称为Unix时间或Unix纪元时间。Sets a new elapse time in the object. January 1, 1970, GMT is called the Unix time (or Unix epoch time)

- 声明方式:

        java.util.Date date = new java.util.Date(); 

        System.out.println(date.toString()); 

2.7 Random类

- 方法 :

+Random() 

        构造一个以当前时间为种子的Random对象 Constructs a Random object with the current time as its seed

+Random(seed: long) 

        构造一个以指定值作为种子的Random对象。种子值用于确保生成的随机数序列是可重复的。Constructs a Random object with a specified seed.

+nextInt(): int 

        返回一个随机的int值。Returns a random int value.

+nextInt(n: int): int 

        返回一个介于0和n(不包括n)之间的随机int值 Returns a random int value between 0 and n (exclusive).

+nextLong(): long 

        返回一个随机的long值Returns a random long value.

+nextDouble(): double 

        返回一个介于0.0和1.0(不包括1.0)之间的随机double值 Returns a random double value between 0.0 and 1.0 (exclusive).

+nextFloat(): float 

        返回一个介于0.0F和1.0F(不包括1.0F)之间的随机float值 Returns a random float value between 0.0F and 1.0F (exclusive).

+nextBoolean(): boolean

        返回一个随机的boolean值。Returns a random boolean value.

- 声明方式:

        Random random1 = new Random(3); 

        for (int i = 0; i < 10; i++) 

        System.out.print(random1.nextInt(1000) + " ");

2.8访问修饰符和访问器/修改器方法Visibility Modifiers and Accessor/Mutator Methods

- 默认情况下,类、变量或方法可以被同一包内的任何类访问。By default, the class, variable, or method can be accessed by any class in the same package.

        ·public(UML中用+表示) 

                >类、数据或方法是对任何包内的任何类可见的。The class, data, or method is visible to any class in any package.

        ·private(UML中用-表示) 

                >数据或方法只能被声明它的类访问 - 用于保护数据!The data or methods can be accessed only by the declaring class - To protect data! 

- getField(访问器)和setField(修改器)方法用于读取和修改私有属性。 getField (accessors) and setField (mutators) methods are used to read and modify private properties.

2.8.1修饰符 Pmodifiers

private修饰符限制访问权限仅限于类内部The private modifier restricts access to within a class

默认修饰符(即不加任何修饰符)限制访问权限仅限于包内部。The default modifier restricts access to within a package

public修饰符——无限制访问。public – unrestricted access


网站公告

今日签到

点亮在社区的每一天
去签到