Object-oriented programming (oop)
-
Object-Oriented Programming (OOP)
-
The essence of object-oriented programming is to organize code as classes and data as objects.
-
abstract
-
Three main features: encapsulation, inheritance, polymorphism
-
From an epistemological point of view, there are objects before classes. Objects are concrete things, classes are abstract, and objects are abstract.
-
From the point of view of code operation, there are classes before objects, and classes are templates for objects.
Retrospective methods and deepening
-
Definition of method
-
Modifier
-
Return type
-
break: the difference between jumping out of switch, ending loop and return ing
-
Method Name: Note specifications, see names
-
Parameter list: (parameter type, parameter name)...
-
throws
-
-
Method calls:
-
Static method
-
Non-static method
package com.oop; import com.xiang.method.Test; public class Demo02 { public static void main(String[] args) { //Class name. Method, static methods of other classes can be called directly Student.say(); //Calling a non-static method requires new to instantiate the class //Object type object name = object value; Student student=new Student(); student.test(); } }
package com.oop; public class Student { //Static method static public static void say(){ System.out.println("speak"); } //Non-static method public void test(){ System.out.println("hello"); } }
Note: Both methods must be static or non-static before they can be called. Since a method does not exist until it is instantiated, calling a method that does not exist with a static method will result in an error.
-
Formal and actual parameters
-
Value and Reference Passes
package com.oop; //pass by value public class Demo04 { public static void main(String[] args) { int a=1; System.out.println(a); //1 Demo04.change(a); System.out.println(a); //1, the change method does not return a value, after jumping out of the change method, the value of a is still 1 } //a can be assigned when calling the method, but there is no return value here public static void change(int a){ a=10; } }
package com.oop; //Reference Pass: Pass Object, Essential or Value Pass public class Demo05 { public static void main(String[] args) { Person person = new Person(); //Instantiation process System.out.println(person.name); //null change(person); System.out.println(person.name); //Zhang San } public static void change(Person person){ //Person is an object pointing to - "Person person=new Person (); This is a specific person who can change attributes! person.name="Zhang San"; } } //Multiple classes can be defined within a class, but only one public class //Defines a Person class with an attribute name. (The fields below the class are called attributes) class Person{ String name; //null }
-
this keyword: indicates its own class
-
Relationship between Classes and Objects
- Class is an abstract data type. It describes and defines a certain kind of things, but it cannot represent a specific thing.
-
Person class, Pet class, etc. are used to describe and define the characteristics and behavior of a specific kind of things.
-
- Objects are concrete instances of abstract concepts
-
It is a concrete instance, not an abstract concept, that reflects characteristics and functions.
-
Creating and initializing objects
-
Create objects using the new keyword
-
When created using the new keyword, in addition to allocating memory space, default initialization of created objects and calls to constructors in classes are also made
package com.oop.demo02; //Student Class public class Student { //Attribute: Field String name="Someone"; int age=3; //Method public void study(){ //this represents the current class of Student System.out.println(this.name+"I'm learning"); } }
package com.oop.demo02; //A project should have only one main method public class Application { public static void main(String[] args) { //Class: abstract, needs instantiation //A class instantiates and returns its own object //xiaoming,zhangsan is a concrete instance of the Student class Student xiaoming=new Student(); Student zhangsan=new Student(); xiaoming.name="Xiao Ming"; xiaoming.age=15; System.out.println(xiaoming.name); //Xiao Ming System.out.println(xiaoming.age); //15 System.out.println(zhangsan.name); //Someone System.out.println(zhangsan.age); //3 } }
-
Constructors in classes also become construction methods, which must be called when creating objects, and have the following two advantages:
-
Must have the same name as the class.
-
There must be no return type and no void to write.
-
Construction method
package com.oop.demo02; // public class Person { //A class, even if it writes nothing, automatically generates a method with the same name. This is called a construction method. //public Person() {} String name; //Instantiate Initial Value //Action 1. With the new keyword, you essentially call the constructor //2. Used to initialize values public Person(){ name="Zhang San"; } //Parametric constructs: once a parametric construct is defined, a definition must be displayed without a parameter public Person(String name){ this.name=name; //The first name represents the property of the class itself, and the second is the parameter name of the method } //alt+insert Shortcut to Generate Constructor //Default builds parameterized, click select null to generate parameterized } /* Constructor features: 1.Same as class name 2.no return value Effect: 1.new Essentially calling a construction method 2.Initialize the value of the object Note: After defining a parameterized construct, if you want to use a parameterized construct, you need to show the definition of a parameterized construct Alt+Insert Shortcut keys this.= Represents the current class */
package com.oop.demo02; //A project should have only one main method public class Application { public static void main(String[] args) { //new instantiates an object Person person=new Person(); // Person person=new Person("Li 4"); //will select the method to invoke based on the parameter, where the parameter is invoked System.out.println(person.name); //Zhang San } } /* //Class: abstract, needs instantiation //A class instantiates and returns its own object //xiaoming,zhangsan Is a concrete instance of the Student class Student xiaoming=new Student(); Student zhangsan=new Student(); xiaoming.name="Xiao Ming "; xiaoming.age=15; System.out.println(xiaoming.name); //Xiao Ming System.out.println(xiaoming.age); //15 System.out.println(zhangsan.name); //Someone System.out.println(zhangsan.age); //3 */
Create Object Memory Analysis
static methods and classes are loaded together and can be called by objects
package com.oop.demo03; public class Pet { public String name; public int age; //Default construct with or without parameters public void shout(){ System.out.println("Gave a call"); } }
package com.oop; import com.oop.demo03.Pet; public class Application { public static void main(String[] args) { Pet dog=new Pet(); dog.name="Prosperous Money"; dog.age=3; dog.shout(); System.out.println(dog.name); System.out.println(dog.age); Pet cat=new Pet(); } }
summary
-
Classes and object classes are templates: abstract, objects are concrete instances
-
Method definition, call!
-
Reference Reference Type of Object: Base Type (8) Objects are manipulated by reference: Stack-"Stack
-
Attribute: Default initialization of Field field Field member variable: Number: 0.0 char:u0000 boolean:false reference: null
Modifier Attribute Type Attribute Name=Attribute Value!
-
Creation and use of objects
-
Objects must be created using the new keyword, provided the constructor Person zhangsan=new Person();
-
Object property zhangsan.name
-
Object's method zhangsan.sleep()
-
-
Class: Static Attribute Attribute Dynamic Behavior Method