The initial knowledge of Java object-oriented

Source: Internet
Author: User
Tags define abstract instance method

I've always wanted to write something. Practice your own writing, today write this technical type of article also does not have a good format and the composition, but the beginning of everything is difficult, then starts from the object-oriented.

Most of us know that the existence of Internet software, always affect our real life, then the object-oriented programming idea is from the life, the software appears to describe the real world, so as to solve the real life needs, the point: The human mind depicts the real world, with programming language to achieve, and object-oriented is the most consistent with the way people think. Different things in the world by classification to distinguish, there are instances of the object to act as a special case, the program through the same class and object to one by one describe clearly

Let's start with the concept of object-oriented: Class {properties/Methods}, object, initialization code block, permission modifier, feature (inheritance, polymorphism, encapsulation), other related (abstract, interface, Singleton pattern ...). In fact, this is also the 3 characteristics of the relevant), this idea throughout the Java programming language, understanding and later slowly in practice to understand.

One: Classes and objects

(1) Class loading order: program code at any time there is an execution order, a Java file from being loaded into the unloading of this life process, a total of 5 stages, the JVM will be divided into the class loading process: 1
Load, link (validate + prepare + Parse) initialization (pre-use preparation), use, uninstall. But what we need to know is that the static code executes the initialization order: (static variable, static initialization block) –> (variable, initialization block) –> constructor, if there is a parent class, the order is: the parent class static method –> the subclass static method –> The parent class construction Method-- > Sub-Class construction methods.

(2) attribute: There are two main concepts: member variables (some variables that modify static are also called member variables, but I generally make the variables here as global variables, because the sharing time of his attribute data with the shared scope and class, is the cow) and local variables. Consider these two existing values from a practical standpoint, for example: a person with attributes: height, age, gender, etc., and he was born with a jersey 11th in a basketball game, so he got a new attribute value in this movement, and the value of the property disappears as the game ends. The former can be defined as member variables, the latter can be defined only in the basketball playing the properties, playing the function of basketball can be written as an example of such a method, when he would like to play basketball is also able to get the number 11th shirt to complete the behavior method. An example explains the member variables, local variables, and instance methods. Because the scope of the use is not the same, the member variables are sent in the memory heap (and so on after the class has been loaded), the local variables in the memory stack (the end of the method to the inside of the local variables completely empty, not waiting for the class to load out).

(3) Method: The above content has already explained one kind, also has the supplement. A class method (static modified method, with the static properties of the class loaded at the same time, because the load time is earlier than the instance method and has been with the class exists, there are two kinds of invocation situation: "A" later invoked instance method can write static method, class method cannot directly use class method, The reason is that it has already been loaded and can not be loaded again by the class, only to be called. "B" in the static method can only be called by the new object to call other class methods, I think that the object reference does not need to load any of the functions of the execution, introduced to the object can be introduced to the construction method.

(4) constructor: When creating a non-parametric object, the class will have a default constructor method, it is important to note that when the new object is not only its own definition of a method of construction, you should also add the default construction method manually. Role: ① Create an object ② assign a value to the property of the created object (the order in which the property of the class object is assigned: the default initialization of the ① property, the explicit initialization of the ② property ③ the initialization of the property by the constructor, and the assignment of the property by the "object. Method" way. The assignment can be four. But it is convenient and efficient to give him a value when creating objects for external use.

2:3 Major Features

(1) Encapsulation: The concept is very few, a word: The property is hidden, the public method provides its access (when developing, remember that attributes are used to store data, are accessed directly, are prone to security risks, so the properties in the class are usually privatized, and public access methods are provided externally. /private is only encapsulated? A form of manifestation only). General benefits: segregation of changes; easy to use; high reusability; security.

Encapsulation implements code reuse in a number of ways:

A. Array? For encapsulating data, that is, a specific entity;

B. The problem with the entity is that it is used to encapsulate multiple data

C. What is an object? The object is to put some content such as functions into the line of encapsulation.

D. Multi-program read? When a configuration file is configured, the recommended configuration? file is encapsulated as an object.

E. Internal classes are defined internally for encapsulation. To get an inner class object is usually obtained through the external class's? method. This allows the inner class object to be controlled.

F. Exceptions are Java's object-oriented thinking that encapsulates the problem. This will facilitate the operation of the problem and deal with the problem.

G. Package is a form of encapsulation, which is used to encapsulate a class and wants to be accessed by a program other than the package, the class must be public.

H. Encapsulation of a String object. The advantage of this is that it can be easily manipulated by the constant data of the string. After the object is encapsulated, you can define N multiple properties and behaviors.

I. Basic data type Object wrapper class: Is the basic data type encapsulated as an object-oriented idea. Benefit: 1: Basic data can be manipulated through the properties and behaviors in the object. 2: The conversion between the basic data type and the string can be implemented

J.dateformat df = new SimpleDateFormat (); The object's built-in interior will be encapsulated? A default? date format. 11-12-1 1:48

K. Because the character is not the same in every country, so it involves the character encoding problem, then GBK encoded in the text? Parsing with Unicode encoding is problematic, so it is necessary to obtain the data of the text section and the specified encoding table to parse the correct data. To facilitate the parsing of text, the byte stream and the encoded table are encapsulated into objects, which are character streams. As long as you manipulate character data, you prioritize the use of a character stream system.

L. Reflective technology: It's actually dynamic loading? a specified class and gets all the contents of the class. And the bytecode file is encapsulated as an object, and the contents of the bytecode file are encapsulated into objects.

  I have already listed a lot, it is worth noting that 23 design patterns of thought will have a lot of packaging ideas, you can also write some packaging code for their own projects to avoid duplication of time wasted. Apt to say an example, no encapsulation is a person to move each piece of brick, have after can create their own can play brick container, once used to continue to use it to complete the same thing, also known as the package of tools.

Hidden: Permission modifier (Public private protect default; Package/import will be used)

(2) Inheritance: Single-Inheritance and multi-interface implementation of the perfect match so that we write programs more flexible, in order to extend the implementation of the function has a child to inherit the parent page inheritance (extends) relationship, there will be high coupling of blood relationship, finally using the interface, found that the ability to decouple the development interface is not enough, Hope can be divided into independent modules to complete but they are still connected to each other high cohesion can not be reduced, and now I and colleagues to use the spring framework to complete the project, efficiency is very little involved in code.

Come back and say, 1. Abstract classes are inherited and can be created, note that there is at least one abstract method that is non-private, and you can use the main method. It is usually inherited and sub-class to be implemented. 2. Interface (interface) can only define abstract methods and constants, the implementation of its subclasses must implement all of its methods, extensibility is diverse than single inheritance good, generally preferred.

(3) Polymorphism: A reference to a parent class or interface that points to its own subclass object. Animal a = new Cat (). The disadvantage of polymorphism: when a parent class reference points to a subclass object, it improves extensibility, but only accesses the methods that are available in the parent class, and cannot access the methods that are unique to the. class. (You cannot use late-generation functionality, which is the limitation of access). Polymorphism is a change in thought: Before an object can command only one object to work, it is now possible to use a reference method that can invoke multiple objects.

Every time I want to write more perfect blog posts, but also to gain, but the study may be limited in vision, so I hope you can point out my shortcomings.

  

  

  

The initial knowledge of Java object-oriented

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.