object Scala {

  def main( args : Array[ String ] ) : Unit =
{
val p = new Point( , );
println( p );
p.move( , );
println( p ); val p2 = new Point
println( p2 ) val p3 = new Point( y = )
println( p3 ) val point4 = new Point2
point4.x =
point4.x_=( );
point4.y = // prints the warning
println( point4 ) /**
*
* <pre>
* Primary constructor parameters with val and var are public.
* However, because vals are immutable,
* you can’t write the following.
* class Point(val x: Int, val y: Int)
* val point = new Point(1, 2)
* point.x = 3 // <-- does not compile
* </pre>
* <pre>
* 首要构造器上参数都是public.如果没有明确写出是var x:Int,默认是val
* val是不可变的,所以如上代码不能通过编译
* </pre>
*/
}
} class Point( var y : Int = , var x : Int = ) {
def move( dy : Int, dx : Int ) : Unit =
{
this.x = x;
this.y = y;
} override def toString() : String =
s"($x,$y)"
} class Point2 {
private var _x = ;
private var _y = ;
private var bound = ; //define method x for access _x
def x = _x; /**
* <pre>
* def x_= and def y_= are for validating and setting
* the value of _x and _y.
* Notice the special syntax for the setters:
* the method has _= appended to the identifier of the
* getter and the parameters come after
* </pre>
* <pre>
* def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法.
* </pre>
*/
def x_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
//define method y for access _y
def y = _y;
def y_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String =
s"($x,$y)"
}

下面这份代码更能说明class的setter和getter

object Scala {

  def main( args : Array[ String ] ) : Unit =
{ val point4 = new Point
point4.xx =
point4.y = // prints the warning
println( point4 )
}
} class Point {
private var _x = ;
private var _y = ;
private var bound = ; //define method x for access _x
def xx = _x; /**
* <pre>
* def x_= and def y_= are for validating and setting
* the value of _x and _y.
* Notice the special syntax for the setters:
* the method has _= appended to the identifier of the
* getter and the parameters come after
* </pre>
* <pre>
* def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法.
* </pre>
*/
def xx_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
//define method y for access _y
def y = _y;
def y_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String =
s"($xx,$y)"
}

多个构造函数

object Scala {

  def main( args : Array[ String ] ) : Unit =
{ val point4 = new Point
point4.xx =
point4.y = // prints the warning
println( point4 ) val point5 = new Point( )
point5.y = // prints the warning
println( point5 ) }
} class Point {
private var _x = ;
private var ny = ;
private var bound = ; def this( xxx : Int ) {
this()
xx = xxx;
println("here")
} //define method x for access _x
def xx = _x; /**
* <pre>
* def x_= and def y_= are for validating and setting
* the value of _x and _y.
* Notice the special syntax for the setters:
* the method has _= appended to the identifier of the
* getter and the parameters come after
* </pre>
* <pre>
* def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法.
* </pre>
*/
def xx_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
//define method y for access _y
def y = ny;
def y_=( newValue : Int ) : Unit =
{
if ( newValue < bound )
_x = newValue
else
printWarning
}
private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String =
s"($xx,$y)"
}

scala-class的更多相关文章

  1. jdb调试scala代码的简单介绍

    在linux调试C/C++的代码需要通过gdb,调试java代码呢?那就需要用到jdb工具了.关于jdb的用法在网上大家都可以找到相应的文章,但是对scala进行调试的就比较少了.其实调试的大致流程都 ...

  2. scala练习题1 基础知识

    1, 在scala REPL中输入3. 然后按下tab键,有哪些方法可以被调用? 24个方法可以被调用, 8个基本类型: 基本的操作符, 等:     2,在scala REPL中,计算3的平方根,然 ...

  3. 牛顿法求平方根 scala

    你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...

  4. Scala集合和Java集合对应转换关系

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 用Scala编码的时候,经常会遇到scala集合和Java集合互相转换的case,特意mark一 ...

  5. Scala化规则引擎

    1. 引言 什么是规则引擎 一个业务规则包含一组条件和在此条件下执行的操作,它们表示业务规则应用程序的一段业务逻辑.业务规则通常应该由业务分析人员和策略管理者开发和修改,但有些复杂的业务规则也可以由技 ...

  6. Scala快速概览

    IDEA工具安装及scala基本操作 目录 一. 1. 2. 3. 4. 二. 1. 2. 3. 三. 1. 2. 3. 4. 5. 6. 7. 四. 1. (1) (2) (3) (4) (5) ( ...

  7. Scala Macros - scalamela 1.x,inline-meta annotations

    在上期讨论中我们介绍了Scala Macros,它可以说是工具库编程人员不可或缺的编程手段,可以实现编译器在编译源代码时对源代码进行的修改.扩展和替换,如此可以对用户屏蔽工具库复杂的内部细节,使他们可 ...

  8. Scala Macros - 元编程 Metaprogramming with Def Macros

    Scala Macros对scala函数库编程人员来说是一项不可或缺的编程工具,可以通过它来解决一些用普通编程或者类层次编程(type level programming)都无法解决的问题,这是因为S ...

  9. Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag

    反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...

  10. Scala For Java的一些参考

          变量 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java ...

随机推荐

  1. FastReport 保存为文件

    public void SaveToPDF<TModel>(List<TModel> model, string content, string saveFilePath) { ...

  2. 黄聪:移动应用抓包调试利器Charles

    一.Charles是什么?   Charles是在 Mac或Windows下常用的http协议网络包截取工具,是一款屌的不行的抓包工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了 ...

  3. ubuntu14 16使用libusb过程中遇到的问题及解决方法

    从ubuntu16换到ubuntu14后安装libusb运行一直在libusb_bulk_transfer语句出现运行出现段错误,分别换了libusb1.0.0,1.0.9及1.0.21. 通过查阅链 ...

  4. 在IDEA中将SpringBoot项目打包成jar包的方法

    SpringBoot项目无需依赖tomcat容器(内含)就可以发布,现在将打包步骤记录一下: 1. 打包前确认项目可以正常运行,打开Project Structure 快捷键 Ctrl+Shift+A ...

  5. ALGO-139_蓝桥杯_算法训练_s01串(递归)

    问题描述 s01串初始为" 按以下方式变换 0变1,1变01 输入格式 1个整数(~) 输出格式 n次变换后s01串 样例输入 样例输出 数据规模和约定 ~ 记: 题目给出的信息带有误导性, ...

  6. MDIEMDIE双心封装版0.3.0.0RC6V2

    MDIEMDIE双心封装版0.3.0.0 RC6V2官方主页:http://cres.s28.xrea.com/MDIE(多文档界面资源管理器)是一个MDI类型的文件管理软件,是代替资源管理器的一个无 ...

  7. pyqt5.8.2没有qt Designer和assistant exe

    使用python3.6 pyqt5.8 eric6 创建完新的窗体后,弹出如下的错误: 解决方法: 1.安装pyqt5-tools 下载地址: https://pypi.python.org/pypi ...

  8. 【C#】C#操作Excel文件(转)

    实现C#与Excel文件的交互操作,实现以下功能: 1.DataTable 导出到 Excel文件 2.Model数据实体导出到 Excel文件[List<Model>] 3.导出数据到模 ...

  9. spring AOP 之一:spring AOP功能介绍

    一.AOP简介 AOP:是一种面向切面的编程范式,是一种编程思想,旨在通过分离横切关注点,提高模块化,可以跨越对象关注点.Aop的典型应用即spring的事务机制,日志记录.利用AOP可以对业务逻辑的 ...

  10. TextView 多文字字体颜色及多事件监听

    像微信朋友圈点赞功能如:张三.李四.王五.这种格式 最早做法是在layout中创建一个父类容器如linearlayout然后在创建一个子layout,在代码中 通过for循环addView添加到父类容 ...