Hey, here you object - an object-oriented: Classes and Objects

  I believe when many small partners are beginning to learn Java tactics: HelloWorld First, create a class, and then write a main method, the final printed output, ojbk finished thing!. And when we have time to object-oriented look at what is like, what the object is, They are what relationships. About object-oriented, well written online that I have been copying the example of the difference between the C language and Java the essay, the junior partner interested can point to the upper left corner of the menu view.

  Then the classes and objects in the end what is it? There is a class of certain common characteristics of the set of entities; EXAMPLES Example is an object of the class, of course, it is an object of a class. Come, the example: There is a saying called feather flock together people into groups, we Helenians to, for example, humans or that of course is a class, because when you say it is not a human entity, that is, I do not know what people are talking about ; but when we say that people are aware features, a nose with eyes, to make and use tools. Remember the first day dialogue first article is not English, I'm Kangkang are you Michael? Michael here Kangkang and is not the object, of course, Kangkang a nose with eyes of it. People can not say it will not make and use tools, can be considered a simple, consistent with the characteristics of this class of people, is one example, so people are a target, that Michael do? Of course, also an object.

  The relationship between class and object light out, and class is abstract object, the object is an instance of a class . What does that mean, first of all I do not know that a small partner to abstract understanding and I was not the same as before, the specific antonyms ah, something hard to describe ah, in fact abstract simple here it is out of part image, you see Kangkang a nose with eyes the will to walk, eat, sleep ...... that Michael is, you are, I was; everyone has the characteristics of extracted classified as human. That object is an instance of a class should be well understood, Kangkang this object is an instance not human? Of course, that your cat is not an instance of a cat? Furry, meow also, of course, that people domesticated cat cat naturally an instance of class.

  Here to tell you the properties and methods of the object, and then we will go on a piece of code, object contains properties and methods, attributes that some features of the object, such as: name, sex, height; method can be understood as an object of certain behaviors, such as to eat , sleep, study. To the piece of code:

. 1  Package Test;
 2  
. 3  public  class the Person {
 . 4  
. 5      int Age; // Age attribute 
. 6      String name; // name attribute
 . 7  
. 8      // define a method of sleep 
. 9      public  void SLEEP () {
 10          System.out.println ( " sleep " );
 11      }
 12 is  
13 is      // define a learning method 
14      public  void study () {
 15          // process may add attributes 
16          System.out.println (name +" learning " );
. 17      }
 18 is  
. 19      public  static  void main (String [] args) {
 20 is  
21 is          // Create a Person object class P1 
22 is          Person P1 = new new Person ();
 23 is  
24          // by object properties assigned to the attribute. 
25          P1. = name "Kangkang" ;
 26 is          p1.age = 18 is ;
 27          System.out.println (p1.name);
 28          
29          // method to invoke an object method. 
30          p1.sleep ();
 31 is      
32      }
 33 is  
34 is } 
    
performed The results:
     Kangkang
     go to bed 

I do not know why there are no beginners Kangkang printed before sleep, results Kangkang print p1 objects directly in the main method name property, and sleep is the p1 object called sleep the previous method, the calling when the method of printing.

We come to create an object that one out to look at

 This class can create only one object? Of course not, how much you want as long as the number of new

. 1  Package Test;
 2  
. 3  public  class the Person {
 . 4  
. 5      int Age; // Age attribute 
. 6      String name; // name attribute
 . 7  
. 8      // define a method of sleep 
. 9      public  void SLEEP () {
 10          System.out.println ( " sleep " );
 11      }
 12 is  
13 is      // define a learning method 
14      public  void study () {
 15          // process may add attributes 
16          System.out.println (name +" learning " );
. 17      }
 18 is  
. 19      public  static  void main (String [] args) {
 20 is  
21 is          // Create a Person object class P1 
22 is          Person P1 = new new Person ();
 23 is  
24          // by object properties assigned to the attribute. 
25          P1. = name "Kangkang" ;
 26 is          p1.age = 18 is ;
 27          System.out.println (p1.name);
 28          
29          // through the object method to invoke a method. 
30          p1.sleep ();
 31 is          
32          // Create another Object P2 
33 is          the Person P2 = new newThe Person ();
 34 is          // assigns them 
35          p2.name = "trump" ;
 36          p2.age =. 3 ;
 37 [         
      // call the study P2 method 38 is p2.study ();
      // call the study method P1
39 P1 .study (); 40 41 is } 42 is 43 is }
  execution result:
      Kangkang
      sleep
      Trump learning
      Kangkang learning

Print statements study () method we added a print of a property name when calling a name attribute p2 p2 + learning of Trump, printing is invoked when p1 p1 name attribute Kangkang + learning

So we call a method through an object, the method attribute is the current properties of the object.

Guess you like

Origin www.cnblogs.com/xpybsh/p/12550403.html