当前位置:网站首页>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;
}
边栏推荐
- [Galaxy Kirin V10] [server] iSCSI deployment
- Elevator dispatching (pairing project) ②
- Installation of ES plug-in in Google browser
- Add t more space to your computer (no need to add hard disk)
- [advantages and disadvantages of outsourcing software development in 2022]
- Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
- The last month before a game goes online
- TS type gymnastics: illustrating a complex advanced type
- Recursive method to achieve full permutation (C language)
- Ten key performance indicators of software applications
猜你喜欢

Notes on writing test points in mind mapping

Virtual machine configuration network
![[Galaxy Kirin V10] [desktop] cannot add printer](/img/a6/28e4aa31e805a018e6db2b32ca1be0.jpg)
[Galaxy Kirin V10] [desktop] cannot add printer

Canoe - the third simulation project - bus simulation - 3-2 project implementation

Personal thoughts on the development of game automation protocol testing tool

Hidden C2 tunnel -- use of icmpsh of ICMP

Replace() function

Open the neural network "black box"! Unveil the mystery of machine learning system with natural language

Add t more space to your computer (no need to add hard disk)

Oracle11g | getting started with database. It's enough to read this 10000 word analysis
随机推荐
Canoe - the third simulation project - bus simulation - 3-1 project implementation
XMIND installation
Regular expression
/*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*/
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
Application and Optimization Practice of redis in vivo push platform
Safety testing aspects
[Galaxy Kirin V10] [server] iSCSI deployment
Hidden C2 tunnel -- use of icmpsh of ICMP
Getting started with window functions
TS type gymnastics: illustrating a complex advanced type
Performance test process
regular expression
[machine] [server] Taishan 200
F12 clear the cookies of the corresponding web address
2、 Operators and branches
Canoe: distinguish VT, VN and vteststudio from their development history
JMeter correlation technology
MBG combat zero basis