当前位置:网站首页>Understanding of object
Understanding of object
2022-07-04 10:58:00 【Black demon fairy moon】
One 、object Class summary
1.object Feature 1 :
object It can solve the problem of parameter unification , in other words object Parameters of all data types can be accepted .
Object o=new Person1();// Upward transformation
/** * instanceof see o Is it right? Person1 Instantiation */
if(o instanceof Person1){
Person1 per=(Person1)o;
System.out.println("Person The downward transformation was successful ");
}
class Person1 extends Object{
}
// result :Person The downward transformation was successful
2.object Two commonly used functions of
(1)toString
toString yes ptintln The method that will be called by default , So we usually export toString Omit
toString stay Object Generally, the address value of the object is directly different , The name of the output class [email protected]+16 Hexadecimal hash code, such as :
Person1 pa=new Person1(" Fu Jiayue ",23);
System.out.println(pa);
cn.tedu.exercise.Person1@2519ca98
So if we want to output the attributes of the object directly , We need to rewrite in the corresponding class toString Methods ( stay idea Can be directly Generate->toString), But if we want to be in Person1 Rewriting in toString The method is also very simple :
public String toString(){
return "name"+name+"age"+age;
}
(2)equals
equals stay Object By default == The way :
== Generally, there are two types , If there are data classes before and after , Our comparison is numerical , If it's an object , Our comparison is the address value . Data types are left aside , Let's take the object as an example :
Person1 pa=new Person1(" Fu Jiayue ",23);
Person1 pb=new Person1(" Fu Jiayue ",23);
System.out.println(pa.equals(pb));
The result is :
false
If we want to judge whether the properties of two objects are equal , We must rewrite equals Method ( stay idea Can be directly Generate->equals() and hashcode() Generate equals Method ), If we want to rewrite equals Methods can refer to the following methods , Of course, the method is not just one .
class Person1 extends Object{
String name;
int age;
@override
public boolean equals(Object obj){
/** * Judge whether the two are the same class , such as pa.equals(" Dog ") */
if(!(obj instanceof Person1)){
return false;
}
/** * Judge whether the incoming object is empty, that is null, In this case, a null pointer error will occur */
if(obj==null){
return false;
}
/** * Judge whether they are the same object */
if(this==obj){
return true;
}
Person1 per = (Person1) obj;//obj There's no person Properties of , In order to obtain person Properties of
return this.name.equals(per.name)&&this.age==per.age;
}
边栏推荐
- Add t more space to your computer (no need to add hard disk)
- F12 clear the cookies of the corresponding web address
- Summary of several job scheduling problems
- Get the data of the top 100 headlines today with Tianxing data
- [test theory] test process management
- How do microservices aggregate API documents? This wave of show~
- Virtual machine configuration network
- Linked list operation can never change without its roots
- Huge number multiplication (C language)
- DNS hijacking
猜你喜欢

Elevator dispatching (pairing project) ④

MFC document view framework (relationship between classes)
![[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing](/img/72/5e725a44a50f152b477a4b2907a2d0.jpg)
[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing

Appscan installation error: unable to install from Net runtime security policy logout appscan solution

Recursion and divide and conquer strategy

Introduction to Lichuang EDA

Function introduction of canbedded component

JMeter Foundation

Jemeter script recording

How do microservices aggregate API documents? This wave of show~
随机推荐
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
Network connection (II) three handshakes, four waves, socket essence, packaging of network packets, TCP header, IP header, ACK confirmation, sliding window, results of network packets, working mode of
Common system modules and file operations
[Galaxy Kirin V10] [desktop] can't be started or the screen is black
The bamboo shadow sweeps the steps, the dust does not move, and the moon passes through the marsh without trace -- in-depth understanding of the pointer
Communication layer of csframework
51 data analysis post
[Galaxy Kirin V10] [desktop] build NFS to realize disk sharing
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
Failed to configure a DataSource: ‘url‘ attribute is not specified... Bug solution
Installation of ES plug-in in Google browser
/*Write a function to open the file for input, read the contents of the file into the vector container of string class 8.9: type, and store each line as an element of the container object*/
Replace() function
shell awk
F12 clear the cookies of the corresponding web address
VPS安装Virtualmin面板
Locust installation
Performance test method
Write a program that uses pointers to set all elements of an int array to 4.18: 0.
DDL language of MySQL database: create, modify alter, delete drop of databases and tables