package cn.kgc
//伴生类
class Oop1 (name:String,age:Int){//scala默认的主构造函数
private var uname:String=name
private var uage :Int=age
private var uaddress:String=_
def this(){
this(" ",0)
println("无参")
}
def this(name:String){
this(name,0)
}
def this(age:Int){
this("",0)
}
def this(name:String,age:Int,address:String){
this(name,age)
this.uaddress=address
}
def showMsg():Unit={
println("进入到Oop1 class showMsg()方法中")
println(this.uname,this.age,this.uaddress)
Oop1.showObjectMsg
println(Oop1.like)
println("进入到Oop1 class showMsg()方法,最后一行")
}
}
//伴生对象
object Oop1{
private var like:String="篮球"
def sum(a:Int,b:Int):Int={
println(a,b)
a+b
}
def showObjectMsg()={
println("Object Oop1 showOjbectMsg")
}
//加上可以省去new对象
def apply(name: String, age: Int): Oop1 = new Oop1(name, age)
def apply(name: String): Oop1 = new Oop1(name)
def apply(age: Int): Oop1 = new Oop1(age)
def apply(): Oop1 = new Oop1()
def apply(name: String, age: Int, address: String): Oop1 = new Oop1(name, age, address)
def main(args:Array[String]):Unit={
println("main")
// val oop=new Oop1("chen",26)
val oop2 = new Oop1
// val oop3 = new Oop1("kb11")
// val oop4 = new Oop1(12)
// val oop5 = new Oop1("gree", 21, "安德门")
oop2.showMsg
val opp6=Oop1
val opp7=Oop1("zs")
println("main end")
// println(oop.uname)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)