I Object oriented thought
Process oriented and object-oriented are both ways to solve problems
Process oriented: write code from top to bottom according to business logic; It emphasizes that the steps, processes and every step are realized by yourself.
Object oriented: create objects, use objects, and command objects to do things.
Object oriented programming idea:
Simplify complex things and turn us from executors to commanders
Three characteristics of object-oriented: encapsulation, inheritance and polymorphism
1.1 related concepts
Object: everything in the program world is an object. It is a set of related attributes and behaviors. Objects are entities
Class: a collection of identical objects. Classes are abstract
Attribute is the descriptive information of the thing
Behavior is what the thing can do
1.2 definition of students
Attribute: name, age, gender
Behavior: study, sleep
1.3 definition of mobile phone
Attribute: brand, price
Behavior: making phone calls and playing games
II Memory area analysis
In the process of running java applications, the jvm will divide the memory it manages into several different data areas.
Why heap and stack? What are the benefits of this design?
-
Java automatically manages stacks and stacks. Programmers cannot directly set stacks and stacks.
-
The Java heap is a runtime data area. The heap is automatically managed by the JVM's garbage collector. The advantage of heap is that it can dynamically allocate the memory size when the program is running, but it is for this reason that its access speed is slow.
-
The advantage of stack is that the access speed is faster than heap, second only to register, and stack data can be shared. But the disadvantage is that the data size and lifetime in the stack must be determined and lack of flexibility.
-
A very important feature of stack is that the data in the stack can be shared.
Stack: when each method is executed, a space will be created in the stack area
Stack features: first in and last out
Method is called until the execution is completed, corresponding to the process from entering the stack to exiting the stack
Stack is automatically allocated by the system, which is fast and trestle like, a continuous memory space
Heap: heap is used to store created objects and arrays (arrays are also objects)
Heap is a discontinuous memory space with flexible allocation and slow speed
Method area:
The method area belongs to the heap. Used to store classes, constants and other related information
Also called static area. It is used to store the permanent or unique content (class information, static variables, strings, constants, etc.) in the program
A: Different positions in the class
Member variable: outside method in class
Local variable: in the method definition or on the method declaration
B: Different initialization values
Member variable: has default initialization value
Local variable: there is no default initialization value. It must be defined and assigned before it can be used.
C: Different locations in memory
Member variable: in heap memory (member variable belongs to object, and object enters heap memory)
Local variables: in stack memory (local variables belong to methods, and methods enter stack memory)
D: Different life cycles
Member variable: exists with the creation of the object and disappears with the disappearance of the object
Local variable: exists with the method call and disappears with the method call
matters needing attention:
The local variable name can be the same as the member variable name. When used in the method, the proximity principle is adopted.
What are the basic data type variables: byte,short,int,long? The default value is 0
The default value of float and double is 0.0
boolean: false
char:'\u0000'
Which variables of reference data type include: array, class, interface,, String. When reference data type is used as member variable, the default value of initialization is null
How to call when the formal parameter of the method is the class name (Master)
A: The parameter of the method is the class name public void print(Student s){}//print(new Student());
If you see that the formal parameter of a method is the type of a class (reference type), what you need here is the object of the class.
Overview and application of anonymous objects (Master)
Definition of anonymous object: an object without a name
Anonymous object application scenario
When the method needs to be called through this object only once.
Benefits of anonymous object calls
Save code
Note: it is not suitable for multiple calls. After the anonymous object is called, it is garbage. Can be recycled by the garbage collector.
Anonymous objects can be passed as actual parameters
public class Demo1 { public static void main(String[] args) { int a =0; System.out.println(a); method(); } static void method(){ int a = 0; } }
public class Demo2 { public static void main(String[] args) { Student s1 = new Student(); System.out.println("The student's name is:"+s1.name);//Null the default value of the reference data type is null System.out.println("The student's name is:"+s1.age);//Null the default value of the reference data type is null } }
public class Demo3 { int x ; //The default value of member variable is 0 public static void main(String[] args) { int x = 10; //local variable //The local variable name can be the same as the member variable name. When used in the method, the value is // Proximity principle // System.out.println(x); Demo3 d1 = new Demo3(); d1.x = 888; Demo3 d2 = new Demo3(); d2.x = 999; System.out.println(d1.x); } }
public class Student { //Function: describe the Student class from two aspects //Cannot run alone, relying on a main class (test class) /* Attribute: --- > variable representation Wechat name, name, age and gender Behavior: --- > method embodiment (code block that can complete a function) Talk, study and play games */ String wechatName;//Wechat name String name; //full name int age;//Age char sex;//Gender //Method without any modifiers void speak(){ System.out.println("Well said"); System.out.println("flower"); } void show(){//Show your information System.out.println("My name is"+name+",this year:"+age+",Gender is:"+sex); } void study(){ System.out.println("Watch videos"); System.out.println("Knock code"); System.out.println("do homework"); } void playGame(){ System.out.println("look for sb."); System.out.println("Go to the Internet cafe"); System.out.println("gang up"); } }
public class TestStudent {//TestStudent class: the basic unit of a program. An application is composed of multiple classes //There is a main() method in TestStudent. This TestStudent class can be called the main class (test class) to run //entrance public static void main(String[] args) { //Test Student class Student s1 = new Student(); //Assign an object name to an attribute Attribute name = value; s1.wechatName = "Shmily"; s1.name = "Li Yang"; s1.age = 16; s1.sex = 'male'; //Call methods through objects to command objects to do things s1.playGame(); s1.speak(); s1.study(); s1.show(); //Another object Student s2 = new Student(); s2.wechatName = "Wen Wenwen"; s2.name = "Wen translated as"; s2.age = 18; s2.sex = 'male'; s2.show(); System.out.println("----------------------------"); Student s3 = s1;//Assignment reference address s3.sex = 'female'; s1.show(); s3.show(); } }