Base-oriented (summary of knowledge points)

Source: Internet
Author: User
Tags modifiers

Object-Oriented foundation object-oriented features are: encapsulation, inheritance, and polymorphism

Object-oriented Design trilogy:

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

2) Discover the properties of the class, the properties of the class that the static characteristics owned by the object are represented in the class

3) Methods of discovering classes, actions performed by objects called Methods of classes

1. Define the syntax format of the class: [access modifier] class name {};

The access modifier has public and private, class name attributes of the first uppercase class: Properties of objects that are owned by an object are called classes when they are represented in a class

2. The syntax format for defining attributes is as follows: [access modifier] Data type property name;

An access modifier is an optional way for an object to perform an action called a class method, eg people have a way of working, work is a method of "human" class, "People" class there are many ways, such as eating and drinking sleep and so on

3. Syntax format for defining methods: [Access modifier] Return type method name (parameter type argument name 1, argument type parameter name 2) {}

Analysis: The a\ access modifier is optional b\ The return type can be void, when the method is defined, the return type is void when there is no return value, the method does not have to return the specific data with return, but you can use the return keyword to exit the c\ return type if it is not void, Then be sure to return the result of the corresponding type using return, or the program will have a compilation error d\ "parameter type argument name 1, parameter type argument Name 2 ..." is called parameter list "e\ when you need to pass arguments to the method when the method executes, the argument list is required. Omit if no arguments are required, but parentheses cannot be omitted, and when multiple arguments are passed, they are separated by a comma of half-width

Creating and Using objects

Class can call members in a class, such as properties and methods.

1. Create the syntax format of the object: Class Name Object name =new class name (); A\new is the data type of the class name of the key b\ to the left of the object c\ the right class name is the class's a-constructor method 2 uses the object, the properties and methods to refer to the object are ".", using the syntax format of the object:

The object name. Property//Reference object's properties; method ()//method that refers to the object

3. Object-Oriented Advantages:

1) The same as human thinking habits: the object-oriented thinking mode is from the perspective of human considerations, the human problem-solving process of thinking to the process can be understood, object-oriented programming using "class" to simulate the real world of abstract concepts, with "objects" to simulate real-world entities, So as to solve the real problem by computer

2) information hiding, improve the maintainability and security of the program: encapsulation realizes the modularization and information hiding, the property and behavior of the class are encapsulated in the class, which guarantees the modification of them and does not affect other objects, which is conducive to maintenance. At the same time, encapsulation makes it impossible to access the object's properties and methods outside the object, which avoids the influence of external errors and improves security.

3) to improve the reusability of the program: A class can create multiple object instances, reflecting the reusability of

Class members include: Member methods and member variables

Member Methods

A method with parameters

Formal parameters and arguments, arguments are actual arguments, and the actual parameters passed in when invoking a method are called actual parameters, referred to as arguments

Method overloading refers to defining multiple methods with the same name in a class, but requiring each method to have different parameter types and number of arguments

Features of method overloading:

1) Same as 2) method name in the same class

3 The number of parameters or the return value of different methods of the type can not be used as the basis for determining whether the method is overloaded.

The advantages of method overloading: The method overload in fact, the original method of an upgrade, can be different according to the parameters, the implementation of different methods, and do not need to write multiple names, simplifying the code of the class invocation method

Member variables

A property of a class, that is, a variable defined directly in a class, is called a member variable, and it is defined as a variable in the method that is defined in the external local variable of the method.

The difference between a member variable and a local variable:

1 scopes differ, local variables are limited to the method that defines it, and cannot be accessed outside of the method. Member variables are visible throughout the class. It can be used by all member methods, and member variables can be used outside of the class if access permission is allowed

2 The initial value is different, for a member variable, if it is not given an initial value in the class definition, Java gives it a default value, the base data type has a value of 0, and the value of the reference data is null. However, Java does not give the local variable the initial value, so the local variable must be defined and assigned to use the

3 in the same method, local variables with the same name are not allowed, and in different methods, local variables of the same name can be

4 Local variables can have the same name as member variables, and when used, local variables have higher precedence

There are three types of reference data: classes, arrays, and interfaces

The difference between a base data type (or when passing a parameter) and a reference data type (or pass a parameter):

For basic data types, different variables are assigned to different storage spaces, and the value of the variable is stored in the storage space, the value of the variable is passed by the assignment operation, and changing the value of one variable does not affect the value of the other variable for reference data type, which assigns a reference (memory address) to the original object. Passed to another reference. In the case of arrays, when a reference to an array name assigns a value directly to another array name, it is equivalent to passing a reference, at which point the two references refer to the same array, that is, to the same memory space

Construction method: When the class creates an object, the constructor of the class is automatically called, and the construction method is divided into the default construction method and the construction method with the parameter

What is a construction method? Syntax format: [access modifier] Method name ([parameter list]) {}

1, the construction method does not return a value of 2, the default constructor method has no parameters, so the parameter list is optional 3, the method name of the construction method and the same class name

The main function of the construction method is to do some initialization of the data

Constructs a method overload, that is, the construction method that can define multiple overloads in the same class

The This keyword is a default reference to an object that has a this reference variable within each instance method, pointing to the object that called the method

This uses the following example:

1 calling member variables, resolving conflicts of member variables and local variables with the same name

2 Calling Member methods

3 Call the overloaded constructor method, which can only be used in the construction method, and must be the first statement of the constructor method

4this can only invoke instance variables, instance methods, and construction methods

5 You cannot call class variables and class methods, and you cannot call local variables

The essence of encapsulation is to hide the state information of the class inside the class, not to allow the external program to access directly, but to implement the operation and access to the hidden information through the methods provided by the class.

Encapsulation effectively avoids the effect of external errors on this object, and can prevent the object users from the error actions caused by the effect, and the same object-oriented programming advocates the loose coupling between the objects. The advantage of encapsulation is to hide the implementation details of the class, so that the user can only access the data through the method prescribed by the programmer, it is convenient to join the storage control to restrict unreasonable operation.

Loosely coupled means minimizing the association between objects to reduce the complexity and dependencies between them

Steps for encapsulation:

First step: Modify the visibility of the property (when public is modified to private, the other classes are inaccessible, and if access requires a second step in encapsulation)

Step two: Set the Setter/getter () method, which is added by the system with Source-setter/getter or Ctrl+shift+s

The third part: Set the access limit of the attribute, set the legality check for the attribute value, and need to use the conditional judgment statement to make the assignment restriction in setter method.

Package mechanism is also a form of encapsulation

It has three main functions: A package allows you to combine classes into the smallest unit (like a folder), which is easy to find and use the appropriate class file. Second, to prevent naming conflicts, only classes in different packages in Java can be renamed. Class naming is inevitable, with the package, the class name is easy to manage. A defines a class sort, encapsulated in package A, B defines a class sort, encapsulated in package B. The sort that is defined separately by a and B by the package name area, such as A.sort and B.sort, correspond to the sort class defined by A and B, respectively. Three packs allow the protection of classes, data, and methods in a broader context. Depending on the access rules, code outside the package may not be able to access the class

The syntax format for defining packages is as follows: Package name

1. Package is the key word

2. The package declaration must be the first non-annotative statement in the Java source file, and the source file can have only one package declaration statement, and the package needs to correspond to the file system structure.

Package naming adheres to the following coding specifications:

1, a unique package name prefix is usually all lowercase ascii letters, and is a top-level domain name com\edu\gov\net and org, usually using the organization's network domain name in reverse order. Eg: if the domain name is javagroup.net, you can declare the package as: "Packages net.javagroup.mypackage;"

2. The following parts of the package name vary according to the rules of each organization

The meaning of the declaration package is life? The package in which the current class resides; The meaning of the import package is to declare the package that contains the other classes to be used in the current class

Access control for classes and class members 1, the access modifier of the class has public and default modifiers, the difference between the default modifier is not in the same package can be used 2, the access modifier of the class member private, the default modifier, the protected, and the public difference:

1) Private can only be used in the same class, not in the same package, in subclasses, and elsewhere

2) Default modifiers are available in the same class and in the same package, and are not available in subclasses and elsewhere

3) protected can be used in the same class, in the same package, in subclasses, and not in other places

4) public can be used in the same class, in the same package, in subclasses, and elsewhere

3. Modifying a property with the static keyword, called a static variable or a class variable, without a property that uses the static modifier

4, using the Static keyword modification method

Methods that are modified with static are called Static or class methods, and methods that are not decorated with the static keyword are called instance methods or are called member methods

The difference between static and non-static:

A property in static is called a class or static variable, a method called a class method or a static method, a property load order: A class that executes when it is loaded, the method loading order: It is executed when called, belongs to: Class, call: Object name and operator, can modify properties, methods, and code blocks A property in non-static is called a member or instance variable, which is called a member method or an instance method, and the property is loaded in order: the Creation object (constructor method) is executed, the method is loaded in order: it is executed when called, belongs to: Object, call: Object name and operator

Base-oriented (summary of knowledge points)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.