/*package com.fish.object;java Object-Oriented language objects: The only thing that really exists. For example, my family's dog: is actually the extraction of the common attribute and behavior of some kind of thing. Abstract Concepts .... cars----> Drawings Objects---> Real things we've been touching a lot of objects since we were kids, Our brains classify things that have the same attributes and behaviors. In real life there are objects and classes of these two things. Computer is to serve the human, in order to let the computer closer to our life, this time there is an object-oriented computer language. Process oriented (C) Object-oriented (Java) approach to problem solving: focus on the process of solving the problem: Focus on the object of real life : object---------class in Java: class------------> Object-oriented computer language core idea: find the right object to do the right thing. How to find the right object: 1. sun has defined a lot of classes, we just need to know these classes, we can create objects using these classes. 2. custom classes to create object usage by using custom classes. The custom class creates a three-step:1. custom class for the object. Format:class class Name {The public property of a thing uses a member variable description. The public behavior of a thing uses a function description. }2. creates an object from a custom class. Format: class name variable name = new class name ();3. access (set) the properties of the object or invoke the function of the object. 1. Format of Access Object properties: Object. Property name. 2. Sets the properties of the object: Object. Property name = data. 3. the function object of the calling object. function name ();//Car Class class car{//the public properties of things use member variable descriptions. stringname; //the property of the name stringcolor; //the Color property int wheel;//the number of wheels//The public behavior of the thing using the function description. public Void run () {System.out.println (name+) "ran fast ...");} Class demo1 {public static void main (String[] args) {//used the car class to declare a C variable, The  C variable points to a car object. Carc = new car (); //sets the property value of the car object. c.name = "BMW";c.color = "white"; c.wheel = 4;//the property value of the Access car object System.out.println ("Name:" + c.name+ " Color:" + c.color+ " Number of Wheels:" +c.wheel "; C.run (); Carc1 = new car (); c1.name = "BMW"; Car c2 = new car ();c2.name = "Volkswagen"; C1 = c2; system.out.println (" Name: "+ c1.name"); //Volkswagen Volkswagen \ Volkswagen}}*/
package com.fish.object;/* requirements: Use Java to describe a car and garage two things, car has the public properties: Number of wheels, name, color Also has the function of running the behavior. Before running to check whether the wheel is less than 4, if less than 4, then to be sent to the garage repair, garage repair, the number of wheels to fill back 4. Then the car went on running. Garage: has public properties: name, address, phone. Public behavior: repair the car. The beginner's classic error:1. variables are directly accessible on the same scope (curly braces). 2. if a class is to access another class variable, it can only be accessed by creating an object at this time. (only right now) the public properties of the *///car class class car{//things use member variables to describe string name ; // names string color; Name int wheel; //number of wheels//common behavior of things use function Description Public void run () {if (wheel>=4) {System.out.println ( Name+wheel+ "a wheel running fast."); Else{system.out.println (name+ "Not enough 4 wheels, hurry to repair");}} Garage class carfactory{string name;//name string address ;//address string tel;//telephone//repair car public behavior ------ Return value type, unknown parameter Public void repair (car c) {if (c.wheel>=4) { System.out.println ("Tell you, it's a great effort to fix it, give money"); else{c.wheel = 4; System.out.println ("Fix it, give money!!!");}}} Class demo2 {public static void main (string[]&Nbsp;args) {/*//0x98car c = new car ();//Assign property value c.name = "Lufeng" to the vehicle object; c.color = "BLACK"; C.wheel = 4;for (int i = 0 ; i<100 ; i++) { C.run ();} C.wheel = 3;c.run ();//Create Garage object Carfactory f = new carfactory ();//Assign property value to garage F.name = "cluster bao Garage";f.address = "Yun Tai Commercial Plaza first Floor";f.tel = "020-1234567";//Call Repair car repair f.repair (c);// 0x98//c.run (); *///requirements: Call the repair method once. premise: This object is no longer in use after the object has called a method. }}
package com.fish.object;/* the difference between a member variable and a local variable: the location of the defined:1. member variable is defined outside the method, within the class. 2. Local variables are defined within the method. The difference in function:1. member variables are used to describe the public properties of a class of things. The function of 2. local variables is to provide a variable for internal use of the method. The life cycle difference:1. exists as objects are created and disappears as objects disappear. 2. local variables are executed when the corresponding method is called, when the statement that created the variable exists, and the local variable goes out of its own scope, that is, its own {}, which disappears from memory immediately, unlike JavaScript. The difference between the initial value of the:1. member variable is the default initial value. Data type default initial value int 0float 0.0fdouble 0.0boolean falsechar ' ' String (reference data type) null2. Local variables have no default initial values and must be initialized before they can be used. *///Human class person { string name; //member variable &NBSP;&NBsp; public void eat () { //system.out.println ("Hello world") // execution here does not create a local variable int age ; //Local variables are not used in the back, then not initialized, compile will not error string name; //Local Variables age = 12; for (int i = 0 ; i< 5 ; i++) { System.out.println ("Hello world" + age); } //system.out.println (i) //No access to i }}class demo3 { public static void main (String[] args) { Person p1 = new person (); p1.eat (); }}
This article from "Small Fish Blog" blog, declined reprint!
Java Object-oriented learning