Java foundation ------ (2) object-oriented

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 Postclass 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); 
    } 
}

publicIt is a modifier to indicate external access to this class.

PostState 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 Cardefine two member variables classes, usually we call attributes:

int color;
int speed;

Member variables are data types, colorand speedall inttypes, namely an integer.

Similarly, each one has its own blog title and content, so we Postdefine two classes of member variables:

String title;
String content;

titleAnd contentboth Stringtypes 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, Carthere 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.

PostClass provides a method to print the contents of the blog:

void print() {
    System.out.println(title);
    System.out.println(content);
}

print()A method Postof titleand contentprogressive printed.

startup()And print()the method is not a return value and parameters of the method. Without a return value, use voidrepresentation.

Creating and Using Objects

To HelloWorld example, the definition of a good Postafter class, you can maincreate and use the Postobject.

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( postthat is, an identifier); then need to use the newkeyword to create Postobjects.

Created here postvariable 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 Posttarget, you can access its properties and methods, use .the connection object names and property / method names:

  • post.title = "博客"Of postobject titleattributes assigned.

  • post.print()This line of code calls the postobject's print()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 .

Guess you like

Origin www.cnblogs.com/mliu/p/11320011.html