Introduction to object oriented
Object oriented is relative to process oriented , Process oriented is a process oriented approach “ The process ” Programming idea of center , Analyze the steps needed to solve the problem , Then use functions to implement these steps step by step , When using it, you can call it one by one . Object oriented , Is a kind of “ object ” Programming idea of center , Think of problems as objects , Through the properties and behavior of objects , Solve the problem . Process oriented emphasizes modularity , Object oriented emphasizes the objectification of things .
In terms of complexity, object-oriented is based on “ object ” Building systems for basic units , Object is to put data and data manipulation methods together , As an interdependent whole . Abstracts the same kind of object from its generality , Formative class . So a class is an abstraction of an object , Object is the embodiment of class materialization ( Classes are large-scale , A class can have multiple objects ), Most of the data in the class can only be implemented through the methods of this class , Class contains description and implementation , The explanation part is seen by the outside world , Communicate with the outside world through simple external interface ; The implementation part is not seen by the outside world , Internally, through different methods of construction , Realize different functions . Objects communicate with each other through messages , The procedure flow is determined by the user in use .
Object oriented is used to solve more and more complex requirements , The software reusability is improved , Flexibility and expansibility .
static keyword
static Express “ static state ” It means , Can be used to decorate member variables and member methods . static The main function of is to create domain variables or methods independent of specific objects Simple understanding : By static Keyword decorated methods or variables do not need to rely on objects for access , As long as the class is loaded , You can access it through the class name , It does not create multiple copies of data in memory due to multiple creation of objects .
- Static members Load and initialize when the class is loaded .
- No matter how many objects exist in a class , Static properties , There is only one copy in memory forever ( It can be understood that all objects are common )
- During the visit : Static cannot access non static , Non static can access static !
Three ways to call static methods :
1.new xx(). static state (); // Using object calls , Not recommended
2.xx. static state (); // Class name call , Normal call method , recommend
3. static state (); // Static methods of this class are called in this class , Call directly
matters needing attention :
When the same package is not the same kind of call , Can directly Class name . Method () call , No need to create objects .
When different packages are called , It needs to be called again .
Code example :
package com.vedio.highobject;
public class Pool {
public static int water=0;// Static variables
public static void outlet() {// water
if(water>=2) {
water=water-2;
}else {
water=0;
}
}
public static void inlet() {// inlet
water=water+3;
}
public static void main(String[] args) {
System.out.println(" Water volume of the pool :"+water);
System.out.println(" Two times of water injection into the pool .");
Pool.inlet();
Pool.inlet();
System.out.println(" Water volume of the pool :"+water);
System.out.println(" Let water out of the pool once .");
Pool.outlet();
System.out.println(" Water volume of the pool :"+water);
}
}
Be careful : When you call static and static methods in classes, , Class name is required . Variable name or class name . Method name call , When you call static and static methods in this class, , No class name is allowed .
instanceof keyword
There is only inheritance , Can be used instanceof keyword , Otherwise, an error will be reported
Code example :
// There is only inheritance , Can be used instanceof keyword , Otherwise, an error will be reported
public class Computer {// Computer
public static void main(String[] args) {
Pad ipad=new Pad();
LenovoPad lenovoPad=new LenovoPad();
System.out.println("Pad Inherited from computer ?"+(ipad instanceof Computer));
System.out.println("LenovoPad Inherit from Pad?"+(lenovoPad instanceof Pad));
System.out.println("Pad Inherit from LenovoPad?"+(ipad instanceof LenovoPad));
// System.out.println("Pad Is it inherited from mankind ?"+(ipad instanceof Person));//ipad And Person This class does not have any inheritance relationship
}
}
class Pad extends Computer{// The tablet
}
class LenovoPad extends Pad{// Lenovo tablet
}
public class Person {
public Person(String name){
System.out.println(" Hello! , My name is "+name);
}
}
ipad and Person There is no inheritance between the two classes , So use instanceof when , Will report an error directly .
euqals() Method
see equals() Source code , as follows
public boolean equals(Object obj) {
return (this == obj);
}
So in object Class equals Method can only be used to compare whether the address is the same , And == It works the same . But when we can rewrite it ourselves equals Method , Make it meet our needs .
Let's define a Person class , Only id and name When it's all the same , To make sure they're the same person . Code example :
package com.vedio.highobject;
public class Person {
String name;
String id;
public boolean equals(Person p) {
return this.name.equals(p.name)&&this.id.equals(p.id);
}
}
package com.vedio.highobject;
public class Demo1 {
public static void main(String[] args) {
Person p1=new Person();
Person p2=new Person();
Person p3=new Person();
p1.name=" Xiao Ming ";
p1.id="123";
p2.name=" Xiaohong ";
p2.id="123";
p3.name=" Xiao Ming ";
p3.id="123";
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p3));
System.out.println(p2.equals(p3));
}
}
Can be observed , Only id and name Exactly the same , Only then true. By rewriting equals Method , Meet our needs .
equals And == The difference between
String have access to equals The address is compared because it rewrites equals Method
see String Yes equals Method , as follows :
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
equals Is to determine whether the values of two variables or instances pointing to the same memory space are the same , and == Is to determine whether two variables or instances point to the same memory space ,
Take a popular example ,== It's to determine whether two people live in the same address , and equals It's to judge whether people living in the same address are the same
Code example :
package com.vedio.highobject;
public class Demo2 {
public static void main(String[] args) {
String s1=new String("123");
String s2=new String("123");
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
String s3="123";
String s4="123";
System.out.println(s3.equals(s4));
System.out.println(s3.equals(s1));
}
}
toString() Method
package com.object.high;
// The parent of all classes --Object class
// stay Java in , All classes inherit directly or indirectly java.lang.Object class
// Because all classes are Object Subclasses of classes , So any class can be overridden Object Methods in class
public class Hello {
// rewrite toString() Method
public String toString(){
return "Say \""+getClass().getName()+"\" to Java";//getClass().getName() Get the name of the class
}
public static void main(String[] args) {
System.out.println(new Hello());// When printing a class object , The overridden toString() Method
}
}
Last
Thank you for seeing here , What's wrong with the article , If you think the article is helpful to you, please give me a compliment , Share every day java Related technical articles or industry information , Welcome to follow and forward the article !