slug
type
status
category
date
summary
tags
password
icon

Topic 1.7: Application Program Interface (API) and Libraries


🔍 LEARNING OBJECTIVE: 1.7.A

Identify the attributes and behaviors of a class found in the libraries contained in an API.

📘 Essential Knowledge

✅ 1.7.A.1 – What is an API and Library?

  • Libraries are collections of pre-written classes (code structures) that programmers can use to build software faster and more efficiently.
  • An API (Application Programming Interface) is the specification or documentation that tells programmers how to use those libraries—what classes are available, what they do, how to use them, and what parameters they require.
  • These classes are grouped into packages (e.g., java.util, java.io in Java).
  • You can use these classes to create objects and call their methods without writing all the underlying code yourself.

✅ 1.7.A.2 – Attributes and Behaviors:

  • Attributes: These are data fields or variables associated with a class or object. They define the state of an object.
  • Behaviors: These are defined by methods (functions) that an object of the class can perform or that can be performed on the object.

🧾 Examples (in Java)

Example 1: String class from the Java Standard Library

Library/Package: java.lang
  • Attributes (not directly visible): The characters inside the String object (e.g., "Alice").
  • Behaviors (Methods):
    • .length() → returns the number of characters in the string.
    • .toUpperCase() → returns a new string with all uppercase letters.
The API documentation for String tells us about all the methods available, like .substring(), .charAt(), and others.

Example 2: ArrayList class from the java.util package

  • Attributes:
    • The underlying list of elements (e.g., ["Jane"]).
  • Behaviors (Methods):
    • .add() → adds an element to the list.
    • .remove() → removes an element.
    • .size() → returns the number of elements.
The ArrayList API documentation explains what each method does, what parameters it needs, and what it returns.

📌 Why This Matters

  • Understanding how to read and use an API helps you avoid "reinventing the wheel."
  • You can efficiently build programs using tested and optimized library classes.
  • Knowing the attributes and behaviors helps you predict what a class does and how to interact with it.

 
以下是对 Topic 1.7:API(应用程序编程接口)与类库(Libraries) 的中文复述:

🧠 学习目标:1.7.A

识别 API 所包含类中的属性(Attributes)与行为(Behaviors)。

📚 基础知识

✅ 1.7.A.1 —— 什么是 API 和类库?

  • 类库(Library) 是一组已经写好的 类(Classes),程序员可以直接使用这些类来加速开发,不必从零开始编写所有功能。
  • API(应用程序编程接口) 是一种 说明文档,它告诉程序员如何使用这些类库。API 包含类的定义、方法说明、参数要求等。
  • 这些类通常被组织在不同的 包(Package) 中(如 Java 中的 java.util, java.io)。
  • 使用类库时,可以直接 创建对象,并调用这些对象的方法来实现功能。

✅ 1.7.A.2 —— 属性与行为的含义

  • 属性(Attributes):类或对象中储存的数据,通常由变量表示。例如,一个学生类可能包含 name, age 等属性。
  • 行为(Behaviors):类或对象可以执行的动作,由 方法(Methods) 实现。例如,一个字符串对象可以调用 .toUpperCase() 方法变为大写。

📘 例子(以 Java 为例)

例子 1:String 类(字符串类)

所属包java.lang(Java 默认导入的包)
  • 属性:字符串中包含的字符内容(如 "Alice")。
  • 行为(方法):
    • .length():返回字符串的长度。
    • .toUpperCase():返回大写形式的新字符串。
这些方法的用法都可以在 Java 官方的 API 文档中查到。

例子 2:ArrayList

所属包java.util
  • 属性:内部存储的数据列表(例如:["Jane"])。
  • 行为(方法):
    • .add():向列表中添加元素。
    • .remove():移除指定元素。
    • .size():返回列表中元素的数量。
使用 ArrayList 时,API 会清楚地告诉你每个方法的功能、参数和返回值。

为什么这很重要?

  • 掌握 API 的使用,可以 提升编程效率,利用已有代码实现复杂功能。
  • 了解类的属性和行为,可以帮助你快速判断一个类能做什么,以及你可以如何与它交互。
  • 阅读 API 文档是现代编程的重要技能。

 
Learn Java with Fun File handling in Java
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
Oxford
2025-6-2
AP CSA 期末考试
2025-5-28
An introduction to AP CSA (2026)
2025-5-27
Learn Java with Fun
2025-5-27
Cracking CSA:Inheritance, Superclasses, and Subclasses(旧大纲)
2025-5-27
Cracking CSA:Inheritance and Polymorphism(旧大纲)
2025-5-27
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---