Java Object-oriented (top)

Source: Internet
Author: User

I. Object-oriented Concepts
  1. Process oriented
Analyze the steps required to resolve the problem, and then use the function to implement each of these steps, using a single call
  2. Object-oriented
The solved problem is divided into several independent objects according to some rules, and the method of calling object is used to solve the problem, and the function of the program is realized through the cooperation of multiple objects.

two. Classes and Objects
   1. Concept of Class
A common feature and behavior used to describe a set of objects, which is the blueprint for an object
   2. Introduction to Classes
class contains member variables and member methods
   (1) member variables
Features used to describe objects, also known as attributes, are written outside the method body and are divided into static variables (class variables) with static modifiers and instance variables without static modification
   (2) member method
The behavior used to describe an object, also known as a method, contains a method body and a local variable. A local variable can have the same name as a member variable.
   3. Creation and use of objects
   (1) Create an object
Class name Object name;//Create app variable
New person ();//instantiation of a class
Object name =new person ();//Create Link
     * Connected together is:
Class Name Object name =new class name ();
such as: Person pr=new person ();
* objects are placed on garbage collection objects, and when no variables refer to this object it becomes garbage object is recycled
   (2) Invocation of methods and properties
Object reference. Method name () or. Attribute;
such as: Pr.ag;
Pr.print ();
   4. Encapsulation of Classes
   (1) Concept
is to hide some information about a class inside a class, not to allow direct access by external programs, but to manipulate and access hidden information through the methods provided by the class.
(2) Benefits
Can only access data through the prescribed method, the implementation details of hidden classes, easy to join control statements, easy to modify the implementation
(3) Use
Use private to decorate the methods and properties that you want to encapsulate
Modified and accessed by the setxxx () and accessor GetXXX (), the setup and accessors cannot be decorated with private

Three. Construction method
  1. Definition
The method name is the same as the class name, and the method name does not precede the declaration of a value type, and the method cannot return a value with return
 2. Role  

Used only to initialize the object, if you do not define the system will customize a non-parametric construction method, once you define the system will not be in the custom
 3. Overloading of construction methods
Multiple construction methods can be defined in a class, but the parameter types or the number of parameters of a method are different

Four. This and static keywords
  1.this usage
(1) through this. Member variable, you can go to the member variable of a class, solve the problem with the local variable name conflict
(2) Calling member methods through this
(3) Use this () to invoke other constructor methods
(4) objects in this class (not commonly used)
  2.static
    Statically decorated with the classloader of the class created, and can only be created once, non-static methods can call static variables, static methods cannot invoke instance variables
No specific data is generated when defining a class, and the system allocates memory space only when an object is instantiated with the New keyword

(1) Static variables
Sometimes we want certain data to be in memory only, and can be shared by each object we need static
Variable is a member variable that is modified with static cannot be used to decorate a local variable

  (2) static method
A method that can be called without creating an object, using the static decoration method
  (3) Static code block
static{}

Five. Inner class
  1. member Inner class
He can access the properties and methods of the external class,
The outer class name. Internal class name Variable name =new external class name (). New internal class name ();

2. Static Inner class

Modifies a member's inner class with static, which can be instantiated when no external class object is created

3. Method Inner Class

Can only be used in the current method

Java Object-oriented (top)

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.