当前位置:网站首页>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;
}
边栏推荐
- Performance features focus & JMeter & LoadRunner advantages and disadvantages
- /*Write a loop to output the elements of the list container in reverse order*/
- Crawl Zhejiang industry and trade news page
- Deepmind proposed a Zuan AI, which specially outputs network attack language
- Write a program that uses pointers to set all elements of an int array to 4.18: 0.
- Replace() function
- Appscan installation steps
- Elevator dispatching (pairing project) ①
- Oracle11g | getting started with database. It's enough to read this 10000 word analysis
- VPS安装Virtualmin面板
猜你喜欢
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
DNS hijacking
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
[Galaxy Kirin V10] [server] system partition expansion
Article publishing experiment
Application and Optimization Practice of redis in vivo push platform
TS type gymnastics: illustrating a complex advanced type
[Galaxy Kirin V10] [server] FTP introduction and common scenario construction
JMeter assembly point technology and logic controller
Deepmind proposed a Zuan AI, which specially outputs network attack language
随机推荐
MBG combat zero basis
Cacti主机模板之定制版
MFC document view framework (relationship between classes)
[Galaxy Kirin V10] [server] iSCSI deployment
[Galaxy Kirin V10] [server] NFS setup
Canoe - the second simulation engineering - xvehicle - 2panel design (principle, idea)
Unittest+airtest+beatiulreport combine the three to make a beautiful test report
Introduction to canoe automatic test system
TS type gymnastics: illustrating a complex advanced type
[Galaxy Kirin V10] [desktop] login system flash back
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
[Galaxy Kirin V10] [server] set time synchronization of intranet server
regular expression
Read a piece of text into the vector object, and each word is stored as an element in the vector. Convert each word in the vector object to uppercase letters. Output the converted elements in the vect
Snake (C language)
PHP programming language (1) - operators
20 minutes to learn what XML is_ XML learning notes_ What is an XML file_ Basic grammatical rules_ How to parse
Ten key performance indicators of software applications
1. Circular nesting and understanding of lists
Oracle11g | getting started with database. It's enough to read this 10000 word analysis