First, the process-oriented and process-oriented comparison
Process-oriented: 1 Do-it-yourself—> 2 Grocery shopping—> 3 Washing vegetables—> 4 Cooking and stir-frying—> 5 It’s hard to eat and a waste of time
Object-oriented: 1. Looking for professional objects—>2 restaurants. Ordering food—>3 restaurants, cooking—>4 Delicious meals, saving time
Object-oriented thinking first finds some objects and uses them directly. If sun does not provide them, then create objects by yourself
1) Encapsulation
2) Inheritance
3) Polymorphism
The process of development: In fact, it is to continuously create objects, use objects, and direct objects to do things
The process of design: in fact, it is to manage and maintain the relationship between objects
Three, java describes things
Attributes: member variables in a class
Behavior: Member functions in classes
Fourth, define the class
1) Define a class using the class keyword
2) The class name is the identifier, naming rules, the first letter of the word is capitalized, and the first letter of multiple words is capitalized
3) The class name followed by a pair of {} indicates the start and end of the class
4) No need to initialize the value
public class Car { String color;// member variable int num; // member variable // member function void run() { System.out.println(color + "car, tire number: " + num + ", running"); } }
5. Create objects
class CarDemo { public static void main(String[] args) { //Use the new keyword to open up space in the heap memory and generate an entity //In order to facilitate the use of the production car, name it // c is of type Car, called a class type variable // c is the held reference, the newly produced car is not directly assigned to c, just like a TV remote control Car c = new Car(); c.run(); //Use the function of the object } }
6. Object member call
1) Member variables
2) Member method
public class CarDemo { public static void main(String[] args) { Car c = new Car(); //Object name. The member variable name will return the value stored in the member variable int num = c.num; System.out.println(num); //Object name. Member variable name, you can also assign values to member variables c.num = 4; c.color = "black"; //Object name.Member method(); c.run(); } }
example
[root@bch04 java]# cat Cardemo.java class Car { String color; int num; void run() { System.out.println(color + "color car, number of tires: "+num +", running"); } } class Cardemo { public static void main(String[] args) { Car c = new Car(); c.color = "red"; c.num = 4; c.run(); } } //operation result [root@bch04 java]# java Cardemo Red color car, tire count: 4, running
Seven, local variables and member variables
Member variable: a variable defined in a class is valid in the entire class, it belongs to an object, it is created with the creation of the object, and disappears with the disappearance of the object; if no initial value is assigned, the default value in the storage heap is used value
Local variable: A variable defined in a method of a class is only valid in the method it is declared in, and the space is released immediately after use; if you want to use it, you must manually initialize it
example
[root@bch04 java]# cat Demo13.java class Car { String color = "red"; String name = "Smart"; int num = 4; void run() { if (num <4) { System.out.println("bad!"); } else { System.out.println(name +":" +color +":" +num +":run!"); } } } class CarFactory { String name; String addr; void repair(Car sc) { sc.num = 4; System.out.println("good!"); } } public class Demo13 { public static void main(String[] args){ Car sc1 = new Car(); sc1.run(); sc1.num = 3; sc1.run(); CarFactory sc2 = new CarFactory(); sc2.name = "Happy Depot"; sc2.addr = "Shenzhen, Guangdong"; sc2.repair(sc1); sc1.run(); } } //operation result [root@bch04 java]# java Demo13 Smart:red:4:run! bad! good! Smart:red:4:run!
Definition: Entity without a name
Function: An anonymous object can be used when it is only used once. After execution, the object becomes garbage; if the object needs to be called multiple times with multiple members, then
You must give the object a name, and you cannot continue to use anonymous objects; when executing the method, you can pass the anonymous object as an actual parameter and pass it in
example
[root@bch04 java]# cat Demo13.java class Car { String color = "red"; String name = "Smart"; int num = 4; void run() { if (num <4) { System.out.println("bad!"); } else { System.out.println(name +":" +color +":" +num +":run!"); } } } class CarFactory { String name; String addr; Car repair(Car sc) { sc.num = 4; System.out.println("good!"); return sc; } } public class Demo13 { public static void main(String[] args){ //create non-anonymous object Car sc1 = new Car(); sc1.run(); sc1.num = 3; sc1.run(); CarFactory sc2 = new CarFactory(); sc2.name = "Happy Depot"; sc2.addr = "Shenzhen, Guangdong"; sc2.repair(sc1); sc1.run(); System.out.println("**********"); //Create anonymous objects, anonymous objects can be passed as actual parameters, anonymous objects have methods but no attributes //Anonymous object one, used once new Car().run(); //Anonymous object two Car sc3 = sc2.repair(new Car()); sc3.run(); } } [root@bch04 java]# java Demo13 Smart:red:4:run! bad! good! Smart:red:4:run! ********** Smart:red:4:run! good! Smart:red:4:run!