scala基础题--面向对象2

练习2:根据下图实现类。在TestCylinder类中创建Cylinder类的对象,设置圆柱的底面半径和高,并输出圆柱的体积

import scala.beans.BeanProperty

object work04 {
  def main(args: Array[String]): Unit = {
    val area1=new TestCylinder
    var area2=new area1.Cylinder

     var areanum:Double=area2.findArea(5.0)
    println(areanum)
    var volumenum:Double=area2.findVolume(2)
    println(volumenum)
  }
}
class TestCylinder{


  class Cylinder{
    @BeanProperty var radius:Double=1
    def findArea(radius:Double):Double={

      var area:Double=0
      area=radius*radius*3.14
      area

    }
    @BeanProperty var length:Double=1
    def findVolume(length:Double):Double={
      var volume:Double=0
      volume=findArea(radius)*length
      volume
    }

  }
}

  

 

 

posted @ 2019-12-07 00:24  梁衍  阅读(489)  评论(0编辑  收藏  举报