Advanced Java from entry to the road (six)

Previous article, we introduced the Java array chapter we look at Java objects and classes.

Java is an object-oriented language, what is it an object, the object belongs to a very broad concept in a programming language, we can say that everything is an object, each object has its own state and behavior, such as a dog will color, breed, sex and other states, but also running, eating, sleeping and other behavior.

In programming languages, will be used to carry the object class, the class is a template that describes the state and behavior of a class of objects.

In the previous article we do not introduce the concept of the object, if we want to print out a person's basic information, as follows:

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          String name = "zhangsan" ;
 . 4          int Age = 18 is ;
 . 5          String Gender = "M" ;
 . 6          Print (name, Age, Gender); / / name: zhangsan Age: 18 sex: Male 
. 7      }
 . 8  
. 9      static  void Print (String name, int Age, gender String) {
 10          of System.out.print ( "name:" + name);
 . 11          of System.out.print ( "Age:" + Age);
12 is          of System.out.print ( "Gender:" + Gender);
 13 is      }
 14 }

In fact, the above is a process-oriented programming, we first define a print method, were passed in the name, age, sex, and then were printed output, we can define variables, age, gender, calls the print methods to achieve what we want function.

There is a problem is when we need the output of another person's information again, we need the name, age, gender incoming again, in fact, we can define an object to be used in these three variables, as follows:

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Emp = E new new Emp (); // Create a target Emp 
. 4          e.name = "zhangsan" ;
 . 5          e.age = 18 is ;
 . 6          E .gender = "M" ;
 . 7          Print (E); // name: zhangsan Age: 18 sex: Male 
. 8      }
 . 9  
10      static  void Print (Emp E) {
 . 11          of System.out.print ( "name:" + E. name);
 12 is         System.out.print("年龄:"+ e.age);
13         System.out.print("性别:"+ e.gender);
14     }
15 }
16 
17 class Emp{
18     String name;
19     int age;
20     String gender;
21 }

In the above code, we will process incoming print variable becomes a target Emp, Emp object was defined ourselves out of it before and we are talking about Array is actually a reason:   arr = new new int [5 ];  , 5 generates a length of the array.

In the above code, the method Print role is in fact we define for outputting print information we need, we also can be placed Emp class we define as follows:

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Emp = E new new Emp (); // Create a target Emp 
. 4          e.name = "zhangsan" ;
 . 5          e.age = 18 is ;
 . 6          E .gender = "M" ;
 . 7          e.print (); // name: zhangsan Age: 18 sex: Male 
. 8      }
 . 9  }
 10  
. 11  class Emp {
 12 is      String name;
 13 is      int Age;
 14     Gender String;
 15  
16      void Print () {
 . 17          of System.out.print ( "Name:" + name);
 18 is          of System.out.print ( "Age:" + Age);
 . 19          of System.out.print ( "Gender: "+ Gender);
 20 is      }
 21 is }

At this point we actually had fruit object-oriented thinking defines a class, when we need to define another person's information, direct calls to the properties and methods of the object on it.

We begin to learn a programming language, when are slowly beginning the process-oriented depth to the object-oriented, process-oriented but by a number of disadvantages, as follows:

1, the lack of data package

2, the separation of data and methods

As the above example the beginning of what we need to fight cents own data definition, data name age gende and print methods are separated, then we changed to a unified Emp class, so that you achieve a unified data and methods.

 

We next look at the following code:

. 1  public  class the HelloWorld {
 2      public  static  void main (String [] args) {
 . 3          Emp = E new new Emp (); // Create a Emp objects 
. 4          e.print (); // Name: null Age: 0 Sex: null 
. 5      }
 . 6  }
 . 7  
. 8  class Emp {
 . 9      String name;
 10      int Age;
 . 11      String Gender;
 12 is  
13 is      void Print () {
 14          of System.out.print ( "name:" + name);
 15         System.out.print ( "Age:" + Age);
 16          System.out.print ( "Gender:" + Gender);
 . 17      }
 18 is }

After you create the Emp object, we do not name age class and gender assignment, direct calls print () method, the result of a compilation error did not occur, because we define a variable when, java will provide one of us the default value, the string is null, the number is 0.

 

Guess you like

Origin www.cnblogs.com/weijiutao/p/11082571.html