Introduction to java (XIV) | object oriented (OOP) and objects
java entry series, start from scratch!!!
The last issue was variables. Variables are always everywhere in java. Variables mean numbers that can be changed. In general, we can describe them in terms of variable type, variable name and variable value
This issue is a beginning for object-oriented (OOP), which provides a basic understanding of its concepts and three characteristics, focusing on the explanation and application of classes and objects
Object oriented (OOP)
1.1 concept
Thinking in java
The so-called object-oriented is a kind of programming idea, through which we can simplify the complex things in life, from the original executor to the commander.
Object oriented is based on process oriented.
Object Oriented Programming (OOP)
1.2 three characteristics
- Encapsulation, encapsulating related data into a "class" component
- Inheritance means that subclasses automatically share the properties and methods of the parent class, which is a relationship between classes
- Polymorphism to enhance the flexibility and reusability of software
Classes and objects
Class 2.1
1. The most basic unit of the Java language is the class, which is similar to the type.
2. A class is an abstraction of a class of things.
3. It can be understood as a template or design drawing.
2.2 objects
Each object has three characteristics: object state, object behavior and object identification.
1. The state of an object is used to describe the basic characteristics of an object.
2. The behavior of an object is used to describe the function of an object.
3. Object identification means that objects have a unique address value in memory to distinguish them from other objects.
4. A class is an abstraction of a class of things, and an object is a concrete implementation.
Automobile:
It can have Audi objects and BMW objects.
2.3 relationship between class and object
- Computer language is used to describe things in the real world. Describe by describing the characteristics and behavior of things
- How to describe it in java language? Describe things through classes, treat the attributes of things as member variables, and treat behaviors as member methods.
Analyze mobile phone affairs:
Class: mobile phone class, extracting the same features and functions
Object: many mobile phones can be produced according to the template, such as mobile phone 1 object, which contains unique member variables and member methods
Creation and use of classes and objects
Create a class with the class keyword
Create an object with the new keyword.
3.1 exercise 1: creating and using classes
To create a Phone class:
package cn.qile.oop; /** * Phone Class is used to describe mobile phone things * Class is the abstraction of a class of things. It only abstractly specifies the appearance of a class of things. The specific implementation depends on the object * Describe things: features / attributes of things + functions / behaviors of things */ public class Phone { //Characteristics / attributes of things -- member variables String color; //colour int size; //size String pinpai; //brand double price; //price //Function / behavior of things -- member method public void call(){ System.out.println("Calling"); } public void message(){ System.out.println("Sending SMS"); } public void music(){ System.out.println("Listening to music"); } }
Create Test1_Phone test class:
package cn.qile.oop; //Test the use of mobile phones public class Test1_Phone { public static void main(String[] args) { //Use the new keyword to create information about the specific description class of the mobile phone object //p is a variable of reference type and refers to the address value Phone p = new Phone(); //Set values for object properties p.color = "gules"; p.size = 5; p.pinpai = "HUAWEI"; p.price = 9999; //Call the attributes / variables specified by the template System.out.println(p.color);//null -- "red" System.out.println(p.size);//0 -- 5 System.out.println(p.pinpai);//null --"HUAWEI" System.out.println(p.price);//0.0 --9999.0 //Call the functions specified in the template p.call(); //Calling p.message();//Sending SMS p.music(); //Listening to music } }
3.2 storage of objects in memory
- Generally speaking, local variables are stored in the stack, and the memory will be released after the method is executed
- Objects (things from new) are stored in the heap. The memory will be released only when the objects are no longer used
- Each heap memory element has an address value
- All properties in the object have default values
3.3 single object memory diagram
3.4 creating multiple objects
Make changes to the Phone class above:
package cn.qile.oop; /** * Phone Class is used to describe mobile phone things * Class is the abstraction of a class of things. It only abstractly specifies the appearance of a class of things. The specific implementation depends on the object * Describe things: features / attributes of things + functions / behaviors of things */ public class Phone { //Characteristics / attributes of things -- member variables String color; //colour int size; //size String pinpai; //brand double price; //price //Function / behavior of things -- member method public void call(){ System.out.println("Calling"); } public void message(){ System.out.println("Sending SMS"); } public void music(){ System.out.println("Listening to music"); } //change data //toString() -- view the value of the property @Override public String toString() { return "Phone{" + "color='" + color + '\'' + ", size=" + size + ", pinpai='" + pinpai + '\'' + ", price=" + price + '}'; } }
Change Test1_Phone:
package cn.qile.oop; //Test the use of mobile phones public class Test1_Phone { public static void main(String[] args) { //Use the new keyword to create information about the specific description class of the mobile phone object //p is a variable of reference type and refers to the address value Phone p = new Phone(); //Set values for object properties p.color = "gules"; p.size = 5; p.pinpai = "HUAWEI"; p.price = 9999; //Call the attributes / variables specified by the template System.out.println(p.color);//null -- "red" System.out.println(p.size);//0 -- 5 System.out.println(p.pinpai);//null --"HUAWEI" System.out.println(p.price);//0.0 --9999.0 //Call the functions specified in the template p.call(); //Calling p.message();//Sending SMS p.music(); //Listening to music //Added p2 second mobile data Phone p2 = new Phone(); //Set values for object properties p2.color = "Rose Gold"; p2.size = 6; p2.pinpai = "VIVO"; p2.price = 5888; //Call the attributes / variables specified by the template // System Out Println (p2.color)// Rose Gold // System.out.println(p2.size);//6 // System.out.println(p2.pinpai);//"VIVO" // System.out.println(p2.price);//5888.0 //Since toString() is provided in the Phone class, you can directly print p2 to see the value of the attribute // Phone [color= rose gold, size=6, pinpai=VIVO, price=5888.0] System.out.println(p2); //Call the functions specified in the template p2.call(); p2.message(); p2.music(); } }
3.5 multiple object memory diagram:
Next issue: Introduction to java (15) | encapsulation and private of OOP(2)
Pay attention to [Qile is not a code farmer], reply to [getting started with Java], and read the whole series of getting started with java!!
Joy awaits your arrival