什么叫Kotlin元组(Pair & Triple)

在之前的学习中通常我们对变量赋值时,只带一个值,比如:

  val name = "jack"

比如我们有时候处理一些复杂情况的话,就只能进行分割处理,比如表述学生的 姓名、性别、年龄,就需要用到对象了。

此处我们以Java为例,有的场合我们需要使用学生的(姓名 性别 年龄),有的时候只需要使用(姓名 性别)再或者只使用(姓名 年龄)。可以使用如下处理方案(使用了部分面向对象的知识,不懂也没关系,后续课程里也会介绍)

public class Student {
    private String name;
    private String sex;
    private int age;
 
    //构造方法,包含姓名、性别、年龄
    public Student(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
 
    //构造方法,包含姓名、年龄
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
 
    //构造方法,包含姓名、性别
    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }
 
    public String getName() {
        return name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public int getAge() {
        return age;
    }
}
 
public class Person {
    public static void main(String[] args) {
        Student student = new Student("Alfred","男",29);
        System.out.println(student.getName());
        System.out.println(student.getSex());
        System.out.println(student.getAge());
 
        Student student1 = new Student("Thomas",21);
        System.out.println(student1.getName());
        System.out.println(student1.getAge());
 
        Student student2 = new Student("Jack","男");
        System.out.println(student2.getName());
        System.out.println(student2.getSex());
    }
}

结果:

Alfred
男
29
Thomas
21
Jack
男

这个就是常规解决方案,但是在简单的使用场合有必要这么复杂吗?下面就来看看元组

元组
可以把多个值同时赋给一个变量,或者同时给多个变量赋值。kotlin中元组分为二元元组(Pair)和三元元组(Triple),在新版Kotlin中已经删除了多元元组。也就是只有Pair和Triple。

先来看看API。

/**
 * Represents a triad of values
 *
 * There is no meaning attached to values in this class, it can be used for any purpose.
 * Triple exhibits value semantics, i.e. two triples are equal if all three components are equal.
 * An example of decomposing it into values:
 * @sample samples.misc.Tuples.tripleDestructuring
 *
 * @param A type of the first value.
 * @param B type of the second value.
 * @param C type of the third value.
 * @property first First value.
 * @property second Second value.
 * @property third Third value.
 */
public data class Triple<out A, out B, out C>(
    public val first: A,
    public val second: B,
    public val third: C
) : Serializable {
 
    /**
     * Returns string representation of the [Triple] including its [first], [second] and [third] values.
     */
    public override fun toString(): String = "($first, $second, $third)"
}
 
/**
 * Represents a generic pair of two values.
 *
 * There is no meaning attached to values in this class, it can be used for any purpose.
 * Pair exhibits value semantics, i.e. two pairs are equal if both components are equal.
 *
 * An example of decomposing it into values:
 * @sample samples.misc.Tuples.pairDestructuring
 *
 * @param A type of the first value.
 * @param B type of the second value.
 * @property first First value.
 * @property second Second value.
 * @constructor Creates a new instance of Pair.
 */
public data class Pair<out A, out B>(
    public val first: A,
    public val second: B
) : Serializable {
 
    /**
     * Returns string representation of the [Pair] including its [first] and [second] values.
     */
    public override fun toString(): String = "($first, $second)"
}

很简单不做过多的解释,直接看例子就明白了。

fun main() {
    val student1 = Triple<String, String, Int>("Alfred", "男", 29)
    val student2 = Pair<String,Int>("Thomas",21)
    val student3 = Pair<String,String>("jack","男")
    println(student1.first)
    println(student1.second)
    println(student1.third)
    println(student2.first)
    println(student2.second)
    println(student3.first)
    println(student3.second)
}

}
结果:

Alfred
男
29
Thomas
21
jack
男

看看激动不,实现结果跟Java面向对象实现结果一致。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值