[Scala基础]--Scala构建超过22个元素的class

本文介绍在Scala中如何创建具有超过22个元素的case class,通过实现Product特质来解决问题,并提供了一个具体的实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

1、在scala-2.10.x版本种,case class的元素超过22个以后即会编译报错

2、有些业务场景下,需要超过22个元素的值


二、如何解决

Scala提供了解决方案,即使用类实现Product特质


三、举例

package com.ngaa.scala

/**
  * @author Created by yangjf on 20180712.
  *         Update date:
  *         Time: 15:43
  *         Project: dev-spark01-examples
  *         Package: com.ngaa.scala
  *         Describe :   Scala构建超过22个元素的class
  *                      API参考:
  *                             https://www.scala-lang.org/api/current/scala/Product.html
  *                      学习参考:
  *                             https://www.artima.com/pins1ed/
  *
  */
object More22Elements {
  def main(args: Array[String]) {
    val student = new Student(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27)
    /* 获取其中一个元素 */
    println(student.productElement(1).toString)
    /* 是否包含元素:23 */
    println(student.canEqual(23))
    /* 遍历元素 */
    student.productIterator.foreach(println)
    /* toString方法 */
    println(student.productPrefix)
    println(student.equals(student))
  }
}

/** Student类中有大于22个元素的实现  */
class Student(one: Int, two: Int, three: Int, four: Int, five: Int
              , six: Int, seven: Int, eight: Int, nine: Int, ten: Int, eleven: Int, twelve: Int
              , thirteen: Int, fourteen: Int, fifteen: Int, sixteen: Int
              , seventeen: Int, eighteen: Int, nineteen: Int, twenty: Int, first: Int, second: Int
              ,third:Int, fourth: Int, fifth: Int, sixth: Int, seventh: Int) extends Product {
  val arrays = Array(one, two, three, four, five
    , six, seven, eight, nine, ten, eleven, twelve
    , thirteen, fourteen, fifteen, sixteen
    , seventeen, eighteen, nineteen, twenty, first, second, third
    , fourth, fifth, sixth, seventh)

  /**
    * 201871314:47:27
    * 自定义比较:是否包含这个元素
    *
    * @param that 输入的元素
    * @return 是否包含,true代表包含
    */
  override def canEqual(that: Any): Boolean = arrays.contains(that)

  /**
    * 201871314:26:29
    * 获取迭代器
    *
    * @return 迭代
    */
  override def productIterator: Iterator[Int] = new scala.collection.Iterator[Int] {
    private var c: Int = 0
    private val cMax = productArity

    def hasNext = c < cMax

    def next() = {
      val result = productElement(c)
      c += 1
      result
    }

  }

  /**
    * 201871314:26:46
    * 根据下标获取元素
    *
    * @param n 下标
    * @return 元素
    */
  override def productElement(n: Int): Int = arrays(n)

  /**
    * 201871314:27:10
    * 元素个数
    *
    * @return 元素个数
    */
  override def productArity: Int = arrays.length

  /**
    * 201871314:31:54
    * 类似toString方法
    *
    * @return 元素链接字符串
    */
  override def productPrefix = arrays.mkString("\t")
}

测试结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oo寻梦in记

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值