当前位置:网站首页>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;
}
边栏推荐
- How to deal with the relationship between colleagues
- Day06 list job
- Swagger and OpenAPI
- R built in data set
- Summary of automated testing framework
- [Galaxy Kirin V10] [desktop] printer
- VI text editor and user rights management, group management and time management
- Capl: timer event
- 2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
- Getting started with window functions
猜你喜欢
![[Galaxy Kirin V10] [server] KVM create Bridge](/img/a4/a35a276d13e194cefc547607c59f00.jpg)
[Galaxy Kirin V10] [server] KVM create Bridge

Error C4996 ‘WSAAsyncSelect‘: Use WSAEventSelect() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARN
![[Galaxy Kirin V10] [server] soft RAID configuration](/img/d5/789387613fafc18f623d0cff45093b.jpg)
[Galaxy Kirin V10] [server] soft RAID configuration

Huge number multiplication (C language)

Digital simulation beauty match preparation -matlab basic operation No. 6

Add t more space to your computer (no need to add hard disk)
![[Galaxy Kirin V10] [desktop] cannot add printer](/img/a6/28e4aa31e805a018e6db2b32ca1be0.jpg)
[Galaxy Kirin V10] [desktop] cannot add printer

How do microservices aggregate API documents? This wave of show~

Canoe test: two ways to create CAPL test module

Send a request using paste raw text
随机推荐
Heartbeat启动后无反应
2022 ape circle recruitment project (software development)
Dictionaries and collections
Locust learning record I
[Galaxy Kirin V10] [server] set time synchronization of intranet server
[Galaxy Kirin V10] [server] soft RAID configuration
Snake (C language)
Hidden C2 tunnel -- use of icmpsh of ICMP
Ten key performance indicators of software applications
[Galaxy Kirin V10] [server] iSCSI deployment
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
Postman interface test
[Galaxy Kirin V10] [server] NUMA Technology
Performance test overview
Digital simulation beauty match preparation -matlab basic operation No. 6
Solaris 10网络服务
JMeter Foundation
Using SA token to solve websocket handshake authentication
DDL language of MySQL database: create, modify alter, delete drop of databases and tables
[advantages and disadvantages of outsourcing software development in 2022]