04.scala对象
面向对象的概述
封装:属性、方法封装到类中
User : int id,String name,Date birthday …
继承:
父类和子类之间的关系
User extends Person
多态(☆)
父类引用指向子类对象 精髓所在 开发框架的基石
Person person = new Person()
User user = new User()
Person person = new User()
类的定义与使用
object SimpleObjectApp {
def main(args: Array[String]): Unit = {
val people = new People();
people.name = "Messi" //people.age = 21 println(people.name+"...."+people.age)
println(people.eat())
people.watchFootball("china")
}
class People{
var name: String =""; //get /set都有
val age: Int =10; //只有get方法没有set方法
def eat(): Unit = { println(name + " eating...") }
def watchFootball(teamName: String)={
println(name + " is watching match of "+teamName)
}
}
}
11.png
在对象中用private [this] 修饰的变量或者函数只能在class类中使用
image.png
只能在当前类里使用,所以printInfo方法能调用
而people.gender无效
spark的源码用到很多
image.png
构造器
object ConstructorApp {
def main(args: Array[String]): Unit = {
val person = new Person("zhangsan",30) println(person.name+ ":"+ person.age+ " : "+ person.school)
val person2 = new Person("pk",18,"Z") println(person2.name+ ":"+ person2.age+ " : "+ person2.school+" : "+ person2.gender)
}
}
class Person(val name: String,val age: Int){
//主构造器
println("Person Constructor enter....")
val school = "ustc"
var gender: String = _ //附属构造器
def this(name: String,age: Int,gender: String){
//附属构造器 this(name,age)//附属构造器的第一行必须要调用主构造器或者其他附属构造器
this.gender = gender
}
println("Person Constructor leave....")
}
继承与重写
object ConstructorApp {
def main(args: Array[String]): Unit = {
// val person = new Person("zhangsan",30)
// println(person.name+ ":"+// person.age+ " : "+
// person.school)// val person2 = new Person("pk",18,"Z")
// println(person2.name+ ":"+
// person2.age+ " : "+
// person2.school+" : "+
// person2.gender)
val student = new Student("pk",19,"Math")
println(student.name+ " " +student.age+ " " +student. major)
}
}
class Person(val name: String,val age: Int){//主构造器
println("Person Constructor enter....")
val school = "ustc"
var gender: String = _ //附属构造器
def this(name: String,age: Int,gender: String){
//附属构造器
this(name,age)
//附属构造器的第一行必须要调用主构造器或者其他附属构造器
this.gender = gender
}
println("Person Constructor leave....")
}
class Student(name: String, age: Int,major: String) extends Person(name,age){
println("Student Constructor enter....") println("Student Constructor leaver....")
}
运行结果
Person Constructor enter…
Person Constructor leave…
Student Constructor enter…
Student Constructor leaver…
pk 19 null
会先调用父类构造器,再调用子类构造器,注意子类的特有参数获取不到,子类特有参数需要加var才能获取
重写:属性和函数在父类拥有时子类重写需要加overrride来修饰
override def toString: String = "override def toString"
image.png
Object类必问
抽象类
object AbstactApp {
def main(args: Array[String]): Unit = {
val student = new Student2()
println(student.name+" "+student.age+" ")
student.speak
}
}
/** * 类的一个或者多个方法没有完整的实现(只有定义,没有实现)
*/
abstract class Person2{
def speak
val name: String
val age: Int
}
class Student2 extends Person2{
override def speak: Unit = {
println("say hello")
}
override val name: String ="pk"
override val age: Int = 18
}
伴生类和伴生对象
object ApplyApp {
def main(args: Array[String]): Unit = {
}
}
//伴生类和伴生对象
//如果有一个class 还有一个class同名的object
//那么称这个object是class的伴生对象,class是object的伴生类
class ApplyTest{
}
object ApplyTest{
}
apply
object ApplyApp {
def main(args: Array[String]): Unit = {
// for (i<- 1.to(10)){
// ApplyTest.incr
// }
// println(ApplyTest.count)
//结果为10 说明object是个单例对象
var b = ApplyTest()
//调用的是object方法里面的apply 加了()默认走的是object里面的apply println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") var c = new ApplyTest() println(c) c()
//类名() ==》object.apply
//对象()==》Class.apply
}
}
//伴生类和伴生对象
//如果有一个class 还有一个class同名的object
//那么称这个object是class的伴生对象,class的object的伴生类class ApplyTest{
def apply() ={
println("Class AppltTest apply...")
new ApplyTest
}
}
object ApplyTest{
println("enter....")
var count = 0
def incr={
count+=1
}
//最佳实战:在object的apply方法中去
new Class
def apply() ={
println("Object AppltTest apply...")
new ApplyTest
}
println("leaver....")
}
面试常问:
看到伴生类名字加个()
默认走的就是object的apply方法
半生对象名字加个()默认走的class的apply方法
case class
object CaseClassApp {
//通常用在模式匹配
def main(args: Array[String]): Unit = { println(Dog("wangcai").name)
}
}
//case class不用newcase class Dog(name: String){
}
trait
Trait xxx extends ATrait with BTrait
例子:
12.png调整自己的心态,生活失意时,静品命运苦乐韵味;人生腾达时,静听生命辉煌乐章!许多人的失败和痛苦,不是出不去,而是回不来。