Object-oriented

Object-Oriented

Review

1 Use of tools Arrays binarySearch() // Binary search sort()//sort fill()//fill copyOf()//copy copyOfRange(); range copy toString()//turn the array into a string equals(); 2 method parameter transfer and return value method The method of passing by value is adopted: the basic type passes the actual data: it is equivalent to copying the address of the reference class type to pass the data. It is the same data return value: the basic type returns the actual data, and the reference type returns the address 3 each element in the two-dimensional array or the array definition int[][] arr=new int[][]{{1 ,2},{3,4}}; 4 Debugging skills 4.1 Add breakpoint 4.2 Single step F6 single step skip F5 single step enter F7 single step return to F8 continue other debugging methods: add input statement 5 document comment generation api There are hints/** */ tags in the document coding: @author @version @since @see @param @return

today’s task

1. Object-oriented 
2. Process-oriented
3. Class creation
4. Object creation
5. Object memory analysis
6. Construction method
7. this Keywords

teaching goals

1. Master the object-oriented thinking
2. Master the creation of classes and objects
3. Master the objects Create memory analysis
4. Master the construction method
5. Master this keyword

Section 1: Object-Oriented Programming Thoughts< /h4>

1.1 What is object-oriented
A way of thinking about problems, focusing on finding a specific individual with special functions, Then entrust this individual to do something, and we call this individual an object. It is a kind of thinking that is more in line with human thinking habits [lazy thinking], which can simplify complex things and transform programmers from executors to commanders to use object-oriented development. First, find the ones with the required functions. Object to use, if the object does not exist, then create an object with the required functions
1.2 what is process-oriented
a way of looking at the problem When thinking about problems, focus on how the problem is solved step by step, and then solve the problem by yourself
1.3 Object-oriented and process-oriented Comparison of
Object-oriented is based on the philosophical view that everything is an object

? For example:

? Case 1: I want to eat a large plate of chicken
? Process-oriented object-oriented
? 1. Go shopping by yourself 1. Entrust a person who can bargain to help buy food
2 .Choose your own dishes 2. Entrust a temporary worker to help choose dishes
? 3. Make your own dishes 3. Entrust a chef to help make dishes
? 4. Start eating by yourself 4. Start eating by yourself

? Case 2: Xiao Ming is a computer novice, and wants to equip a computer. After buying the parts, he needs to transport it to his home. After the assembly is completed, he will open the computer to play games.
? Process-oriented and object-oriented? Knowledge 1. Entrust a friend (Pharaoh) who understands computers to help select parts
? 2. Xiao Ming to buy parts 2. Entrust someone who can run errands to buy parts
? 3. Xiao Ming bring the parts home 3. Entrust a courier to help Xiao Ming deliver home
? 4. Xiao Ming assemble the computer 4. Entrust someone who can assemble the computer to help Xiao Ming assemble the computer
? 5. Xiao Ming turn on the computer to play with the computer 5. Xiao Ming turns on the computer by himself , Start playing the game

1.4 Summary of Differences
a. It is a way of thinking about problems, both Solve the problem b. Process-oriented focus on all things and follow the steps to achieve c. Object-oriented focus on finding an object with special functions, and entrust this object to do something. Note: Object-oriented is an idea, not a programming language. 
1.5 The concept of class
 A collection of entities with the same attributes and functions [group], the class is Java language Basic unit
1.6 Object concept
In a class, an entity with special functions can solve specific problems. Objects are also called instances. 
1.7 The relationship between a class and an object
A class is an abstraction (template) of an object, and an object is a class Specific embodiment (example)
1.8 class declaration

? Syntax: access permission modifier class class name{

< p>? //Class body
? }

? Description: a. Access modifier: can only be public (can be omitted, default is default)
? b. Class The name only needs to be a legal identifier, but it is required: the first letter must be capitalized and follow Pascal’s nomenclature. public class FirstDemo {

? }

? public class Person{

? }

? public class Student{

? }

? Note: a. Multiple classes can be written in one Java file. If multiple classes are written, multiple .class files will be generated after compilation
? b. One There can be at most one public-modified class in a Java file. The class name of this class must be consistent with the Java source file name.
? C. If there is only one class in a Java file, and this class does not need to be publicly modified, The class name and the file name can be different, but as a rule, we will write public

1.9 The definition of member variables in a class

< blockquote>

? Member variables:

? a. Member variables are actually global variables
? b. Member variables are also called attributes
? c. Divided into static variables and non- Static variables
? D. Only static member variables can be accessed in static methods, and non-static member variables and static member variables can be accessed in non-static methods.

? The difference between member variables and local variables:
? a. The location of the definition is different

Member variables: defined in the class, acting on the entire class
Local variables: defined in the method or statement, acting on the method or statement .

? b. The time and location in the memory are different

Member variables: When the object is created, it appears in the heap memory. 
Local variable: When the method to which it belongs is operated, it appears in the stack memory.

? c. The life cycle is different.

Member variables: appear with the appearance of the object, and disappear with the disappearance of the object. 
Local variable: As the calculation of the method it belongs to, it is released.

? d. Different initialization values

Member variables: Because member variables are in the heap memory, they have default initial values
Local variables: No default initial values

? e. The member variable and the local variable have the same name, and the local variable has a higher priority. The principle of proximity.

1.10 Definition of methods in a class

? a. Divided into static methods and non-static methods

? b. In the same class, only static methods can be called in static methods, and non-static methods and static methods can be called in non-static methods.

/** * Human: Describe the static characteristics of a person: Name, Gender, Height, Weight... Dynamic characteristics: Eat, drink, write code, sleep, fall in love... * Member variables: describe static characteristics* Method: describe dynamic characteristics* * @author wgy * public Access modifier public * class representation class * Person class name * */public class Person {//Member variable String name;//Name String sex;//Gender int height;//Height int weight;// Weight//Methods//Eat public void eat() {System.out.println(name+" eat...");} //drink public void drink() {System.out.println(name+" drink beer" );} //Write code public void writeCode() {System.out.println(name+"Write java code");} }

Summary: 1 Static can access static, not direct access to non-static Yes, if you want to access, you must create an object.

? 2 Non-static can directly access non-static and static.

? 3 If you want to access static member variables and static method class names in another class. Static member variables, class names. Static method names.

Computer exercise: Define a student class:

? Student class contains attributes: student ID, name, age, major

? Student class contains methods: study, exam

/** * Student class* Attributes: student ID, name, age, major* Method: study exam* @author wgy * */public class Student {//attribute//student ID String stuNo ; //Name String name; //Age int age; //Professional String pro; //Method//Learning public void study() {System.out.println(name+"Is studying hard, student ID:"+stuNo) ;} //Exam public void exam() {System.out.println(name+"To take"+pro+"exam"); ​​}}

?

Section 2: Object Creation and Memory Analysis

2.1 Object Creation

? Object The creation process is also called the instantiation process of an object.

? Syntax: class name object name = new class name();

? call attributes:

? Object name. Attribute name

? Calling method:

? Object name. Method name (actual parameter);

//Demonstration object creation, cross-class invocation of member methods and access to member variables//Test class: The class containing the main function is called the test class public class TestPerson {public static void main(String[] args) {// 1. Create an object // Syntax: class name variable name = new class name (); Person xiaoM ing = new Person(); //2. Call object properties // Syntax: object. Property name xiaoMing.name = "小明"; xiaoMing.age = 10; xiaoMing.gender ='男'; //3. Call object Method//Syntax: object.Method name() xiaoMing.eat(); }}//Entity class: Represents a class with certain characteristics or certain behaviors//Describes the common characteristics and common behaviors of multiple objects/ /Requirements: Humans, with the characteristics of name, age, gender, etc., can eat, can run,,,, class Person{ //The first part // Member variable: Feature [noun] //Non-static member variable String name; //null int age;//0 char gender;// //Part two //Member method: behavior [verb] //Non-static member method public void eat() {System.out.println("eating ~~~~"); }}
2.2 Object memory allocation

When the program is running, the operating system will allocate three main memory spaces p>

  • Stack: directly store basic type data, and the address of reference type
    • The stack space is relatively small, and the access speed is relatively fast
    • < li>First-in-last-out

  • Heap: Store the actual data part of the reference type
    • The heap space is relatively large, and the access speed is relatively large. Relatively slow
  • Method area: save class information (including class name, method information, field information); method area There is a space called string pool (constant pool), which is used to store string constants; the other space is called static area, which is used to store static data.

    After jdk1.7, the constant pool and static area are part of the heap, and the concept of method area is weakened.

    2.3 Memory analysis

Objects in memory

? Note: The Person type variable defined in the program is actually a reference, which is Stored in the stack memory, he points to the actual Person object stored in the heap memory.

2.4 exercise
//test class public class Test {public static void main(String[] args) {/ /Requirements: School starts, Teacher Wang asks students Xiaoming, Xiaohua, Xiaoli to introduce themselves //Name, age, hobbies, and a talent show/* Teacher characteristics: Name behavior: Let students introduce themselves*/ //1 .Create a teacher object Teacher wang = new Teacher(); wang.name = "王老师"; wang.knowStudent(); }}//Teacher class public class Teacher{ String name; //Get to know students public void knowStudent( ) {Student stu=new Student(); stu.name = "Xiao Ming"; stu.age = 10; stu.hobby = "Blowing Bulls"; stu.introduce(); stu.dance(); stu.sing() ; }}//Student class public class Student{ String name; int age; String hobby; public void introduce() {System.out.println("I am" + name + "this year" + age + "hobby:" + hobby);} public void dance() {System.out.println("Dancing a square dance");} public void sing() {System.out.println("Come to a freeStyle"); }}

Section 3: Definition of Construction Method

3.1 Definition of the construction method

? The construction method is also called the constructor, which refers to when an object is instantiated (to create an object), The first method to be called

? Syntax:

Access permission modifier class name (parameter list) {//Method body}

< p>? Common method:

Access permission modifier Other modifier Return value type Method name (parameter list) {}

? Note: a. Construction The method has no return value type, and the constructor is automatically called in the process of instantiating the object.

? b. If you do not write a construction method, the system will provide us with a parameter-free construction method by default. If a construction method is added, the system will no longer provide a default construction method.

3.2 Invoke the construction method
//Demonstrate the use of the construction method public class Test {public static void main(String[] args) {//Create animal Animal dog=new Animal(); //Use attribute dog.color="yellow"; dog.nickname="wangcai"; dog.age=3; //Use Method dog.eat(); }) public class Animal {//color String color; //nickname String nickname; //age int age; //default construction method public Animal() {} //eat public void eat() {System.out.println(nickname+"guts and mouths"); }}
3.3 Difference between construction method and ordinary method

? a. The construction method is automatically called during the process of creating the object, and the ordinary method can only be called manually.
? b. The construction method has no return value type [note the difference between the return value void], the ordinary method The return value type is either a certain type or void
? C. The system will provide us with a parameterless construction method by default, and ordinary methods can only be manually added
? D. The method name of the construction method must be the same The corresponding class names are consistent
? E. The construction method will be executed in the process of creating the object, and each object is executed only once. For ordinary methods, they are executed only when they need to be used, and a Objects can be called multiple times

3.4 Construction method overload

? Method overload: In the same class, the method name is the same, The parameter list is different

? The parameter list is different: the number is different, the type is different, and the order is different.

? It has nothing to do with the return value modifier.

? Construct method overload : In the same class , The construction method name is the same, the parameter list is different

? The parameter list is different: the number is different, the type is different, and the order is different

? The method name and the class name are the same.

Default construction method: You can only create an object and cannot do any initialization operations. If you want to initialize properties when creating an object, you need to add a construction method with parameters to initialize the properties of the object. If there are multiple constructors in a class, this is constructor overloading. 
//Demonstrate the overloading of the construction method//Test class public class Test {public static void main(String[] args) {//Direct assignment/* Dog maomao = new Dog (); maomao.name = "毛毛"; maomao.age = 3; maomao.lookHome(); */ //Assign Dog dahuang = new Dog("大黄",5); dahuang.lookHome(); }}//Entity class public class Dog{ //Member variable String name; int age; //No parameter construction method public Dog() {} //Construction method with parameters, the parameters are generally set to the parameters related to member variables public Dog(String n,int a) {//Assign a member variable name = n; age = a;} /* public Dog(String n) {name = n;} */ //Member method public void lookHome() {System.out.println(name + "housekeeping"); }}
3.5 exercise
//scene: rich second generation Wang Sicong is driving The newly bought white BMW ran on the road and proudly showed off the car to his new girlfriend.
/*
Rich second-generation class
Feature: Amount of name and money
Behavior: Driving, showing off

Cars
Features: Color, Brand
Behavior: Running

Girlfriends
Features: name, age, color Value
*/
//Test class public class Test {public static void main(String[] args) { //1. Create a rich second-generation object RichMan wang = new RichMan("王思聪",true); wang.drive(); wang.show(); }}/*rich second-generation features: name money behavior: Drive, show off the car*/public class RichMan{ //Member variable String name; double money; //The default construction method public RichMan() {} //The parameter construction method public RichMan(String n,double m) {name = n ; money = m;} //Member method public void drive() {Car c=new Car(); c.brand="BMW"; System.out.println(name + "Drive a luxury car" + c.brand );} //Show public void show() {Car c=new Car(); c.brand="BMW"; GirlFriend gf=new GirlFriend(); gf.name="凤姐"; System.out.println( name + "向" + gf.name + "Show off luxury cars" + c.brand); }}/*Car features: color, brand behavior: running*/public class Car{ //member variable String color; String brand ; //Construction method public Car() {} public Car(String c,String b) {color = c; brand = b;} //Member method public void run() {System.out.println("a car" + color + "的" + brand + "On the run"); }}/*Girlfriend class feature: name*/public class GirlFriend{ //Member variable String name; //Construction method public GirlFriend(){} public GirlFriend(String n) {name = n;} }

Section 4: this keyword

this: represents the reference of the current object.

4.1 this.attribute
accessing member variables of this class

? Function: In order to distinguish the situation where the member variable and the formal parameter variable have the same name

The default value of the member variable:

? Reference type: null

? Basic type :Byte short int :0

? long: 0L

? float: 0.0f

? double:0.0

? char: 0 or’’

? boolean:false

4.2 this.method

? Other methods to access this class

4.3 exercise
//Demonstrate the use of this public class Test {public static void main(String[] args) {// Cat maomao = new Cat("毛毛",10); maomao.show1(); }} public class Cat{ String name;//nickname int age;//age String color ;//Color//3.this() public Cat() {System.out.println("The construction method without parameters is called");} //1.this. attribute public Cat(String name,int age, String color) {this.color = color; this.name=name; this.age=age; System.out.println("The construction method with parameter 2 is called");} //2.this. method//normal Method public void show1() {//Call in this class When using methods, this can be omitted this.show2();} public void show2() {}}
4.4 this() (Understanding)

< p>? Access to other construction methods in this class

? Note:

? (1) This (parameter) can only be used in the construction method, and must be the first statement

? (2) This (parameter) can only be called once

public class Test {public static void main(String[] args) { System.out.println("Hello World!"); }}public class Dog{ String name; int age; int num; String hobby; //Improve the readability and maintainability of the code//Construction method public Dog( ) {} public Dog(String name) {this.name = name;} public Dog(int age) {this.age = age;} public Dog(String name,int age) {this.name = name; this.age = age;} public Dog(String name,int age,int num,String hobby) {this(name,age); this.num = num; this.hobby = hobby; }}

Summary

1 Object-oriented thinking: A way of thinking that focuses on finding a specific individual with special functions, and then entrusting that individual to do something. 

Process-oriented thinking: a way of thinking that focuses on the process and steps of problem-solving.

The concept of two categories: a collection of entities with the same properties and functions.

3 Object: an individual with a specific special function.

4 The relationship between a class and an object: A class is an abstract or template of an object, and an object is a concrete or instance of a class.

5 Define the class

public class Person{

//Member variables: attributes

//Methods: behavior Function

}

6 Create object

Class name object name=new class name();

// Use object properties and methods

Object name.Property name

Object name.Method name();

Object memory allocation

Stack: relatively small, block access speed, features: first in and out

heap: relatively large space, slower storage speed.

Method area (static area, string pool (constant pool), code segment)



7 Construction method: method called when creating an object .

Public class name(){

}

Default constructor

Construction method with parameters

Construction method overloading

8 this keyword: a reference to the current object

this.attribute

this.method

this();//Call other construction methods

Silent Writing

1. What is a method?

2. The syntax of the definition method?

3. What is method overloading?

homework

1: Describe objects according to requirements

? 1. Write at least three in the kitchen Class and create an object to run.

? 2. Write at least three classes in the classroom, and create objects to run.

? 3. Write at least three animal classes in the zoo, and create objects to run.

Two: Use object-oriented thinking to describe the following scenes

? 1. Xiaomei walks away in Chaoyang Park [Note: Wangcai is a dog]

? 2. Xiao Ming is running in the Olympic Park wearing white Xtep shoes

? 3. Teacher Zhao is giving lectures on the podium, Xiao Gang is taking notes seriously at the lecture

? 4. Zhang Auntie Li and Auntie Li bought Red Fuji at Wumart Supermarket.

Interview Question

1. What is object-oriented? What is the difference between object-oriented and process-oriented

2. The difference between construction method and ordinary method

3. The role and use of this keyword

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2846 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.