当前位置:网站首页>Kotlin introductory notes (VII) data class and singleton class
Kotlin introductory notes (VII) data class and singleton class
2022-07-05 09:14:00 【Meng Meng Mu Xi】
Preface : This tutorial is best done with JAVA Study on the basis of
One 、 Data class
java in :
stay java in , Data classes are often rewritten equals() 、hashCode() 、toString() Here are a few ways . among ,equals() Method is used to determine whether two data classes are equal .hashCode() Method as a equals() The supporting methods of also need to be rewritten . Otherwise, it will lead to HashMap、HashSet etc. hash The related system class does not work properly .toString() Method for clearer input logs , Otherwise, a data class will print out a line of memory address .
public class Cellphone {
private String brand;
private double price;
public Cellphone(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public int hashCode() {
return brand.hashCode() + (int)price;
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof Cellphone) {
Cellphone other = (Cellphone) obj;
return other.brand.equals(brand) && other.price == price;
}
return false;
}
@NonNull
@Override
public String toString() {
return "Cellphone(band=" + brand + ",price=" + price +")";
}
}
kotlin in :
data class Cellphone(val brand : String , val price : Double)
stay Kotlin in , need Kotlin It takes only one line to achieve
How I felt when I first saw this scene , Only two words can be used to describe : Out of line
When a class is declared in front of data Keywords , That means you want this class to be a data class ,Kotlin It will help you put... According to the parameters in the main constructor equals() 、hashCode() 、toString() And other fixed methods without practical logical significance , Thus greatly reducing the workload of development .
in addition , Because the function has no code , You can omit the trailing brace .
Let's test this data class , stay main Function to write the following code :
fun main() {
val cellphone1 = Cellphone("Samsung",1299.99)
val cellphone2 = Cellphone("Samsung",1299.99)
println(cellphone1)
println("cellphone1 equals cellphone2 " + (cellphone1 == cellphone2))
}
Print the results :
Two 、 Singleton class
java in :
public class Singleton {
private static Singleton instance;
private Singleton() {}
public synchronized static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
public void singletonTest(){
System.out.println("singletonTest is called.");
}
}
Kotlin:
Implement the singleton class :
object Singleton {
}
Added as above singleTest() To test
object Singleton {
fun singletonTest() {
println("singletonTest is called.")
}
}
It only takes threeorfour lines to realize and java Exactly the same effect ! Calling functions is also very simple :
Singleton.singleTest()
It's similar to Java Static method calls , But behind it Kotlin Helped us create Singleton Class , And ensure that there is only one Singleton example .
If you like this series , You might as well pay attention ! Thank you for watching !
Reference resources :
《 First line of code Android ( The third edition )》 --- Guo Lin
边栏推荐
- TF coordinate transformation of common components of ros-9 ROS
- Understanding rotation matrix R from the perspective of base transformation
- notepad++
- Oracle advanced (III) detailed explanation of data dictionary
- Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
- C # compare the differences between the two images
- Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
- Kubedm series-00-overview
- . Net service governance flow limiting middleware -fireflysoft RateLimit
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
猜你喜欢
Summary of "reversal" problem in challenge Programming Competition
图神经网络+对比学习,下一步去哪?
高性能Spark_transformation性能
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Newton iterative method (solving nonlinear equations)
【ManageEngine】如何利用好OpManager的报表功能
TF coordinate transformation of common components of ros-9 ROS
Wxml template syntax
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
My experience from technology to product manager
随机推荐
Causes and appropriate analysis of possible errors in seq2seq code of "hands on learning in depth"
我的一生.
一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
Ros- learn basic knowledge of 0 ROS - nodes, running ROS nodes, topics, services, etc
12. Dynamic link library, DLL
牛顿迭代法(解非线性方程)
Transfer learning and domain adaptation
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
2311. 小于等于 K 的最长二进制子序列
Programming implementation of ROS learning 5-client node
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
notepad++
asp. Net (c)
Summary of "reversal" problem in challenge Programming Competition
Progressive JPEG pictures and related
交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
Programming implementation of ROS learning 6 -service node
Solution to the problem of the 10th Programming Competition (synchronized competition) of Harbin University of technology "Colin Minglun Cup"
Newton iterative method (solving nonlinear equations)
Hi Fun Summer, play SQL planner with starrocks!