Object oriented and process oriented
Process oriented: when a function needs to be realized, each specific step should be handled in detail.
Object oriented: when I need to implement a function, I don't care about the specific steps, but find someone who already has the function to help me.
import java.util.Arrays; public class Demo01PrintArray { public static void main(String[] args) { int[] array = {10, 20, 30, 40, 50}; //The required printing format is: [10,20,30,40,50] //Using process oriented, the details of each step should be done by yourself. //That is, first output a [, output traversal in the for loop, print if it is the last output], if not, output,. System.out.print("["); for (int i = 0; i < array.length; i++) { if (i == array.length - 1) {//If it is the last element System.out.println(array[i] + "]"); } else {//If not the last element System.out.print(array[i] + ","); } } System.out.println("==============="); //Using object-oriented programming //Find a JDK to provide us with a good Arrays class, in which the toString method can directly turn the array into the desired format string System.out.println(Arrays.toString(array)); } }
Classes and objects
Class: it is a set of related attributes and behaviors, which can be regarded as the template of a class of things. The attribute characteristics and behavior characteristics of things are used to describe this class of things
Relationship between class and object
Class is the description of a class of things, which is abstract
An object is an instance of a class of things and is concrete
Class is the template of object, and object is the entity of class
Usually, a class cannot be used directly. You need to create an object according to the class before you can use it.
1. Guide Package: that is, indicate the class to be used and where it is located.
import package name Class name;
import cn.itcast.day06.demo01.Student;
If it belongs to the same package as the current class, you can omit the package guide statement without writing
2. Create format
Class name object = new class name ();
Student stu = new Student();
3. It can be used in two cases
Use member variable: object name Member variable name
Use member method: object name Member method name (parameter)
(that is, use the object name for whoever you want to use.)
matters needing attention:
If the member variable is not assigned, there will be a default word, and the rule is the same as that of the array
public class Demo01Student { public static void main(String[] args) { //1. Guide Package //I need to use the student class, and Demo01 myself Students are under the same package, so the package guide statement is omitted //2. Create, format //Class name object = new class name (); //According to the Student class, an object named stu is created Student stu = new Student(); //3. Use the member variable in the format: //Object name Member variable name System.out.println(stu.name);//null System.out.println(stu.age);//0 //Change the value content of the member variable in the object //Assign the string on the right to the name member variable in the stu pair stu.name = "Xiaotiantian"; stu.age = 22; System.out.println(stu.age);//18 System.out.println(stu.name);//Xiaotiantian System.out.println("============"); //4. Use the member method and format of the object //Object name Member method name () stu.study(); stu.eat(); stu.sleep(); } }
/* * Define a class to simulate "student" things, which has two components * * Attribute (what is it) * Age * full name * Behavior method (what can be done) * having dinner * sleep * study * Corresponding to java class * Member variables (properties) * String name;//full name * int age;//Age * Member method (behavior) * public void eat() {}// having dinner * public void sleep() {} * public void study() {} /* * matters needing attention: * 1,Member variables are directly defined in the class and outside the method. * 2,Do not write static keyword for member method*/ public class Student { //Member variable String name;//full name int age; //Member method public void eat() { System.out.println("Have a meal"); } public void sleep() { System.out.println("Sleep sleep"); } public void study() { System.out.println("study"); } }
Mobile phone exercises
Define a mobile phone class
Mobile phones
/ * Define a class to simulate "mobile phone" things * * Attributes: brand, color, price * Behavior: calling, texting * * Corresponding to class: * Member variables (properties) * String barand;//brand * double price;//Price * String color;//colour * Member method (behavior) * public void call(){}//phone * public void sendMessage(){}//Mass messaging * * */ public class Phone { //Member variables (properties) String barand;//brand double price;//Price String color;//colour //Member method (behavior) public void call(String who) { System.out.println("to" + who + "phone"); }//phone public void sendMessage() { System.out.println("Mass texting"); }//Mass messaging }
Memory map of an object
Complete creation of objects, access to member objects, call member methods, and what happens to the stack, heap, and method area in memory
public class Demo02phoneOne { public static void main(String[] args) { //Create an object named one according to the phone class //Format: class name object name = new class name (); Phone one = new Phone(); System.out.println(one.barand);//null System.out.println(one.price);//0.0 System.out.println(one.color);//null System.out.println("================"); one.barand = "Apple"; one.price = 8388.0; one.color = "black"; System.out.println(one.barand); System.out.println(one.price); System.out.println(one.color); System.out.println("================"); one.call("Steve Jobs"); one.sendMessage(); } }
Memory map of two objects using the same method
package cn.itcast.day06.demo02; public class Demo02phonetwo { public static void main(String[] args) { Phone one = new Phone(); System.out.println(one.barand);//null System.out.println(one.price);//0.0 System.out.println(one.color);//null System.out.println("================"); one.barand = "Apple"; one.price = 8388.0; one.color = "black"; System.out.println(one.barand); System.out.println(one.price); System.out.println(one.color); System.out.println("================"); one.call("Steve Jobs"); one.sendMessage(); System.out.println("================"); Phone two = new Phone(); System.out.println(two.barand);//null System.out.println(two.price);//0.0 System.out.println(two.color);//null System.out.println("================"); two.barand = "Samsung"; two.price = 5999.0; two.color = "blue"; System.out.println(two.barand); System.out.println(two.price); System.out.println(two.color); System.out.println("================"); two.call("Oba"); two.sendMessage(); } }
The references of two objects point to the memory space of the same object
Since the above two objects use the same method, there is no connection between the memory occupation, because two also uses new, but if not new, how about the memory occupation when one is assigned to two?
public class Demo02phoneSame { public static void main(String[] args) { Phone one = new Phone(); System.out.println(one.barand);//null System.out.println(one.price);//0.0 System.out.println(one.color);//null System.out.println("================"); one.barand = "Apple"; one.price = 8388.0; one.color = "black"; System.out.println(one.barand); System.out.println(one.price); System.out.println(one.color); System.out.println("================"); one.call("Steve Jobs"); one.sendMessage(); System.out.println("================"); //Assign the address object saved in one to two Phone two = one; System.out.println(two.barand);//Apple System.out.println(two.price);//8388.0 System.out.println(two.color);//black System.out.println("================"); two.barand = "Samsung"; two.price = 5999.0; two.color = "blue"; System.out.println(two.barand); System.out.println(two.price); System.out.println(two.color); System.out.println("================"); two.call("Oba"); two.sendMessage(); } }
Use the object type as the parameter of the method
When an object is passed to a method as a parameter, what is actually passed in is the address value of the formation.
public class Demo01PhonParm { public static void main(String[] args) { Phone one = new Phone(); one.barand = "Apple"; one.color = "luxury gold color" ; one.price = 8388.0; method(one);//The parameter passed in is actually the address value } public static void method(Phone param){ System.out.println(param.color);//luxury gold color System.out.println(param.price);//8388.0 System.out.println(param.barand);//Apple } }
Use the object type as the return value of the method
When the object type is used as the return value of the method, the return value is actually the address of the object.
public class Demo01PhoneReturn { public static void main(String[] args) { Phone two = getPhone(); System.out.println(two.color);//black System.out.println(two.price);//8388.0 System.out.println(two.barand);//Apple } public static Phone getPhone(){ Phone one = new Phone(); one.barand = "Apple"; one.price = 8388.0; one.color = "black"; return one; } }