Java is an object-oriented language, Java program is a collection of a series of objects (Object), collaborative completion of a specific function calls to each other by means of the object.
Object-oriented acquaintance
Object-oriented programming is a very in line with the human mind, because the real world is the interaction between objects and objects made of, so we are actually very easy to map to real-world software development.
A car, a blog, a person, corresponds to the software system is an object; and the object has its own state and behavior.
Car, for example:
- A car can have fuel consumption, color, wheelbase, speed and other state property
- A car may have started running, acceleration and deceleration, braking and other methods
class
Class template describes the state and behavior of a class object. It can be thought of as a class automotive design, one example of this car design produced every car is like, these examples it is called an object.
public class Car { int Color; // member variable int Speed; // member variables // member methods void Startup () { the System. OUT .println ( " Start! " ); } // member method void RUN ( int Speed) { . the System OUT .println ( " my speed " + speed); } }
Another example we have to do a blog application, a blog then each have similar status and behavior, then we can define a Post
class as a template of the blog.
public class Post { String title; // member variable String Content; // member variables // member methods void Print () { the System. OUT .println (title); . the System OUT .println (Content); } }
public
It is a modifier to indicate external access to this class.
Post
State and behavior are reflected on the internal member variables and member methods defined.
Member variables
The state of an object by the value of a member variable of the decision.
For example, every car has its own color and speed, so we Car
define two member variables classes, usually we call attributes:
int color; int speed;
Member variables are data types, color
and speed
all int
types, namely an integer.
Similarly, each one has its own blog title and content, so we Post
define two classes of member variables:
String title;
String content;
title
And content
both String
types of properties, i.e., is represented by a string.
Member method
The method defines the behavior of the class, a class can have many methods, the process may write logic, manipulating data, perform certain actions. Sometimes referred to as a function of the method.
A relatively independent calculation process (i.e., a certain behavior of the object), is accomplished by a method, when needed to reuse such a process, the program can be made finer so lucid.
For example, Car
there is a name for the startup()
method, indicate the car starts:
void Startup () { System.out.println ( "Start ! "); }
This method of printing a single line of text to the console.
Post
Class provides a method to print the contents of the blog:
void print() { System.out.println(title); System.out.println(content); }
print()
A method Post
of title
and content
progressive printed.
startup()
And print()
the method is not a return value and parameters of the method. Without a return value, use void
representation.
Creating and Using Objects
To HelloWorld example, the definition of a good Post
after class, you can main
create and use the Post
object.
public class the HelloWorld { public static void main (String [] args) { Post POST = new new Post (); // create a blog objects post.title = "blog"; // access object properties post.content = "first blog "; // access object properties post.print (); // call the object method } }
You need to type an object is declared Post
, and name, code named post
( post
that is, an identifier); then need to use the new
keyword to create Post
objects.
Created here post
variable is a local variable within the function, you can also name any other legal identifier, such as:
Post myPost = new Post();
Variables are generally begin with a lowercase, using the hump named .
Local variables and member variables, it does not belong to an object, it is a temporary variable, when the method has finished executing, the variable will no longer work. Variables declared in a method belongs to a local variable.
Now have a Post
target, you can access its properties and methods, use .
the connection object names and property / method names:
-
post.title = "博客"
Ofpost
objecttitle
attributes assigned. -
post.print()
This line of code calls thepost
object'sprint()
function.
Run the program may see the following output:
Blog
first blog
Packaging (package)
During development, the number of classes will be more and more, you can package (Package) to organize the class. Naming packages generally the domain of an organization's anti writing at the beginning .