English Version of Java Study - Lesson 2

Object-Oriented

I. Basic Concept of Object-Oriented

 A. What is an object

  1. In Java, everything is an object.

  2. The world is made of objects. 

 B. What is a class

  1. Class is used to describe the common characteristics of objects in the objective world.

  2. Object is the concrete existence of class.

  3. Class is a set of objects with the same attributes and methods.

  4. There are objects before classes, which conforms to the rule that people know things.

 C. The connection between an Object and a class

  1. Classes are templates for objects, and objects are instances of classes.

  2. NOTE: A class can have multiple objects. 

II. Creat a Class

  1. Discover classes, which define the characteristics (attributes) and behaviors (methods) that an object will have.

  2. Discover the attributes of a class, and the static features of an object in the class represent the attributes called classes.

  3. The method of discovering classes. The operations performed by objects are called the method of classes.

 A. Access modifier

  

  

 B. Definition class

  1. Access modifiers such as public, private, etc. are optional.

  2. Class is the keyword for declaring classes.

  3. Class names are capitalized according to naming conventions.

 C. Attribute

  1. Access modifiers are optional

  2. In addition to accessing modifiers, other grammars are similar to declaring variables.

 D. Methods

  1. Access modifiers are optional

  2.The return type can be void. When defining a method, the return type is void, indicating that there is no return value. The return keyword is not necessary to return specific data in the body of the method, but the return keyword can be used to exit the method.

  3. If the return type is not "void", then the method body must use the "return" keyword to return the results of the corresponding type, otherwise the program will have compilation errors.

  4. In parentheses, "parameter type parameter name 1, parameter type parameter name 2,..." is called parameter list.

  5. When you need to pass parameters to a method when it is executed, you need a parameter list. If you don't need to pass parameters, you can omit them, but parentheses can't be omitted. When you pass multiple parameters, you divide them into commas at half corners.

III. Creat and Use the Object

 A. Create a class

  1. "new" is the key word.

  2. The class on the left is called the data type of the object.

  3. The class name () on the right is called the construction method of the class.

  

 B. Use the object

  1. Method:

   

  2. Object-oriented has the following advantages: 

   - Object-oriented programming uses "classes" to simulate abstract concepts in the real world.

   - Information hiding improves the maintainability and security of programs

   - At the same time, the encapsulation makes it impossible to access the object's attributes and methods freely outside the object, avoids the influence of external errors on it, and improves the security.

   - A class can create multiple object instances, which reflects reusability.

IV. The Definition of a Class

 A. Membership variables

  1. Membership variables

   a. The attributes defined in the class, that is, the variables directly defined in the class, are called member variables, which define the exterior of the method.

  2. Local variables

    

  3. Differences between member variables and local variables

   - Scope is different: the scope of a local variable is limited to the method that defines it, and it cannot be accessed outside of that method. 

   - Initial values are different: For member variables, Java will give them a default value if they are not given an initial value in the class definition. 

   - In the same method, local variables with the same name are not allowed. In different methods, local variables with the same name can be used.

   - Local variables can have the same name as member variables, and when used, local variables have higher priority.

 B. Membership methods

  1. Method with parameters

   - Note: The parameters defined when creating the work () method are called formal parameters, which are abbreviated as formal parameters.

   - The parameters passed in when calling a method are called actual parameters, referred to as arguments.

  2. Overload

   - In the same class.

   - The method name is the same.

   - The number or type of parameters are different.

   - The return value of the method can not be used as a basis for judging whether the overload between the methods is constituted.

 C. Construction methods

  1. Definition of construction methods

   - The main function of the construction method is to initialize some data.

   - Constructor has no return value

   - The default constructor has no parameters, so the list of parameters is optional.

   - The method name of the construction method is the same as the class name.

  2. Overload of construction method

   - Constructors can also be overloaded, i.e. multiple overloaded constructors can be defined in the same class.

  3. "this" as a keyword

   - This keyword is the default reference to an object.

   - Inside each instance method, there is a this reference variable that points to the object calling the method.

V. Encapsulation

 A. Understanding the encapsulation

  - Hidden class implementation details

  - Let users access data only through pre-scheduled methods, so that control logic can be added to the method, restricting unreasonable access to member variables.

  - Data checking can be carried out to ensure the integrity of object information.

  - Easy to modify and improve the maintainability of code

  - In order to achieve good packaging, we need to consider these two aspects.

  - Hide member variables and implementation details of objects and do not allow direct external access

  - Expose methods to allow methods to control secure access and manipulation of these member variables

  - Therefore, encapsulation actually has two meanings: hiding the hidden and exposing the exposed. Both aspects need to be implemented by using access modifiers provided by Java.

 B. Using the encapsulation

  - Visibility of modified attributes

  - Setting a common sette/getter () method

Setting Access Restrictions for Attributes

 C. About "package"

  1. Summary about package

   - Java introduces package mechanism, which provides multi-layer namespace for classes to solve the naming conflicts of columns and class file management.

   - There are files that are easy to find and use.

  2. Definition of package

   - A unique package name, usually prefixed by all lowercase letters, and a top-level domain name such as com, cn, etc.

   - The subsequent parts of the contract vary according to the internal norms of different agencies.

   - Such naming conventions may distinguish between departments, projects, machines or registry names by the composition of specific directory names, such as: Package java.util;

 D. "static" as a keyword

  1. Modifier variable

   - Static-modified class variables, known as static variables (also known as class variables), have only one in memory.

   - Within a class, static variables can be accessed directly in any method, and in other classes, directly by class name.

   - It can be shared by all instances of a class and can be used as shared data for communication between instances.

   - If all instances of a class contain the same constant property, this property can be defined as a static constant type, thus saving memory space.

  2. Modifier methods

   - Static-modified methods can not use this and super.

   - Can not directly access instance variables and instance methods of their classes.

   - Can directly access static variables and static methods of classes (also known as class methods).

   - Can be accessed directly through the class name. 

    

猜你喜欢

转载自www.cnblogs.com/study-java-with-english-syq/p/English-Version-of-Java-Study-Lesson-2.html