类通过class关键字定义
类通过new关键字创建实例
类拥有成员变量和方法
类的成员默认为public,也支持private、protected
类中无法定义静态成员变量和方法
类无需明确定义构造方法,通过构造参数列表声明为类的一部分
class Student(name:String,age:Int) {//主构造方法
var stuname=name
var stuage=age
def fun(): Unit ={
println("学生姓名:"+name+"学生年龄:"+age)
}
}
辅助构造器
class Student{//主构造方法
var name:String=_
var age:Int=_
def this(name:String,age:Int)={
//辅助构造器必须从调用其他构造器开始
this()
this.name=name
this.age=age
}
def fun(): Unit ={
println("学生姓名:"+name+"学生年龄:"+age)
}
}
def move(dx: Int, dy: Int) ={
x = x + dx
y = y + dy
}
val zcy = new Student("zcy",15)
zcy.fun()
abstract class Shape {
def draw(): Unit ={
}
def write(): Unit ={
println("写代码")
}
}
class Circle extends Shape{
override def draw(): Unit = {
println("画一个圆")
}
}
object Shape{
def main(args: Array[String]): Unit = {
val circle = new Circle
circle.draw()
circle.write()
}
}
//Student.scala
//伴生类
class Student(n: String, a: Int) {
private var name = n //私有变量,伴生对象可以访问
private var age = a
}
//伴生对象
object Student {
//使用伴生对象的apply()方法省掉new关键字。Student.apply()等价于Student()
def apply(n: String, a: Int): Student = new Student(n, a)
def main(args: Array[String]): Unit = {
val stu=Student("Jason",9) //通过伴生对象的apply()方法创建实例
println(stu.name)
}
}
trait相当于java中的接口
package oop
trait SayHello{
def sayHello(name:String)
}
trait ListenMusic{
def playMusic(musicName:String): Unit ={
println(musicName+"正在播放!")
}
}
trait Fly{
def plySky(): Unit ={
println("起飞!!!!")
}
}
trait Sea{
def goSea(): Unit ={
println("下海")
}
}
class Person(var name:String)extends SayHello with ListenMusic{
//Person继承SayHello,ListenMusic类后可以使用SayHello,ListenMusic的方法
override def sayHello(name: String): Unit = {
print("hello"+name)
}
override def playMusic(musicName: String): Unit = {
println(musicName+"正在播放!")
}
}
//Superman 继承了 Person
class Superman extends Person("zcy") {
}
object Person {
def main(args: Array[String]): Unit = {
var person=new Person("zcy")
person.sayHello("zhangsan")
// print(person.name)
person.playMusic("最炫民族风")
//混入特质
//Superman 继承了 Person后可以使用with的方式使用Person的方法
var superman:Superman with ListenMusic with Fly with Sea
=new Superman with ListenMusic with Fly with Sea
superman.playMusic("RapGod")
superman.goSea()
superman.plySky()
superman.sayHello("sdfa")
}
}
package oop
//燃料,动力,方式
trait Type{
//能源/汽油/电
val energy:String
def power():String
def wheels:String={
"四个轮子"
}
}
trait Tpye1{
val brand="凌云两轮电动汽车"
def skill():String={
"自动平衡"
}
def skill2():String={
"新能源"
}
}
trait Type3{
def func1():String={
"上天"
}
def func2():String="下地"
}
abstract class Car {
val brand:String
def run():String
def didi():String={
"滴滴 汽车鸣笛..."
}
}
class Bmw extends Car with Type{
override val energy: String = "汽油"
override val brand: String = "奔驰"
override def power():String="八缸涡轮"
override def run():String={
"速度可以跑到400km/h"
}
class LinYun{
}
object Bnw{
def main(args: Array[String]): Unit = {
var bmw = new Bmw
bmw.power()
var ly:LinYun with Tpye1 with Type3=new LinYun with Tpye1 with Type3
ly.brand
ly.func1()
ly.func2()
ly.skill()
ly.skill2()
}
object ObjCovariantAndInversionDemo {
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 Covariant[+T]{
//协变
}
class Inversion[-T]{
//逆变
}
class Invarian[T]{
//不变
}
def main(args: Array[String]): Unit = {
var cat:Cat=new Tiger//父类类型引用子类对象
var catObj:Invarian[Tiger]=new Invarian[Tiger]
// var tigerObj:Invarian[Tiger]=catObj 不成立
//协变后还是遵循原来的父子关系
var catObj2:Covariant[Tiger]=new Covariant[Tiger]
var tigerObj:Covariant[Cat]=catObj2
//逆变后还是逆反原来的父子关系
var catObj3:Inversion[Cat]=new Inversion[Cat]
var tigerObj3:Inversion[Tiger]=catObj3
}
}