当前位置:网站首页>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] grub default password
- Write a program to define an array with 10 int elements, and take its position in the array as the initial value of each element.
- First article
- LVS+Keepalived实现四层负载及高可用
- Getting started with window functions
- 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
- Communication layer of csframework
- 三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
- /*Write a loop to output the elements of the list container in reverse order*/
- Strings and characters
猜你喜欢
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
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
Analysis function in SQL
[Galaxy Kirin V10] [server] KVM create Bridge
Canoe the second simulation engineering xvehicle 3 CAPL programming (operation)
XMIND installation
Canoe - the third simulation project - bus simulation - 3-1 project implementation
Introduction to tree and binary tree
Collection of practical string functions
Network connection (III) functions and similarities and differences of hubs, switches and routers, routing tables and tables in switches, why do you need address translation and packet filtering?
随机推荐
Analysis function in SQL
[untitled]
[Galaxy Kirin V10] [desktop] login system flash back
[Galaxy Kirin V10] [server] system startup failed
Appscan installation steps
Elevator dispatching (pairing project) ①
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
Canoe - the second simulation project -xvihicle1 bus database design (operation)
Canoe - the third simulation project - bus simulation - 3-1 project implementation
Huge number multiplication (C language)
[Galaxy Kirin V10] [desktop] cannot add printer
Knapsack problem and 0-1 knapsack problem
Advanced order of function
Heartbeat报错 attempted replay attack
Postman interface test
Performance test method
本地Mysql忘记密码的修改方法(windows)[通俗易懂]
3W word will help you master the C language as soon as you get started - the latest update is up to 5.22
unit testing
Using Lua to realize 99 multiplication table