Java Language Programming (18) Theoretical Definition of Objects and Classes

1. Define classes for objects
  1. Classes define properties and behaviors for objects.
  2. Object-oriented programming (OOP) is the use of objects for programming. An object represents an entity that can be clearly identified in the real world. For example: a student, a table, a circle or even a loan can be regarded as an object. Each object has its own unique identity, state and behavior.
      The state (state, also called property and attribute) of an object is represented by a data field with current values. An object's behavior (behavior, also called action) is defined by methods. One way to call an object is to ask the object to complete an action.
  3. Use a generic class to define objects of the same type. A class is a template, blueprint, or contract that defines what the data field of the object is and what the method does. An object is an instance of a class. You can create multiple instances from a class. The process of creating an instance is called instantiation. Object and instance are often interchangeable.
  4. Java classes use variables to define data fields and methods to define actions. In addition, the class also provides a special type of method called the constructor (constructor), calling it to create a new object. The construction method itself can complete any action, but the construction method is designed to complete the initialization action, for example: initialize the data field of the object field.

  2. Use the constructor to construct the object
  1. The constructor is called when the new operator is used to create the object.
  2. The construction method is a special method. They have the following three peculiarities: The
constructor must have the same name as the class it is in. The
constructor has no return value type, and even void has no
constructor. It is called when an object is created using the new operator. The role of the constructor is to initialize the object.
  3. Usually, a mine will provide a construction method without parameters (for example: Circle()). Such a method of construction is called a no-arg or no-argument constructor.
  4. A class may not define a construction method. In this case, the class implicitly defines a no-argument constructor with an empty method body. This constructor is called the default constructor, and it is automatically provided if and only if there is no clear definition of any constructor in the class.
  3.
      The data and methods of accessing objects through reference variables can be accessed through the reference variables of the object using the dot operator (.).
  4. Reference variables and reference types
  1. On the surface, the object reference variable seems to store an object, but in fact, it only contains a reference to the object. Strictly speaking, object reference variables and objects are different, but in too many cases, this difference can be ignored. Therefore, you can simply say that myCircle is a Circle object, instead of verbosely describing that myCircle is a variable that contains a reference to the Circle object.
  2. In Java, arrays are regarded as objects. The array is created with the new operator. An array variable is actually a variable that contains an array reference.
   5. Access to the data and methods of the object
  In object-oriented programming, object members can refer to the object's data fields and methods. After an object is created, its data and methods can be accessed and invoked using the dot operator (.), which is also known as object member access operator:
objectRefVar.dataField refers to the data field of the object
objectRefVar.method(arguments) calls the method of the object
  6. Reference data fields and null values
  ture and false are direct variables of boolean type, and null is direct variables of reference type.
    7. The difference between a basic type variable and a reference type variable
  Each variable represents a memory location where a value is stored. When declaring a variable, you are telling the compiler what type of value the variable can store. For basic type variables, the value stored in the corresponding memory is the basic type value. For reference type variables, the value stored in the corresponding memory is a reference, which is the storage address of the object.
  8. Use the classes in the Java library
  8.1 The Date class
  can use the non-tragic construction method in the Date class to create an instance for the current date and time, and its getTime() method returns since GMT time January 1, 1970. For the elapsed time, its toString() method returns a string of date and time.
  8.2 Random class
  You can use Math.random() to get a random double value between 0.0 and 1.0 (not including 1.0).
  8.3 Static variables, constants and methods
  1. Static variables are shared by all objects in the class. Static methods cannot access instance members in the class.
  2. If you want all instances of a class to share data, you must use static variables, also known as class variables.
  3. Instance method can call instance method and static method, and access instance data domain or static data domain. Static methods can call static methods and access static data fields. However, static methods cannot call instance methods or access instance data fields, because static methods and static data fields do not belong to a specific object.
  8.4 Visibility modifier, data encapsulation, method passed to object parameters
  Visibility modifier can be used to determine the visibility of a class and its members. You can use the public modifier in front of the class, method, and data field to indicate that they can be accessed by any other class. If there is no visibility modifier, the default class, method, and data field can be used by any class in the same package This is called package private or in-package access.

      We can pass an object to a method, just like passing an array, passing an object is actually passing a reference to an object. Java has only one parameter passing method, which is value passing. In the next article, we will use a large number of examples to study objects and class


Guess you like

Origin blog.51cto.com/15064656/2602761