首页 > 其他分享 >每日总结2023-05-01

每日总结2023-05-01

时间:2023-05-01 22:23:52浏览次数:42  
标签:01 users Repository 05 INSTANCE User 2023 null name

今天继续学习了Android中的kotlin语言

初始Java语言

mport java.util.ArrayList;
import java.util.List;


public class Repository {

    private static final Repository INSTANCE = null;

    private List<User> users = null;

    public static Repository getInstance() {
        if (INSTANCE == null) {
            synchronized (Repository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Repository();
                }
            }
        }
        return INSTANCE;
    }

    // keeping the constructor private to enforce the usage of getInstance
    private Repository() {

        User user1 = new User("Jane", "");
        User user2 = new User("John", null);
        User user3 = new User("Anne", "Doe");

        users = new ArrayList();
        users.add(user1);
        users.add(user2);
        users.add(user3);
    }

    public List<User> getUsers() {
        return users;
    }

    public List<String> getFormattedUserNames() {
        List<String> userNames = new ArrayList<>(users.size());
        for (User user : users) {
            String name;

            if (user.getLastName() != null) {
                if (user.getFirstName() != null) {
                    name = user.getFirstName() + " " + user.getLastName();
                } else {
                    name = user.getLastName();
                }
            } else if (user.getFirstName() != null) {
                name = user.getFirstName();
            } else {
                name = "Unknown";
            }
            userNames.add(name);
        }
        return userNames;
    }
}

转化后:

class Repository
// keeping the constructor private to enforce the usage of getInstance
private constructor() {

    private val users: MutableList<User>? = null  // ?= 表示可空

    val formattedUserNames: List<String>
        get() {
            val userNames = ArrayList<String>(users!!.size)
            for ((firstName, lastName) in users) {
                val name: String?

                if (lastName != null) {
                    if (firstName != null) {
                        name = "$firstName $lastName"
                    } else {
                        name = lastName
                    }
                } else if (firstName != null) {
                    name = firstName
                } else {
                    name = "Unknown"
                }
                userNames.add(name)
            }
            return userNames
        }

    init {

        val user1 = User("Jane", "")
        val user2 = User("John", null)
        val user3 = User("Anne", "Doe")

        users = ArrayList()
        users!!.add(user1)
        users.add(user2)
        users.add(user3)
    }

    fun getUsers(): List<User>? {
        return users
    }

    companion object {

        private var INSTANCE: Repository? = null

        val instance: Repository
            get() {
                if (INSTANCE == null) {
                    synchronized(Repository::class.java) {
                        if (INSTANCE == null) {
                            INSTANCE = Repository()
                        }
                    }
                }
                return INSTANCE
            }
    }
}

执行了:

  • 添加了 init 代码块 (Repository.kt#L33)
  • static 字段现已加入 companion object 代码块中 (Repository.kt#L48)
  • users 列表可为 null,因为该对象在声明时并未实例化 (Repository.kt#L9)
  • getFormattedUserNames() 方法现已成为一个属性 (Repository.kt#L11)
  • 在对用户列表执行循环时,其语法与 Java 不同 (Repository.kt#L14)

 

标签:01,users,Repository,05,INSTANCE,User,2023,null,name
From: https://www.cnblogs.com/JJTyyds/p/17367110.html

相关文章

  • 05 Real-Time Physically-Based Materials
    1.MicrofacetBRDF1.1菲涅尔项菲涅尔项:反射光线强度与入射角的关系。对于绝缘体而言,观察方向越是平行于平面,反射越强,图像越清晰;因为镜面反射可逆,所以反之亦然。对于金属而言,规律相似,但是变化没有绝缘体那么明显。菲涅尔项的值与角度以及两个介质的折射率有关,可以采用简......
  • CSP2023-03
    第一题 直接满分了:#include<iostream>usingnamespacestd;constintN=1e6;intn,a,b;intpanduan(intx1,inty1,intx2,inty2,inta,intb){intc,k;if(x2<0||y2<0||x1>a||y1>b)return0;//在外侧else{......
  • 蛮力法解01背包问题
    #include<iostream>usingnamespacestd;structthing{intweight;//物品重量intvalue;//物品价值intnumber;//物品序号};thingthings[10];//假设最多有10个物品intthingsCount;//物品数量intbagSize;//背包容量intmaxTotalValue;//最大总重量......
  • 回溯法解决01背包问题
    #include<iostream>usingnamespacestd;structthing{intweight;//物品重量intvalue;//物品价值intnumber;//物品数量};thingthings[10];//假设最多有10个物品intthingsCount;//物品数量intbagSize;//背包容量intmaxTotalValue;//最大总重量......
  • 摄影-230501
    ......
  • 2023-05-01:给你一个整数 n , 请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1
    2023-05-01:给你一个整数n,请你在无限的整数序列[1,2,3,4,5,6,7,8,9,10,11,...]中找出并返回第n位上的数字。1<=n<=2^31-1。输入:n=11输出:0解释:第11位数字在序列1,2,3,4,5,6,7,8,9,10,11,...里是0,它是10的一部分。答案2023-05-01:该......
  • 分支限界法解01背包问题
    #include<iostream>usingnamespacestd;#defineMAX100structNode{intisVisit;//记录节点是否被扩展doublew;doublev;intlevel;//记录节点所在的层次doubleub;//上界Node*parent;//父节点};doublemaxValue=0;Node*PT[MAX......
  • P5336 [THUSC2016]成绩单
    题意:期末考试结束了,班主任L老师要将成绩单分发到每位同学手中。L老师共有\(n\)份成绩单,按照编号从\(1\)到\(n\)的顺序叠放在桌子上,其中编号为\(i\)的的成绩单分数为\(W_i\)。成绩单是按照批次发放的。发放成绩单时,L老师会从当前的一叠成绩单中抽取连续的一段,让这......
  • 2023-05-01:给你一个整数 n , 请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1
    2023-05-01:给你一个整数n,请你在无限的整数序列[1,2,3,4,5,6,7,8,9,10,11,...]中找出并返回第n位上的数字。1<=n<=2^31-1。输入:n=11输出:0解释:第11位数字在序列1,2,3,4,5,6,7,8,9,10,11,...里是0,它是10的一部分。答案2023-05-01:......
  • 2017年计算机应用专业学术硕士毕业时的部分简历——“胡编内容版本”
      项目经验:硕士期间曾参与教育部人文社会科学研究青年基金,“轻量级数据集成环境下基于语义元数据的商务智能实现技术研究“。在该项目使用100台浪潮英信服务器对运行环境进行假设,采用Hadoop大数据处理框架对1TB的语言文本资料进行自然语言分析和处理,并采用yarn架构对资源......