【Kotlin】data数据类与复数

data class Complex(val re: Double=0.0, val im: Double=0.0) {  //主构造器初始化
   constructor( re:Int, im:Int):this(re.toDouble(),im.toDouble())
//传入其他类型的参数     通过this转换后委托主构造器 
   constructor( re:Double, im:Int):this(re,im.toDouble())
   constructor( re:Int, im:Double):this(re.toDouble(),im)
   
  //加减乘除方法简化   operator操作符的重载
   operator fun plus(other:Complex) = Complex(re+other.re,im+other.im)
   operator fun minus(other:Complex) = Complex(re-other.re,im-other.im)
   operator fun times(other:Complex) = Complex(re*other.re - im*other.im,re*other.im + other.re*im)
   operator fun div(other:Complex) = with( other.re*other.re + other.im*other.im ) {
                                                Complex( (re*other.re + im*other.im)/this,(other.re*im - re*other.im)/this)
                                           }

   fun modulus() = Math.sqrt(re*re + im*im)


   
   override fun toString():String {
       if ( re == 0.0 ) return if ( im == 0.0 ) "0" else "${im}i"
       return if ( im == 0.0 ) "$re" else
                   if ( im > 0.0 ) "${re} + ${im}i" else "${re} - ${-im}i"
   }

   //重写equals方法 因为浮点有-0 +0的区别 所以用equals
   override fun equals(other: Any?) = when (other) {
                                           !is Complex -> false
                                           else -> this === other ||
                                                    re == other.re && im == other.im
                                       }
   //因为Double有-0.0 < +0.0 问题,按照一般原则,两个对象相等, 它们的hashcode也要相等                                
   override fun hashCode()= 31*( if (re.equals(-0.0)) 0.0 else re ).hashCode() + ( if (im.equals(-0.0)) 0.0 else im ).hashCode()
}
   

fun main() {
   val d1=-0.0
   val d2=0.0
   println("d1=d2:${d1==d2}")
   println("d2==-0.0:${d2.equals(-0.0)}")
   println("d1==-0.0:${d1.equals(-0.0)}")
     println("d1=d2:${d1.compareTo(d2)}")
   println(d1.hashCode())
   println(d2.hashCode())
   val c1 = Complex(1.0, 1.0)
   val c2 = Complex(1.0, 2.0)
   val c3=Complex(3,-2)
   val c4=Complex(0.0,0.0)
   val c5=Complex(0,2.0)
   val c6=Complex(3.0,0)
   println(c1)
   println(c2)
   println(c3)
   println(c4)
   println(c5)
   println(c6)
   println("-----------")
   val c7=Complex(+0.0,+0.0)
   val c8=Complex(-0.0,-0.0)
   println(c7)
   println(c8)
   val c9=Complex(3,0)
   val c10=Complex(3.0,-0.0)
   val c11=Complex(3.0,+0.0)
   
   println("c1==c2: ${c1 == c2}")
   println("c7==c8: ${c7 == c8}")
   println("c9==c10: ${c9 == c10}")
   println("c11==c10: ${c11 == c10}")
   println("c1.hashcode=${c1.hashCode()}")
   println("c7.hashcode=${c7.hashCode()}")
   println("c8.hashcode=${c8.hashCode()}")
   println("c9.hashcode=${c9.hashCode()}")
   println("c10.hashcode=${c10.hashCode()}")
   println("c11.hashcode=${c11.hashCode()}")
   
   println("c2+c1=${c2+c1}")
   println("c1-c2=${c1-c2}")
   println("c1*c2=${c1*c2}")
   println("c2/c1=${c2/c1}")
   val c12=Complex()
   println("c1/c12=${c1/c12}")
   println(c1.modulus())


   // copy() function
   println(c1.copy())                      
   println(c1.copy(re=0.0))      
   println(c1.copy(im = 2.1))              
   
   println("re = ${c1.component1()}")                
   println("im = ${c1.component2()}")
}

运行结果:
d1=d2:true
d2==-0.0:false
d1==-0.0:true
d1=d2:-1
-2147483648
0
1.0 + 1.0i
1.0 + 2.0i
3.0 - 2.0i
0
2.0i
3.0
-----------
0
0
c1==c2: false
c7==c8: true
c9==c10: true
c11==c10: true
c1.hashcode=-33554432
c7.hashcode=0
c8.hashcode=0
c9.hashcode=-1057488896
c10.hashcode=-1057488896
c11.hashcode=-1057488896
c2+c1=2.0 + 3.0i
c1-c2=-1.0i
c1*c2=-1.0 + 3.0i
c2/c1=1.5 + 0.5i
c1/c12=NaN - NaNi
1.4142135623730951
1.0 + 1.0i
1.0i
1.0 + 2.1i
re = 1.0
im = 1.0

自己写的偏java风格一些==

data  class  datacomplex(val real: Double,val image: Double) { //数据型data 复数类

    //fun copy(real:Double = this.real, image:Double = image) = datacomplex(real, image)


    internal fun add(a: datacomplex):datacomplex { // 1复数相加
        val real2 = a.real
        val image2 = a.image
        val newReal = real + real2
        val newImage = image + image2

        var result:datacomplex=datacomplex(newReal,newImage)
        return result
    }

    internal fun sub(a: datacomplex): datacomplex { //2 复数相减
        val real2 = a.real
        val image2 = a.image
        val newReal = real - real2
        val newImage = image - image2

        var result:datacomplex=datacomplex(newReal,newImage)
        return result
    }

    internal fun mul(a: datacomplex): datacomplex { //3 复数相乘
        val real2 = a.real
        val image2 = a.image
        val newReal = real * real2 - image * image2
        val newImage = image * real2 + real * image2

        var result:datacomplex=datacomplex(newReal,newImage)
        return result
    }

    internal operator fun div(a: datacomplex):datacomplex { //4 复数相除
        val real2 = a.real
        val image2 = a.image
        val newReal = (real * real2 + image * image2) / (real2 * real2 + image2 * image2)
        val newImage = (image * real2 - real * image2) / (real2 * real2 + image2 * image2)

        var result:datacomplex=datacomplex(newReal,newImage)
        return result
    }

    internal fun mod(a: datacomplex):datacomplex { //5 复数模运算
        val real2 = a.real
        val image2 = a.image
        val newReal = (real * real2 + image * image2) +(real2 * real2 + image2 * image2)
        val newImage = (image * real2 - real * image2) + (real2 * real2 + image2 * image2)
        var result:datacomplex=datacomplex(newReal,newImage)
        return result
    }

 


//6toString打印方法

    override fun toString(): String {
        return "当前运算(实部=$real, 虚部=$image)"
    }


}

//主函数入口

fun main(args: Array<String>){
    println("请用户输入第一个复数的实部和虚部:2.0  4.0")
    val data1 = datacomplex(2.0,4.0)
    println("请用户输入第二个复数的实部和虚部:3.0  5.0")
    val data2 = datacomplex(3.0,5.0)

    // 以下分别为加减乘除
    val result_add: datacomplex = data1.add(data2)
    val result_sub = data1.sub(data2)
    val result_mul = data1.mul(data2)
    val result_div = data1.div(data2)
    val result_mod = data1.mod(data2)

 

    println("data类复数相加"+result_add.toString())
    println("data类复数相减"+result_sub.toString())
    println("data类复数相乘"+result_mul.toString())
    println("data类复数相除"+result_div.toString())
    println("data类复数相除"+result_mod.toString())


    val resultcopy: datacomplex = data1.copy(real = 2.0)//data类copy函数调用
    println("copy函数测试"+resultcopy.toString())
    if (resultcopy.equals(data1))
        println("两个对象相等,copy成功" )


    println("解析函数componentN()用于数据类解析声明")//Component函数在解构声明中使用
    val testcomponentN = datacomplex(real=3.5, image=2.4)
    val (realtc, imagetc) = testcomponentN///编译器处理:val name=jane.Component1()  val age=jane.Component2()
    println("realtc = $realtc,imagetc=$imagetc")//real=3.5, image=2.4

}

 

运行结果

请用户输入第一个复数的实部和虚部:2.0  4.0
请用户输入第二个复数的实部和虚部:3.0  5.0
data类复数相加当前运算(实部=5.0, 虚部=9.0)
data类复数相减当前运算(实部=-1.0, 虚部=-1.0)
data类复数相乘当前运算(实部=-14.0, 虚部=22.0)
data类复数相除当前运算(实部=0.7647058823529411, 虚部=0.058823529411764705)
data类复数相除当前运算(实部=60.0, 虚部=36.0)
copy函数测试当前运算(实部=2.0, 虚部=4.0)
两个对象相等,copy成功
解析函数componentN()用于数据类解析声明
realtc = 3.5,imagetc=2.4

859个字
发布了27 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_38304672/article/details/104939010