scala-class
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的更多相关文章
- jdb调试scala代码的简单介绍
在linux调试C/C++的代码需要通过gdb,调试java代码呢?那就需要用到jdb工具了.关于jdb的用法在网上大家都可以找到相应的文章,但是对scala进行调试的就比较少了.其实调试的大致流程都 ...
- scala练习题1 基础知识
1, 在scala REPL中输入3. 然后按下tab键,有哪些方法可以被调用? 24个方法可以被调用, 8个基本类型: 基本的操作符, 等: 2,在scala REPL中,计算3的平方根,然 ...
- 牛顿法求平方根 scala
你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...
- Scala集合和Java集合对应转换关系
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 用Scala编码的时候,经常会遇到scala集合和Java集合互相转换的case,特意mark一 ...
- Scala化规则引擎
1. 引言 什么是规则引擎 一个业务规则包含一组条件和在此条件下执行的操作,它们表示业务规则应用程序的一段业务逻辑.业务规则通常应该由业务分析人员和策略管理者开发和修改,但有些复杂的业务规则也可以由技 ...
- Scala快速概览
IDEA工具安装及scala基本操作 目录 一. 1. 2. 3. 4. 二. 1. 2. 3. 三. 1. 2. 3. 4. 5. 6. 7. 四. 1. (1) (2) (3) (4) (5) ( ...
- Scala Macros - scalamela 1.x,inline-meta annotations
在上期讨论中我们介绍了Scala Macros,它可以说是工具库编程人员不可或缺的编程手段,可以实现编译器在编译源代码时对源代码进行的修改.扩展和替换,如此可以对用户屏蔽工具库复杂的内部细节,使他们可 ...
- Scala Macros - 元编程 Metaprogramming with Def Macros
Scala Macros对scala函数库编程人员来说是一项不可或缺的编程工具,可以通过它来解决一些用普通编程或者类层次编程(type level programming)都无法解决的问题,这是因为S ...
- Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...
- Scala For Java的一些参考
变量 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java ...
随机推荐
- openwrt挂载摄像头及视频保存
一.编译选项的选择: -> Utilities ->usbutils (这个里面包含lsusb的命令,是查看你的摄像头型号的) -> Kernel modules -> I2C ...
- zabbix 3.4监控mysql,提示mysql: [Warning] Using a password on the command line interface can be insecure.
mysql从5.6版本开始,如果是命令行直接出现了数据库连接密码就会有以下警告: mysql: [Warning] Using a password on the command line inter ...
- delphi中Time消息的使用方法
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Hiero_FnNukeShotExporter的解析与修改
研究对象:Hiero中的FnNukeShotExporter脚本 研究目的:修改FnNukeShotExporter使得可以将多个TrackItem导入到一个.nk中 FnNukeShotExpo ...
- linux上mongodb的安装与卸载
安装 1.下载安装包 wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-1.8.2.tgz 下载完成后解压缩压缩包 tar zxf mon ...
- react的echarts BizCharts
react BizCharts react的饼图,折线图 点击进入官网 -> https://bizcharts.net/products/bizCharts/demo 如果你在这遇到了问题,欢 ...
- 固态硬盘使用简要手册——windows平台
第一步,请安装它. 第二步,请关闭磁盘整理功能,如图 详细:控制面板--管理工具--任务计划程序 打开界面,设置如下 图1 第三步:预读(Superfetch)和快速搜索(Windows Search ...
- Python twilio发短信实践
twilio注册地址 注册的时候可能会报错 最好是*** -->注册-->注册完毕后代码运行是不需要***的 https://www.twilio.com/console 需要pi ...
- Access、SQLServer、Oracle常见SQL语句应用区别
Access.SQLServer.Oracle常见SQL语句应用区别 关劲松 PMP 如果要兼容Access.SQL Server.Oracle三个数据库版本:我们在编写SQL语句的过程中,尽量使用一 ...
- Calling async method synchronously
https://stackoverflow.com/questions/22628087/calling-async-method-synchronously/22629216#22629216 ht ...