1.7 Application Program Interface(API) and Libraries

7 阅读1分钟

1. Exam Points

  • Identify attributes (instance variables) and behaviors(methods).
  • Identify the correct data types for instance variables.
  • Identify a class and an instance.

2. Knowledge Points

(1) Class and Object

  • A class defines common attributes and behaviors of objects.
  • An object is a specific instance of a class with defined attributes.
  • Example:
    • Student is a class
    • Xiaoming is an object/instance of the Student class
  • Attributes refer to the data related to the class and are stored in variables.
  • Behaviors refer to what instances of the class can do (or what can be done with them) and are defined by methods.
  • Example:
    public class Dog{
        private String name;
        private int age;
        
        public void bark(){}
        public void bite(){}
    }
    
    // here is a code segment in another class
    Dog d1 = new Dog();
    Dog d2 = new Dog();
    
    • In this example:
      • Dog is a class
      • d1, d2 are two objects/instances of the Dog class.
      • name and age are instance variables(实例变量), representing attributes of Dog objects.
      • bark and bite are methods (方法), representing behaviors of Dog objects.

(2) Libraries and APIs

  • Libraries are collections of classes.
  • Classes are grouped into packages.
  • An application programming interface (API) specification informs the programmer how to use those classes.
  • Documentation found in API specifications is essential to understanding the attributes and behaviors of a class defined by the API.

3. Exercises