1 overview
Class: is a set of related properties and behaviors. is an abstract concept.
Object: Is the concrete manifestation of this kind of thing. Specific existence of the individual.
(1) Object oriented thinking
Object-oriented is a process-oriented programming idea.
Process oriented: Emphasis on the steps of each of these functions
Object-oriented: emphasizes the object and then invokes the function by the object
(2): Object-oriented thinking features
A: It's a thought that's more in line with our thinking habits.
B: Complex things can be simplified
C: Turning us from the executor to the conductor
Example:
Buy a computer:
Process-oriented: My understanding of the computer--understand my own needs--find the corresponding parameter information--go to Zhongguancun to buy a computer--bargain--Buy back the computer
Object-oriented: I know I want to buy a computer--the squad leader buys it for me--the monitor buys it back.
Eat:
Process oriented: Go to the supermarket to buy food---------------------------------
Object-oriented: Dinner at the restaurant, you-waiter (à la carte)-Chef (cooking)-Waiter (end dish)--eat
Example: Student class
1 /*2 things:3 information Description of a property thing4 the function of a behavioral thing5 6 class:7 property of a member variable thing8 the behavior of a member method thing9 Ten defining a class is actually a member variable and a member method that defines the class. One A case: Let's complete a student class definition. - - Student Things: the Properties: Name, age, Address ... - behavior: Study, eat, sleep ... - - To convert a thing to a corresponding class: + - Student class: + member variables: Name, age, Address ... A member methods: Learn, eat, sleep ... at - member variables: the same format as the definition of the previous variable, but in a different location, outside the method in the class. - member method: the same format as the previous method definition, but today the static is removed first. - - First we should define a class and then complete the members of the class. - */ in //This is my student class. - classStudent { to //Defining Variables + //name - String name; the //Age * intAge ; $ //AddressPanax Notoginseng String address; - the //Defining Methods + //How to learn A Public voidStudy () { theSYSTEM.OUT.PRINTLN ("Students Love learning"); + } - $ //the way to eat $ Public voideat () { -System.out.println ("Tired of learning, to play games"); - } the - //the way to sleepWuyi Public voidsleep () { theSystem.out.println ("The game is tired, to sleep.")); - } Wu}
Example 2 Phone class
1 /*2 test of mobile phone class3 */4 classPhone {5 //Brand6 String brand;7 //Price8 intPrice ;9 //ColorTen String color; One A //How to call - Public voidCall (String name) { -SYSTEM.OUT.PRINTLN ("Call" +name+ "); the } - - //How to send text messages - Public voidSendMessage () { +SYSTEM.OUT.PRINTLN ("Mass sms"); - } + A //How to play the game at Public voidPlayGame () { -System.out.println ("Play games"); - } - } - - classPhonedemo { in Public Static voidMain (string[] args) { - //Create a Phone object to //Class Name Object name = new class name (); +Phone p =NewPhone (); - the //Direct output member variable value *System.out.println (p.brand+ "---" +p.price+ "---" +p.color); $ Panax Notoginseng //Assigning a value to a member variable -P.brand = "Nokia"; theP.price = 100; +P.color = "Gray"; A //Output Again theSystem.out.println (p.brand+ "---" +p.price+ "---" +p.color); + - //Calling Methods $P.call ("Brigitte"); $ p.sendmessage (); - p.playgame (); - } the}
3 Object Memory Graph
Object-oriented initialization of Java Note 12