Object oriented overview
First, what is object-oriented?
Object oriented is relative to process oriented. It refers to organizing relevant data and methods into a whole, and modeling the system from a higher level, so as to be closer to the natural operation mode of things.
Transformation from process oriented to object-oriented thinking:
Process oriented focuses on the process of execution, and object-oriented focuses on the objects with functions.
Process oriented to object-oriented is the change of programmer's thought from executor to commander.
for instance:
Q:
How many steps does it take to put an elephant in the fridge?
answer:
Process oriented answer:
Step 3: 1
Open the refrigerator door,
2
Put the elephant in,
3
Close the refrigerator door
Object oriented answer:
Step 2: 1
Recruit a worker (object) who can operate the refrigerator,
2
Command workers to load elephants
reflection:
If the question is changed to: put 100
How many steps are there for elephants to close into the refrigerator in turn?
Process oriented answer: it needs to be omitted here
N
Word...
Object oriented answer or
2
Step:
1
Recruit a worker (object) who can operate the refrigerator,
2
Command the workers to load the elephants in turn.
So simply put, process oriented is a step-by-step process. Object oriented is a process of writing repetitive things into an assembly method and calling this method with an object.
Relationship between class and object
The relationship between the two
Class represents a product of commonness, which is a comprehensive feature, while object is a product of individuality and an individual feature.
(similar to the concept of drawings and real objects in life.)
A class can only be used through an object, and all operations of the object are defined in the class.
Class consists of properties and methods:
Attribute: it is equivalent to the characteristics of people {String, int, double...}
Method: it is equivalent to people's behaviors one by one, such as talking, eating, singing and sleeping (void say() {...})
Creation of classes and objects
Class definition format
class
Class name
{
Member properties
Member method
}
Where attributes and methods
Attribute definition format:
Data type property name
;(String name;)
Format of attribute definition and assignment:
Data type property name
=
initialize value
; (String name = "Zhang San"
Method definition format:
Permission modifier return value type
Method name
(
Formal parameter list
){
/ / method body
return
Return value;
}
package last302; public class Bleiyuduixiangchuanjian1 { public static void main(String[] args) { //Format for creating objects: Person p =new Person(); //Object attribute assignment //Format: object name Attribute name = value; p.name="Zhao Xinkun"; p.age=22; p.sex='male'; p.say(); int s =p.sum(1124,813); System.out.println(s); } } /** * A class is a drawing * */ //Write create class class Person{ //Properties -- properties String name; int age; char sex; //Method behavior /** * Define format: * Return value type method name (formal parameter table){ * Method body * return Return value; * } * * * Call format: * Object name Method name (actual parameter list); * * */ //Null return value method void say(){ System.out.println("My name is:"+name+"My age is:"+age+"My gender is:"+sex); }; //Method with return value type int sum(int x,int y) { int z =x+y; return z; }; }
objects creating
If a class wants to really operate, it must rely on objects. The definition format of objects is as follows
:
Class name object name = new
Class name
() ;
If you want to access a property or method (definition of a method) in a class, you can rely on the following syntax form:
Accessing properties in a class: object
attribute
;
Call method in class: object
method
(
Actual parameter list);
package last302; public class Cduixiangchuangjianbuchong { public static void main(String[] args) { Book b1 = new Book(); Book b2 = new Book(); b1.name = "Golden Apple"; b1.info = "It tells the hard journey of fruit farmers planting golden apples."; b2.name = "Silver Apple"; b2.info = "It tells the hard journey of fruit farmers planting silver apples."; b1.say(); b2.say(); } } class Book{ String name; String info; void say() { System.out.println("The name of the book is:"+"Introduction to the book:"+info); } }
Object creation memory
This involves the knowledge of stack, heap and method area. Please understand it by yourself. Thank you.
package last302; public class Dduixiangchaungjianneicun { public static void main(String[] args) { Boook b1 = new Boook(); b1.name = "The Plum in the Golden Vase"; b1.info = "Educational books"; b1.say(); Boook b2 = b1; b2.name = "Hey, hey, hey"; b1.say(); } } class Boook{ String name; String info; void say(){ System.out.println("title:"+name+",Introduction:"+info); } }
package last302; public class Eduixiangchuanjianneicun03 { public static void main(String[] args) { red b1 = new red(); b1.name = "The Plum in the Golden Vase"; b1.info = "Hey, hey, hey"; red b2 = new red(); b2.name = "Water Margin"; b2.info = "The story of Liangshan hero"; b2 = b1; b2.name = "Journey to the West"; b1.say(); //The main explanation here is about the details of stack, heap and method area, as well as the mechanism of GC garbage collection } } class red{ String name; String info; void say() { System.out.println("title"+name+",Introduction:"+info); } }
Construction method
Review the creation of objects
Person p = new Person();
On the right
Person
Parentheses that appear after
,
It's actually calling the constructor
!
summary
effect:
Used for object initialization.
Execution time:
When creating objects
,
Automatic call
characteristic:
be-all
Java
There will be at least one constructor in the class
If there is no explicit writing construction method in a class
,
The compiler will automatically generate a parameterless constructor
,
There is no code in the constructor!
If you write any constructor yourself
,
The compiler will no longer automatically generate parameterless constructors.
Define format
It is basically the same as the ordinary method
,
The difference is
:
The method name must be the same as the class name
,
No declaration of return value type
!
//case public class Demo3{ public static void main(String[] args){ Person p = new Person(); p = new Person(); p = new Person(); p = new Person(); } } class Person{ public Person(){ System.out.println("Object creation,This method calls"); } }
package last302; public class Fgouzaofangfa { public static void main(String[] args) { System.out.println("1"); Person2 p1 = new Person2("I'm Zhang San"); System.out.println("2"); //p1.name = "Zhang San"; p1.age = 18; p1.say(); } } class Person2{ //Here, when we do not set a construction method, the system will automatically claim a parameterless construction method String name; int age; //Now let's set up a construction method with parameters and try it Person2(String n){ //System.out.println("I executed"); name = n; } void say(){ System.out.println("self-introduction I am"+name+"My age is:"+age); } }
be careful:
It is recommended to customize the parameterless construction method and do not rely on the compiler to avoid errors.
When there are non constant member variables in the class, it is recommended to provide two versions of construction methods, one is the nonparametric construction method, and the other is the construction method with all attributes as parameters.
When no member of the class is a constant or no member of the class is recommended.
Overloading of methods
Heavy load condition:
Same method name
,
Parameter type or parameter length is different
,
Method overloading can be completed
!
Method overloading is independent of the return value
!
Overloading of methods
,
Can let us under different needs
,
Call methods by passing different parameters to complete specific functions.
package last302; public class Gfnagfadechongzai { public static void main(String[] args) { Math b1 = new Math() ; int num = b1.sum(100, 120); System.out.println(num); double num2 = b1.sum(100.11, 120.22); System.out.println(num); } } /** * Overloading of methods * First of all, the naming of classes should be standardized to see the meaning * * * Methods defined in a class are allowed to be overloaded (the same method name) * * 1.Same method name * 2.Parameter list length or parameter list type (different order of parameter types) * * Note: it is independent of the return value type*/ class Math{ int sum(int x, int y){ int z = x +y; return z; } double sum(double x, double y){ double z = x +y; return z; } double sum(double y ,int x) { double z = x + y; return z; } }
Construction method overload
A class
,
There can be multiple construction methods
:
The overload of the construction method can be completed if the length or type of the parameter list is different.
Overloading of construction methods
,
It allows us to create objects under different requirements
,
Call different methods to complete the initialization of the object
!
package last302; public class Hgouzaofangfadechongzai { /** * There can be multiple construction methods for a class: * If the length or type of the parameter list is different, the overload of the construction method can be completed * We can call different methods to complete the initialization of objects under different needs of creating objects! * */ public static void main(String[] args) { Person3 z = new Person3("Zhang San",18); z.say(); Person3 x = new Person3("Zhao Si"); x.say(); } } class Person3 { Person3(String name2,int age2){ name = name2; age = age2; } Person3(String name2){ name = name2; } String name; int age; void say() { System.out.println("name is" +name+"age is"+age); } }
Anonymous object
An object without an object name is an anonymous object.
Anonymous objects can only be used once. Because there is no object reference, they will be called garbage and wait to be used
G·C
Recycling.
Objects used only once can be completed by anonymous objects, which will be often used in future development.
package last302; public class Inimingduixiang { //An anonymous object is an object without a name public static void main(String[] args) { //According to the usual habit, we will declare and call objects in this way /** *Math2 m = new Math2(); *int num = m.sum(100, 200); * System.out.println(num); * */ //Now write a new, nameless object and call the method directly int num = new Math2().sum(100, 200); System.out.println(num); } } //First write a class class Math2{ int sum(int x,int y) { return x+y; } }
misunderstanding
package last302; public class Jnimingduixiang2 { public static void main(String[] args) { //Misunderstanding of creating objects new Person4().name = "Zhang San"; new Person4().age = 18; new Person4().say(); //This is not right, because creating it in this way is equivalent to creating three different spaces in heap memory } } class Person4{ String name; int age; void say() { System.out.println("introduce oneself to:"+name+"Age is"+age); } }