当前位置:网站首页>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
边栏推荐
- 迁移学习和域自适应
- Introduction Guide to stereo vision (1): coordinate system and camera parameters
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- fs. Path module
- Codeforces Round #648 (Div. 2) D. Solve The Maze
- Applet customization component
- c语言指针深入理解
- C # compare the differences between the two images
- Meta tag details
- Golang foundation - the time data inserted by golang into MySQL is inconsistent with the local time
猜你喜欢

信息與熵,你想知道的都在這裏了

【ManageEngine】如何利用好OpManager的报表功能

Applet data attribute method

Programming implementation of ROS learning 2 publisher node

Nodemon installation and use

Editor use of VI and VIM
![[code practice] [stereo matching series] Classic ad census: (5) scan line optimization](/img/54/cb1373fbe7b21c5383580e8b638a2c.jpg)
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization

Composition of applet code

RT thread kernel quick start, kernel implementation and application development learning with notes

牛顿迭代法(解非线性方程)
随机推荐
AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details
notepad++
深入浅出PyTorch中的nn.CrossEntropyLoss
Alibaba cloud sends SMS verification code
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
混淆矩阵(Confusion Matrix)
Use arm neon operation to improve memory copy speed
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
Wxml template syntax
Programming implementation of subscriber node of ROS learning 3 subscriber
Jenkins Pipeline 方法(函数)定义及调用
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Newton iterative method (solving nonlinear equations)
Talking about label smoothing technology
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
Jenkins pipeline method (function) definition and call
2309. The best English letters with both upper and lower case
生成对抗网络
编辑器-vi、vim的使用
Ros-11 common visualization tools