Java program development and learning classes and objects

Java program development and learning classes and objects

(Study reference book: Java2 practical tutorial fifth edition)

1. Three characteristics of object-oriented programming

  1. Encapsulation: The core idea of ​​object-oriented programming is that objects encapsulate their own data and operations on these data reasonably and effectively.
  2. Inheritance: The subclass can inherit the properties and functions of the parent class, that is, inherit the data and operations on the data owned by the parent class, and at the same time can increase the unique data and operations on the subclass.
  3. Polymorphism: The first is the polymorphism of the operation name, and different messages are passed to the operation so that the object can produce certain behaviors according to the corresponding message; the second is the polymorphism related to inheritance, and the same operation may be called by different objects Produce different behaviors.

Two, class declaration and class body

A class is the basic element of a Java program, encapsulating the state and methods of a class of objects. A class is a template used to define objects, and objects can be created. When an object is created using a class, we also say that an instance of this class is given. The class consists of two parts: the class declaration and the class body.

clss 类名{
    
    
	类体的内容;
}

Among them are class 类名called class declarations.

Three, the composition of the class body

The content of the class body has two: member variables and methods;
member variables : used to describe the properties of the objects created by the class. The type of member variable can be any data type in Java, and it is valid in the entire class, regardless of where it is written in the class body. You can assign initial values ​​when defining member variables.

Method : The construction method is used for class creation and is used to give the initial state of the object created by the class. Other methods are called by the object created by the class. The object calls these methods to manipulate member variables to form a certain algorithm, which reflects a certain behavior of the object.

Four, construction method

A part of the class is called the construction method. The construction method is used when the class creates an object in order to give the object created by the class a reasonable initial state.

  • The construction method is a special method, its name must be exactly the same as the name of the class it is in, and there is no type.
  • If there is no construction method written in the class, Java will provide a default construction method without parameters; if a construction method is written, no default construction method is provided.
  • Java allows several construction methods in a class, but the number or types of parameters of these construction methods must be different.

Five, object creation

The creation of an object is divided into two steps: declaration and assignment of member variables.
(1) Object declaration, format:类的名字 对象名字;

(2) Matching member variables for the
declared object The declared object does not store data, and the entity must be assigned to the object, that is, the member variable must be assigned.
Generally use the new operator combined with the class constructor to assign member variables to the declared object. If there is no constructor in the class, the system will call the default constructor (the default constructor has no parameters, remember that the name of the constructor must be the same as the class name )

The format of creating an object: 对象 = new 构造方法名(即是类名)(构造方法参数);
the declaration of the object and the assignment of member variables can be completed in one step:

类名 对象名 = new 构造方法名(构造方法参数);

(3) Create multiple different objects.
A class can create multiple different objects by using the operator new. These objects will be allocated different memory spaces. Therefore, changing the state of one object will not affect the state of other objects.

(4) Using objects
Objects can not only manipulate their own variables to change state, but also have the ability to use the methods of the class that created it. Objects can produce certain behaviors by using these methods.
Through the operator "." , the object can access its own variables and call methods.
Object properties: The object manipulates its own variables.
Object behavior: The object calls methods in the class.

6. Object references and entities

When an object is created with a class, member variables are allocated memory space. These memory spaces are called entities or variables of the object, and references are stored in the object to ensure that these variables are used by the object operation. Therefore, if two objects have the same reference, they have the same entity.

Java's garbage collection mechanism: The operating environment periodically checks whether an entity is no longer referenced by any object. If such an entity is found, the memory occupied by the entity is released.

An object without an entity is called an empty object, and an empty object can no longer call methods to produce behavior.

Seven, member variables

The variables defined inside the class but outside the method are called member variables.

(1) Class variables and instance variables
Member variables are used to describe the properties of the created object. Some of the member variables are called instance variables, and the other part of the member variables modified with the keyword static are called static variables or class variables.

All objects share class variables, even if a class does not create objects, class variables will still allocate memory space.

Class variables can be accessed not only through an object, but also directly through the class name. (But instance variables can only be accessed through objects)

(2) Constants The member variables modified
with the keyword final are called constants. Constant names are used to use capital letters.
Final constants do not take up memory space. They must be initialized when declared. Objects can be manipulated but cannot be changed.

Eight, local variables

The variables and method parameters declared in the method body are called local variables.

  • Local variables are only valid within the method, and are only valid from the place where it is declared.
  • If the name of the local variable is the same as the member variable, then the member variable is hidden, that is, the member variable is temporarily invalid in this method.
  • If you want to use hidden member variables in the method, you must use the keyword this格式:this.变量名
  • Member variables have default values, but local variables do not have default values. You must ensure that local variables have specific values.

Nine, parameter transfer value

(1) The transfer of basic data types
can only be in the direction of unchanged or reduced accuracy. That is, low-precision parameters cannot be passed to high-precision variables.

(2) Reference
data of reference type Java by value includes arrays, objects, and interfaces. They pass the reference stored in the variable, not the entity referenced.

(3) Variable parameter
refers to the name and number of parameters from a certain item to the last item in the list that does not give parameters when declaring a method, but the types of these parameters must be exactly the same. Variable parameters use "..." to indicate several parameters, these parameters must be of the same type, and the last parameter must be the last parameter in the parameter list of the method . For example: void f(int… x) indicates that the parameters from the first to the last are of type int, but the number of int types that appear continuously is uncertain. It is said that x is the variable parameter in the parameter list of method f. The parameter represents".
Thus was born an enhanced for statement:

for(声明循环变量:参数代表){
    
    
	循环体;
}

10. Combination of objects

A class can use an object as its own member variable. If you create an object with such a class, there will be other objects in this object, which is what people often call Has-A.

If an object a combines object b, then a can delegate b to call its methods.

11. Instance methods and class methods

(1) Instance method: no keyword static modification

数据类型 方法名{
    
    
	方法体内的语句;
}

(2) Class method: modified with the keyword static

static 数据类型 方法名{
    
    
	方法体内的语句;
} 

  • Instance methods can call instance methods or class methods in this class, but class methods can only call class methods of this class.
  • Instance methods can manipulate member variables (class variables and instance variables), and class methods can only manipulate class variables.
  • Instance methods must be called by objects, and class methods can be called by class names (also by objects).

12. Method overloading

Method overloading refers to the polymorphism of behavior. The specific meaning is: There can be multiple methods in a class with the same name, but the parameters of these methods must be different, that is, the number of parameters or the type of a parameter are different. These methods with the same name and different parameters produce different behaviors . But overloaded methods are prone to ambiguous calls when they are called.

Thirteen, this keyword

The this keyword can appear in instance methods and construction methods, but cannot appear in class methods .
(1) Appears in the construction method: represents the object created by the construction method.
(2) Appears in the instance method: represents the current object that is calling the method. When an instance member variable appears in an instance method, the default format is: this. member variable; when a class variable appears in an instance method, the default format is: class name. member variable. But usually it can be omitted. (For special cases, see 8, local variables)

14. Package

The purpose of the package is to effectively distinguish situations where the names of two classes in different Java source files are the same, that is, they are distinguished by belonging to different packages.
(1) Package statement
The package statement is declared through the keyword package . The package statement is the first statement in the Java source file. It indicates that the package format of the class defined by the source file is: package 包名;
(2) Run the main class
with the package name To run the main class with the package name, you need to use the format:包名.主类名

15. Import statement

Let a class use a class that is not in the same package as it.
Java provides a class library. To use the classes in the class library, you must use the import statement to import the classes and interfaces in the package. In a Java statement, there can be multiple import statements, which must be written between the package statement and the class definition of the source file .
If you import all classes in a package, you can use wildcards (*) instead: import 包名.*;
if you import a certain class in a package:import 包名.类名;

Common packages provided by Java:
java.lang: contains all the basic language classes and is the core class library of the Java language;
javax.swing: contains images, text, and window GUI classes in the abstract window tool set;
java.io: contains all input and output classes;
classes in java.util: comprising utility class;
the java.sql: operation of the database containing the class;
the java.net: contains all the class implements the network functions;

16. Access authority

1) Private variables and private methods Member variables and methods modified
with the keyword private are called private variables and private methods. If class A declares private variables and methods, objects created with class A in class B cannot access private variables and private methods of A.

(2) Shared variables and shared methods Member variables and methods modified
with the keyword public are called shared variables and shared methods. If class A declares shared variables and methods, then objects created by class B with class A can access their own shared variables and methods.

(3) Friendly variables and friendly methods Member variables and methods that are
not modified by the public, protected, and private keywords are called friendly variables and friendly methods. If the member variables and methods of class A are friendly, create an object with class A in class B. If class A and class B are in the same package, then the object can access friendly variables and methods.

(4) Protected member variables and methods The member variables and methods modified
with the keyword protected are called protected member variables and methods. If class A declares protected member variables and methods, objects created in class B with class A. If the class A and the class are in the same package , then the object can access the protected member variables and methods. If it is not in a package, but through inheritance, it is possible to make the object accessible.

(5) Public class and friendly class In
class declaration, if public is added before class, then such a class is called a public class. If not, it is friendly. The public class can create objects in any other class, but the friendly class must be in the same package.

17. Encapsulation of basic data types

In java.lang , classes related to basic data types are provided to realize the encapsulation of basic data types. They are:Byte、Interger、Short、Long、Float、Double、Character。

Eighteen, object array

As the name implies, an object array is a collection of several objects of the same type combined in order. The object after the object array is declared is only the empty object after the declaration is completed. Need to be created to use. E.g:

Student stu[] = new Student[10];
for(int i=0;i<10;i++) {
    
    
	stu[i] =new Student();
}

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112688728