I believe that a lot of kids learn together Java It's always the beginning of tactics :HelloWorld!
First , Create a class , Then write a main method , Last printout ,ojbk come to an end . When we learn object-oriented, we have to understand what a class is , What is an object , What is their relationship .
About object oriented , I've copied a good example from the Internet C Language and Java The difference , Interested partners can click the menu in the upper left corner to check .

   So what are classes and objects ? Class is a set of entities with some common characteristics ; An object is an instance of a class , An object is, of course, an instance of a class . come , Example above : There is a saying that birds of a feather flock together , Let's take people for example , Human or human is a class of course , Because when you talk about people, it's not an entity , I don't know who it is ; But when it comes to people, we all know the characteristics , With nose and eyes , Ability to make and use tools . Do you remember the first conversation in the first day of junior high school ,I'm
Kangkang are you
Michael? There Kangkang and Michael Is it an object , Certainly ,Kangkang Have a nose and an eye . It can't be said that people can't make and use tools , It's easy , In line with the characteristics of the human class , Another example , So people are one object , that Michael What about ? Of course, it's also an object .

   The relationship between class and object should be highlighted , Class is an abstraction of an object , Object is an instance of a class
. What does that mean? , First of all, I don't know if my partner's understanding of abstraction is the same as before , Specific antonyms , It's hard to describe , In fact, the abstraction here is simply to extract the part of the image , You see Kangkang With nose and eyes , Can walk , Having dinner , Sleep ...... that Michael Also , You too , Me too. ; Everyone's characteristics are extracted and classified as human beings . That object is an instance of a class should be well understood ,Kangkang Is this object an example of human beings ? Certainly , Is your cat an example of a cat ? Fluffy , And meow , Certainly , The cat that other people keep at home is naturally an example of cats .

   Let's talk about the properties and methods of objects , And then we'll go back to the code , Object contains properties and methods , Properties are some characteristics of objects , such as : Name , Gender , height ; Methods can be understood as some behaviors of objects , Like eating , Sleep , Study . To the last code :
1 package test; 2 3 public class Person { 4 5 int age;// age attribute 6 String
name;// name attribute 7 8 // Define a way to sleep 9 public void sleep() { 10
System.out.println(" Sleep "); 11 } 12 13 // Define a learning method 14 public void study() { 15 //
Attribute can be added to method 16 System.out.println(name + " Study "); 17 } 18 19 public static void
main(String[] args) {20 21 // Create a Person Class objects p1 22 Person p1 = new Person(); 23
24 // adopt object . attribute To assign a value to a property 25 p1.name = "Kangkang"; 26 p1.age = 18; 27
System.out.println(p1.name);28 29 // By object . Method to call the method 30 p1.sleep(); 31 32 } 33 34
}
    
results of enforcement :
     Kangkang
      Sleep  

I don't know if any beginners want to ask why Kangkang Print before sleep , In result Kangkang Is directly in main Print in method p1 Object name Attribute , And sleep is p1 Before the object call sleep Method , Printed when the called method is executed .

Let's take a look at the sentence "create object"

  Can this class create only one object ? Of course not. , As long as you want new How many?
1 package test; 2 3 public class Person { 4 5 int age;// age attribute 6 String
name;// name attribute 7 8 // Define a way to sleep 9 public void sleep() { 10
System.out.println(" Sleep "); 11 } 12 13 // Define a learning method 14 public void study() { 15 //
Attribute can be added to method 16 System.out.println(name + " Study "); 17 } 18 19 public static void
main(String[] args) {20 21 // Create a Person Class objects p1 22 Person p1 = new Person(); 23
24 // adopt object . attribute To assign a value to a property 25 p1.name = "Kangkang"; 26 p1.age = 18; 27
System.out.println(p1.name);28 29 // By object . Method to call the method 30 p1.sleep(); 31 32 //
Create another object p2 33 Person p2 = new Person(); 34 // Assign a value to a property 35 p2.name =" Trump "; 36 p2.age
= 3; 37
      //p2 call study Method 38 p2.study();
      //p1 call study Method 39 p1.study(); 40 41 } 42 43 }
   results of enforcement :
      Kangkang
       Sleep
       Trump learning
      Kangkang Study
study() We add an attribute to the print statement of the name p2 When called, prints p2 Of name attribute Trump + Study ,p1 When called, prints p1 Of name attribute
Kangkang+ Study

So when we call a method through an object , Method is the property of the current object .

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network