伴生类和伴生对象
public class MyStaticClass {
public static String name="大唐";
public static int age=289;
public MyStaticClass(){
}
public MyStaticClass(String name,int age){
this.name=name;
this.age=age;
}
public static void showMsg(){
System.out.println(name+"帝国共传承"+age+"年");
}
public static void main(String[] args) {
MyStaticClass.showMsg();
}
- 伴生类和伴生对象在同一个文件中,名字相同,class类成为object类的伴生类,object称为class类的伴生对象
- 伴生类可以直接访问伴生对象的私有属性和方法,伴生对象不能直接访问伴生类。
class Student(sname:String,sage:Int) {
private val name: String = "zhu"
val age: Int = 22
def this(){
this("***",0)
}
def this(sage:Int){
this("***",sage)
}
def this(sname:String){
this(sname,0)
}
def cClass(): Unit = {
println(sname + "年龄为:" + sage+",他的女朋友叫"+Student.oname+",年龄为:"+Student.oage)
}
}
object Student {
private val oname: String = "tang"
val oage: Int = 19
def oObject(): Unit = {
println(oname + "芳龄为:" + oage)
}
def visitBSLei(stu:Student):Unit={
stu.cClass()
println(stu.age)
println(stu.name)
}
visitBSLei(Student())
def apply(sname: String, sage: Int): Student = new Student(sname, sage)
def apply(sname: String): Student = new Student(sname)
def apply(sage: Int): Student = new Student( sage)
def apply(): Student = new Student()
def main(args: Array[String]): Unit = {
val banShengLei=new Student("lala",20)
val banShengLei2=new Student("lala",20)
val banShengDuiXiang=Student
val banShengDuiXiang2=Student
val stu1=new Student()
val stu2=new Student(18)
val stu3=new Student("haha")
val stu4=new Student("hah",18)
val stuApply1=Student("1",1)
val stuApply2=Student("2")
val stuApply3=Student(3)
val stuApply4=Student()
stuApply1.cClass()
stuApply2.cClass()
stuApply3.cClass()
stuApply4.cClass()
}
}
特质
*纵观全局,其实可以看出,Dog类是在创建类时with加上特质,然后调用时直接创建dog对象就欧克了
而Cat类创建类时只是简单的继承Animal,它需要在调用时创建类对象时with加上特质
简而言之,也就是一个是在原来的类加特质,一个是在调用时添加特质
显然第二种更加方便,而且不改动原代码就是我们程序员最大的贡献。
*/
trait Type4{
def fly:String={
"可以飞"
}
def eat:Unit={}
def sleep
}
trait Type5{
def jiao:Unit={
println("可以叫")
}
}
trait Type6{
def run:Unit={
println("可以跑")
}
}
abstract class Animal{
def haha:Unit
def yaya():Unit={
println("yaya")
}
}
class Cat extends Animal {
override def haha(): Unit = {
println("lala")
}
}
class Dog extends Animal with Type4 with Type5 with Type6 {
override def haha: Unit = {
println("狗在猖狂的大笑")
}
override def sleep: Unit = {
println("哈某睡觉去了")
}
}
object demo{
def main(args: Array[String]): Unit = {
val cat:Cat with Type4 with Type5 with Type6=new Cat with Type4 with Type5 with Type6 {
override def eat: Unit = {
println("吃")
}
override def sleep: Unit = {
println("睡觉了")
}
}
println(cat.fly)
cat.jiao
cat.run
cat.yaya
cat.haha
cat.eat
cat.sleep
val dog=new Dog
println(dog.fly)
dog.sleep
}
}
不变、协变、逆变
object covariantAndContravariant {
class Animal {
def eat(): Unit = {
println("动物要吃食物")
}
}
class Cat extends Animal{
override def eat():Unit=println("猫吃鱼")
}
class Tiger extends Cat{
override def eat(): Unit =println("老虎吃肉")
}
class Invariant[T]{
}
class Covariant[+T]{
}
class Inversion[-T]{
}
def main(args: Array[String]): Unit = {
val cat:Cat=new Cat
val tiger:Tiger=new Tiger
val cat2:Cat=new Tiger
val cat3:Cat=tiger
val bubian:Invariant[Cat]=new Invariant[Cat]
val bubianTiger:Invariant[Tiger]=new Invariant[Tiger]
val xiebianCat:Covariant[Cat]=new Covariant[Cat]
val xiebianTiger:Covariant[Tiger]=new Covariant[Tiger]
val xieCat2:Covariant[Cat]=xiebianTiger
val xieCat3:Covariant[Cat]=new Covariant[Tiger]
val nibianCat:Inversion[Cat]=new Inversion[Cat]
val nibianTiger:Inversion[Tiger]=new Inversion[Tiger]
val niTiger2:Inversion[Tiger]=nibianCat
val niTiger3:Inversion[Tiger]=new Inversion[Cat]
}
}