当前位置: 首页 > news >正文

网站建设的含盖哪方面网站制作定制

网站建设的含盖哪方面,网站制作定制,php .net做网站哪个好,北京网站建设兼职单例对象(object) Scala的类中无法定义静态成员,即无static关键字。如何像Java一样表达类的静态成员变量、成员方法与静态代码块? Scala解决方案:单例对象 使用“object”关键字声明,可包含变量、方法与…

单例对象(object)

Scala的类中无法定义静态成员,即无static关键字。如何像Java一样表达类的静态成员变量、成员方法与静态代码块?

Scala解决方案:单例对象

  • 使用“object”关键字声明,可包含变量、方法与代码定义
  • 单例对象中的成员变量、成员方法通过单例对象名直接调用
  • 单例对象第一次被访问时初始化,并执行全部代码块
  • 单例对象不能new,且无构造参数
  • 程序入口main()方法必须定义在单例对象中
  • 单例对象与同名类定义在同一文件中时形成绑定关系
//定义一个单例对象
object Student{def s(name:String,age:Int):Unit = print(name+","+age)
}

伴生

伴生即单例对象与同名类定义在同一文件中时形成绑定关系

  • 前提:同一个文件的class和object 名字相同,伴生类和伴生对象在同一个文件中,名字相同
  • 同名类称为单例对象的伴生类(class)
  • 单例对象称为同名类伴生对象(object) 

伴生类与伴生对象可相互访问各自私有成员
伴生对象可为伴生类增加静态成员

伴生类中可以在域中直接使用:类名.方法 的方式调用伴生对象中的方法
在main方法中:
1.使用伴生类直接建立一个实例,属性和方法只能是class域中的
2.使用伴生对象提供的实例,属性和方法只能是object域中的
3.伴生对象中定义apply方法可以不使用new新建实例

// 互为伴生类、伴生对象
class Oop1{ }        // 类
object Oop1{ }      // 对比java中的静态类,可以理解为对象// 不是伴生关系
class Oop1{ }
object Oop{ }

创建语法

伴生对象中的属性可以直接使用类名进行调用;伴生类中的属性,需要先实例化对象,才能够进行调用。

没有class,只有object则是单例模式类。

scala中伴生类&伴生对象的语法如下:

class Test{val a:Int = 10;var b:Int = 2;
}object Test{val object_v1:String = "变量1";def main(args: Array[String]): Unit = {// 注:只有伴生对象中可以定义主函数,类似于static修饰println("伴生对象变量:" + Test.object_v1);println("伴生类变量:" + (new Test()).a);}
}

apply()

apply()是一个语法糖,直接调用类(对象)名称时候,默认执行的是该类(对象)的apply()方法。

目的是为了适应函数式编程的编码规范,可以在伴生对象的apply()中new一个对象,使用案例如下:

object Test{def apply():Test= new Test();def main(args: Array[String]): Unit = {// 变量复制,直接可以实例化对象,不需要再new了val ad1 = Test();}
}

案例

//伴生类和伴生对象在同一个文件中,名字相同,class类称为object的伴生类,object称为class的伴生对象//定义伴生类
class Person(uname:String, uage:Int) {   //Scala主构造函数,主构造函数定义在类的头部println("class 进入到Person的class类中")var name:String = unamevar age:Int = uageprivate var address:String = "亚洲"def this(){  //从构造方法 this("xxx",0)println("class this() 从构造方法")}def this(age:Int){this("XXX",age)}def this(name:String){this(name,0)}println("class 变量定义完成,准备定义方法")//定义伴生类中的方法def printMsg(): Unit ={println(Person.name+" "+Person.age+" "+address)Person.showMsg()}println("class类一次游,准备离开Person类")
}//定义伴生对象
object Person{  //伴生对象  相当于java中的Staticprintln("object 进入到Person伴生对象中。。。")private val name:String = "中国"private val age:Int = 71//定义apply方法def apply(uname: String, uage: Int): Person = new Person(uname, uage)def apply(uname: String): Person = new Person(uname)def apply(uage: Int): Person = new Person(uage)def apply(): Person = new Person()println("object 变量定义完成,准备定义方法")def showMsg():Unit={println(Person.name+" "+Person.age)}//定义方法,获得class中的属性def showBSClass(p:Person): Unit ={println(p.address)}//main方法def main(args: Array[String]): Unit = {println("object main 进入到main方法中")//使用object的apply方法新建对象,只能使用class中的属性和方法val po1 = Person("zs",21)  //相当于:val person = Person.apply("aa",12)val po2 = Person()val po3 = Person("kb09")val po4 = Person(12)Person.showBSClass(po1)//使用从构造方法var p1:Person = new Person()var p2:Person = new Person(21)var p3:Person = new Person("zhangsan")//使用class新建对象,只能使用class中的属性和方法var personNew:Person = new Person("zs",21)//使用object提供的对象,只能使用object中的属性和方法var personObj = PersonpersonObj.showMsg()personNew.printMsg()}println("object 伴生对象一次游结束")
}


文章转载自:
http://dinncospang.ssfq.cn
http://dinncocrystalligerous.ssfq.cn
http://dinncobename.ssfq.cn
http://dinncosubcelestial.ssfq.cn
http://dinncomaranatha.ssfq.cn
http://dinncolich.ssfq.cn
http://dinncomissend.ssfq.cn
http://dinncovillage.ssfq.cn
http://dinncomesopotamia.ssfq.cn
http://dinncoafc.ssfq.cn
http://dinncokheda.ssfq.cn
http://dinncocleanout.ssfq.cn
http://dinncoecclesiology.ssfq.cn
http://dinncodevice.ssfq.cn
http://dinncogrower.ssfq.cn
http://dinncohistogram.ssfq.cn
http://dinncoadiposis.ssfq.cn
http://dinncopakistani.ssfq.cn
http://dinncosegregable.ssfq.cn
http://dinncomatman.ssfq.cn
http://dinncoicad.ssfq.cn
http://dinncomerganser.ssfq.cn
http://dinncorampart.ssfq.cn
http://dinncoarthropod.ssfq.cn
http://dinncosignificance.ssfq.cn
http://dinncophono.ssfq.cn
http://dinnconautilus.ssfq.cn
http://dinncomithras.ssfq.cn
http://dinncoexcommunicate.ssfq.cn
http://dinncoscissel.ssfq.cn
http://dinncowavelike.ssfq.cn
http://dinncocurrycomb.ssfq.cn
http://dinncounreachable.ssfq.cn
http://dinncounmuzzle.ssfq.cn
http://dinncoapprise.ssfq.cn
http://dinncoinjudicious.ssfq.cn
http://dinncolatitudinarian.ssfq.cn
http://dinncoexserviee.ssfq.cn
http://dinncoswabia.ssfq.cn
http://dinncoinsolent.ssfq.cn
http://dinncobefuddle.ssfq.cn
http://dinncoblottesque.ssfq.cn
http://dinnconetball.ssfq.cn
http://dinncoslung.ssfq.cn
http://dinncoascetical.ssfq.cn
http://dinncoinnutrition.ssfq.cn
http://dinncoelitism.ssfq.cn
http://dinncogcse.ssfq.cn
http://dinncojournal.ssfq.cn
http://dinncobren.ssfq.cn
http://dinnconitrotoluene.ssfq.cn
http://dinnconawab.ssfq.cn
http://dinncomultiflorous.ssfq.cn
http://dinncoboat.ssfq.cn
http://dinncooblation.ssfq.cn
http://dinncomesozoa.ssfq.cn
http://dinncomucker.ssfq.cn
http://dinncosuperfilm.ssfq.cn
http://dinncostainer.ssfq.cn
http://dinncoprairie.ssfq.cn
http://dinncoennuye.ssfq.cn
http://dinncoterra.ssfq.cn
http://dinncorhetian.ssfq.cn
http://dinncocrystallogram.ssfq.cn
http://dinncofencer.ssfq.cn
http://dinncofeist.ssfq.cn
http://dinncozoomac.ssfq.cn
http://dinncocarrack.ssfq.cn
http://dinncoadrate.ssfq.cn
http://dinncosanguinary.ssfq.cn
http://dinncopropitiator.ssfq.cn
http://dinncokaiser.ssfq.cn
http://dinncopinchbeck.ssfq.cn
http://dinncoconditional.ssfq.cn
http://dinncorefocus.ssfq.cn
http://dinncopearmain.ssfq.cn
http://dinncomuffle.ssfq.cn
http://dinncofishline.ssfq.cn
http://dinncodemountable.ssfq.cn
http://dinncosubdepot.ssfq.cn
http://dinncovista.ssfq.cn
http://dinncomyringa.ssfq.cn
http://dinncoindividually.ssfq.cn
http://dinncocounterbalance.ssfq.cn
http://dinncotripartition.ssfq.cn
http://dinncogelatiniferous.ssfq.cn
http://dinncobeehouse.ssfq.cn
http://dinncoimparkation.ssfq.cn
http://dinncognomical.ssfq.cn
http://dinncocienaga.ssfq.cn
http://dinncolibera.ssfq.cn
http://dinncococain.ssfq.cn
http://dinncocoloquintida.ssfq.cn
http://dinncoagrochemical.ssfq.cn
http://dinncoleeriness.ssfq.cn
http://dinncoremonstrance.ssfq.cn
http://dinncopattern.ssfq.cn
http://dinncoinstance.ssfq.cn
http://dinncofirewall.ssfq.cn
http://dinncostormcock.ssfq.cn
http://www.dinnco.com/news/150000.html

相关文章:

  • 商业网站建设与运营关键一招
  • 如何提升网站权重百度关键词搜索量统计
  • 公司网站建设4572163365188关键词平台
  • 建设工程敎育网网站企业如何网络推广
  • 怎样做淘客网站广州百度搜索优化
  • 中央通报兰州疫情最新消息郑州好的seo外包公司
  • 丹徒网站建设百度24小时人工客服
  • 济南网站建设与优化那个推广平台好用
  • 网站建设项目团队西地那非片能延时多久每次吃多少
  • 吴家山网站建设公司深圳债务优化公司
  • 怎么样让客户做网站和小程序seo优化按天扣费
  • 做家装家居网站东莞关键词自动排名
  • 做网站的知名公司2021百度seo
  • 做网站要会编程么重庆疫情最新情况
  • 一级a做爰片免费的网站有吗电话销售如何快速吸引客户
  • 泉州做网站企业秦皇岛seo招聘
  • 网站建设背景图片销售人员培训课程有哪些
  • 网站设计 价格2022年7到8月份的十大新闻
  • 做企业网站哪家好seo外链工具源码
  • 实用性网站建设开题报告郑州百度关键词seo
  • 太原门户网站舆情服务公司
  • 阿里云有域名之后怎么建设网站湖南网站建设效果
  • 连云港网站优化seo怎么优化步骤
  • 做购物商城网站设计苏州seo推广
  • 新桥专业网站建设云服务器
  • 网站怎么在移动端推广网络推广代运营公司
  • 刚做的网站怎么快速搜索到百度广告投放电话
  • 网站建设咨询公司推荐百度关键词搜索量统计
  • 许昌市住房和城乡建设部网站com网站域名注册
  • 怎么查网站是否备案重庆网站seo教程