视频地址:尚硅谷大数据技术之Scala入门到精通教程(小白快速上手scala)_哔哩哔哩_bilibili
- 尚硅谷大数据技术Scala教程-笔记01【Scala课程简介、Scala入门、变量和数据类型、运算符、流程控制】
- 尚硅谷大数据技术Scala教程-笔记02【函数式编程】
- 尚硅谷大数据技术Scala教程-笔记03【面向对象】
- 尚硅谷大数据技术Scala教程-笔记04【集合】
- 尚硅谷大数据技术Scala教程-笔记05【模式匹配、异常】
目录
第06章-面向对象
P070【070_尚硅谷_Scala_面向对象(一)_包(一)_声明和访问】14:52
P071【071_尚硅谷_Scala_面向对象(一)_包(二)_包对象】08:47
P072【072_尚硅谷_Scala_面向对象(一)_包(三)_导包说明】07:05
P073【073_尚硅谷_Scala_面向对象(二)_类和对象】12:07
P074【074_尚硅谷_Scala_面向对象(三)_封装(一)_访问权限】16:03
P075【075_尚硅谷_Scala_面向对象(三)_封装(二)_构造器】17:32
P076【076_尚硅谷_Scala_面向对象(三)_封装(三)_构造器参数】18:46
P077【077_尚硅谷_Scala_面向对象(四)_继承】15:31
P078【078_尚硅谷_Scala_面向对象(五)_多态】15:11
P079【079_尚硅谷_Scala_面向对象(六)_抽象类(一)_抽象属性和方法】13:33
P080【080_尚硅谷_Scala_面向对象(六)_抽象类(二)_匿名子类】03:37
P081【081_尚硅谷_Scala_面向对象(七)_伴生对象(一)_概念和应用】15:18
P082【082_尚硅谷_Scala_面向对象(七)_伴生对象(二)_单例设计模式】08:18
P083【083_尚硅谷_Scala_面向对象(八)_特质(一)_基本概念和用法】18:55
P084【084_尚硅谷_Scala_面向对象(八)_特质(二)_特质的混入】11:22
P085【085_尚硅谷_Scala_面向对象(八)_特质(三)_特质的叠加】06:24
P086【086_尚硅谷_Scala_面向对象(八)_特质(四)_钻石问题的特质叠加】13:52
P087【087_尚硅谷_Scala_面向对象(八)_特质(五)_特质和抽象类的区别】03:41
P088【088_尚硅谷_Scala_面向对象(八)_特质(六)_自身类型】08:32
P089【089_尚硅谷_Scala_面向对象(九)_扩展内容(一)_类型检测和转换】11:58
P090【090_尚硅谷_Scala_面向对象(九)_扩展内容(二)_枚举类和应用类】06:17
package com {
import com.atguigu.scala.Inner
//在外层包中定义单例对象
object Outer {
var out: String = "out"
def main(args: Array[String]): Unit = {
println(Inner.in)
}
}
package atguigu {
package scala {
//内层包中定义单例对象
object Inner {
var in: String = "in"
def main(args: Array[String]): Unit = {
println(Outer.out)
Outer.out = "outer"
println(Outer.out)
}
}
}
}
}
//在同一文件中定义不同的包
package aaa {
package bbb {
object Test01_Package {
def main(args: Array[String]): Unit = {
import com.atguigu.scala.Inner
println(Inner.in)
}
}
}
}
package object chapter06 {
//定义当前包共享的属性和方法
val commonValue = "尚硅谷"
def commonMethod() = {
println(s"我们在${commonValue}学习!")
}
}
package chapter06
object Test02_PackageObject {
def main(args: Array[String]): Unit = {
commonMethod()
println(commonValue)
}
}
package chapter06 {
object Test02_PackageObject {
def main(args: Array[String]): Unit = {
commonMethod()
println(commonValue)
}
}
}
package ccc {
package ddd {
object Test02_PackageObject {
def main(args: Array[String]): Unit = {
println(school)
}
}
}
}
// 定义一个包对象
package object ccc {
val school: String = "atguigu"
}
- 和 Java 一样,可以在顶部使用 import 导入,在这个文件中的所有类都可以使用。
- 局部导入:什么时候使用,什么时候导入。在其作用范围内都可以使用。
- 通配符导入:import java.util._
- 给类起名:import java.util.{ArrayList=>JL}
- 导入相同包的多个类:import java.util.{HashSet, ArrayList}
- 屏蔽类:import java.util.{ArrayList =>_,_}
- 导入包的绝对路径:new _root_.java.util.HashMap
package chapter06
import scala.beans.BeanProperty
object Test03_Class {
def main(args: Array[String]): Unit = {
//创建一个对象
val student = new Student()
//student.name // error, 不能访问private属性
println(student.age)
println(student.sex)
student.sex = "female"
println(student.sex)
}
}
//定义一个类
class Student {
//定义属性
private var name: String = "alice"
@BeanProperty
var age: Int = _
var sex: String = _
}
package chapter06
object Test04_ClassForAccess {
}
// 定义一个父类
class Person {
private var idCard: String = "3523566"
protected var name: String = "alice"
var sex: String = "female"
private[chapter06] var age: Int = 18
def printInfo(): Unit = {
println(s"Person: $idCard $name $sex $age")
}
}
package chapter06
object Test04_Access {
def main(args: Array[String]): Unit = {
// 创建对象
val person: Person = new Person()
//person.idCard // error
//person.name // error
println(person.age)
println(person.sex)
person.printInfo()
var worker: Worker = new Worker()
//worker = new Worker()
//worker.age = 23
worker.printInfo()
}
}
// 定义一个子类
class Worker extends Person {
override def printInfo(): Unit = {
//println(idCard) // error
name = "bob"
age = 25
sex = "male"
println(s"Worker: $name $sex $age")
}
}
package chapter06
object Test05_Constructor {
def main(args: Array[String]): Unit = {
val student1 = new Student1
student1.Student1()
val student2 = new Student1("alice")
val student3 = new Student1("bob", 25)
}
}
// 定义一个类
class Student1() {
// 定义属性
var name: String = _
var age: Int = _
println("1. 主构造方法被调用")
//声明辅助构造方法
def this(name: String) {
this() //直接调用主构造器
println("2. 辅助构造方法一被调用")
this.name = name
println(s"name: $name age: $age")
}
def this(name: String, age: Int) {
this(name)
println("3. 辅助构造方法二被调用")
this.age = age
println(s"name: $name age: $age")
}
def Student1(): Unit = {
println("一般方法被调用")
}
}
package chapter06
object Test06_ConstructorParams {
def main(args: Array[String]): Unit = {
val student2 = new Student2
student2.name = "alice"
student2.age = 18
println(s"student2: name = ${student2.name}, age = ${student2.age}")
val student3 = new Student3("bob", 20)
println(s"student3: name = ${student3.name}, age = ${student3.age}")
val student4 = new Student4("cary", 25)
//println(s"student4: name = ${student4.name}, age = ${student4.age}")
student4.printInfo()
val student5 = new Student5("bob", 20)
println(s"student3: name = ${student5.name}, age = ${student5.age}")
student3.age = 21
val student6 = new Student6("cary", 25, "atguigu")
println(s"student6: name = ${student6.name}, age = ${student6.age}")
student6.printInfo()
}
}
// 定义类
// 无参构造器
class Student2 {
// 单独定义属性
var name: String = _
var age: Int = _
}
// 上面定义等价于
class Student3(var name: String, var age: Int)
// 主构造器参数无修饰
class Student4(name: String, age: Int) {
def printInfo() {
println(s"student4: name = ${name}, age = $age")
}
}
//class Student4(_name: String, _age: Int) {
// var name: String = _name
// var age: Int = _age
//}
class Student5(val name: String, val age: Int)
class Student6(var name: String, var age: Int) {
var school: String = _
def this(name: String, age: Int, school: String) {
this(name, age)
this.school = school
}
def printInfo() {
println(s"student6: name = ${name}, age = $age, school = $school")
}
}
package chapter06
object Test07_Inherit {
def main(args: Array[String]): Unit = {
val student1: Student7 = new Student7("alice", 18)
val student2 = new Student7("bob", 20, "std001")
student1.printInfo()
student2.printInfo()
val teacher = new Teacher
teacher.printInfo()
def personInfo(person: Person7): Unit = {
person.printInfo()
}
println("=========================")
val person = new Person7
personInfo(student1)
personInfo(teacher)
personInfo(person)
}
}
// 定义一个父类
class Person7() {
var name: String = _
var age: Int = _
println("1. 父类的主构造器调用")
def this(name: String, age: Int) {
this()
println("2. 父类的辅助构造器调用")
this.name = name
this.age = age
}
def printInfo(): Unit = {
println(s"Person: $name $age")
}
}
// 定义子类
class Student7(name: String, age: Int) extends Person7(name, age) {
var stdNo: String = _
println("3. 子类的主构造器调用")
def this(name: String, age: Int, stdNo: String) {
this(name, age)
println("4. 子类的辅助构造器调用")
this.stdNo = stdNo
}
override def printInfo(): Unit = {
println(s"Student: $name $age $stdNo")
}
}
class Teacher extends Person7 {
override def printInfo(): Unit = {
println(s"Teacher")
}
}
public class TestDynamicBind {
public static void main(String[] args) {
Worker worker = new Worker();
System.out.println(worker.name);
worker.hello();
worker.hi();
System.out.println("===================");
// 多态
Person person = new Worker();
System.out.println(person.name); // 静态绑定属性
person.hello(); // 动态绑定方法
// person.hi(); // error
}
}
class Person {
String name = "person";
public void hello() {
System.out.println("hello person");
}
}
class Worker extends Person {
String name = "worker";
public void hello() {
System.out.println("hello worker");
}
public void hi() {
System.out.println("hi worker");
}
}
package chapter06
object Test08_DynamicBind {
def main(args: Array[String]): Unit = {
val student: Person8 = new Student8
println(student.name)
student.hello()
}
}
class Person8 {
val name: String = "person"
def hello(): Unit = {
println("hello person")
}
}
class Student8 extends Person8 {
override val name: String = "student"
override def hello(): Unit = {
println("hello student")
}
}
package chapter06
object Test09_AbstractClass {
def main(args: Array[String]): Unit = {
val student = new Student9
student.eat()
student.sleep()
}
}
// 定义抽象类
abstract class Person9 {
// 非抽象属性
var name: String = "person"
// 抽象属性
var age: Int
// 非抽象方法
def eat(): Unit = {
println("person eat")
}
// 抽象方法
def sleep(): Unit
}
// 定义具体的实现子类
class Student9 extends Person9 {
// 实现抽象属性和方法
var age: Int = 18
def sleep(): Unit = {
println("student sleep")
}
// 重写非抽象属性和方法
// override val name: String = "student"
name = "student"
override def eat(): Unit = {
super.eat()
println("student eat")
}
}
package chapter06
object Test10_AnnoymousClass {
def main(args: Array[String]): Unit = {
val person: Person10 = new Person10 {
override var name: String = "alice"
override def eat(): Unit = println("person eat")
}
println(person.name)
person.eat()
}
}
// 定义抽象类
abstract class Person10 {
var name: String
def eat(): Unit
}
package chapter06
object Test11_Object {
def main(args: Array[String]): Unit = {
// val student = new Student11("alice", 18)
// student.printInfo()
val student1 = Student11.newStudent("alice", 18)
student1.printInfo()
val student2 = Student11.apply("bob", 19)
student2.printInfo()
val student3 = Student11("bob", 19)
student3.printInfo()
}
}
//定义类
class Student11 private(val name: String, val age: Int) {
def printInfo() {
println(s"student: name = ${name}, age = $age, school = ${Student11.school}")
}
}
//伴生对象
object Student11 {
val school: String = "atguigu"
//定义一个类的对象实例的创建方法
def newStudent(name: String, age: Int): Student11 = new Student11(name, age)
def apply(name: String, age: Int): Student11 = new Student11(name, age)
}
package chapter06
object Test12_Singleton {
def main(args: Array[String]): Unit = {
val student1 = Student12.getInstance()
student1.printInfo()
val student2 = Student12.getInstance()
student2.printInfo()
println(student1)
println(student2)
}
}
class Student12 private(val name: String, val age: Int) {
def printInfo() {
println(s"student: name = ${name}, age = $age, school = ${Student11.school}")
}
}
// 饿汉式
//object Student12 {
// private val student: Student12 = new Student12("alice", 18)
// def getInstance(): Student12 = student
//}
// 懒汉式
object Student12 {
private var student: Student12 = _
def getInstance(): Student12 = {
if (student == null) {
// 如果没有对象实例的话,就创建一个
student = new Student12("alice", 18)
}
student
}
}
package chapter06
object Test13_Trait {
def main(args: Array[String]): Unit = {
val student: Student13 = new Student13
student.sayHello()
student.study()
student.dating()
student.play()
}
}
// 定义一个父类
class Person13 {
val name: String = "person"
var age: Int = 18
def sayHello(): Unit = {
println("hello from: " + name)
}
def increase(): Unit = {
println("person increase")
}
}
// 定义一个特质
trait Young {
// 声明抽象和非抽象属性
var age: Int
val name: String = "young"
// 声明抽象和非抽象的方法
def play(): Unit = {
println(s"young people $name is playing")
}
def dating(): Unit
}
class Student13 extends Person13 with Young {
// 重写冲突的属性
override val name: String = "student"
// 实现抽象方法
def dating(): Unit = println(s"student $name is dating")
def study(): Unit = println(s"student $name is studying")
// 重写父类方法
override def sayHello(): Unit = {
super.sayHello()
println(s"hello from: student $name")
}
}package chapter06
object Test13_Trait {
def main(args: Array[String]): Unit = {
val student: Student13 = new Student13
student.sayHello()
student.study()
student.dating()
student.play()
}
}
// 定义一个父类
class Person13 {
val name: String = "person"
var age: Int = 18
def sayHello(): Unit = {
println("hello from: " + name)
}
def increase(): Unit = {
println("person increase")
}
}
// 定义一个特质
trait Young {
// 声明抽象和非抽象属性
var age: Int
val name: String = "young"
// 声明抽象和非抽象的方法
def play(): Unit = {
println(s"young people $name is playing")
}
def dating(): Unit
}
class Student13 extends Person13 with Young {
// 重写冲突的属性
override val name: String = "student"
// 实现抽象方法
def dating(): Unit = println(s"student $name is dating")
def study(): Unit = println(s"student $name is studying")
// 重写父类方法
override def sayHello(): Unit = {
super.sayHello()
println(s"hello from: student $name")
}
}
package chapter06
object Test14_TraitMixin {
def main(args: Array[String]): Unit = {
val student = new Student14
student.study()
student.increase()
student.play()
student.increase()
student.dating()
student.increase()
println("===========================")
// 动态混入
val studentWithTalent = new Student14 with Talent {
override def dancing(): Unit = println("student is good at dancing")
override def singing(): Unit = println("student is good at singing")
}
studentWithTalent.sayHello()
studentWithTalent.play()
studentWithTalent.study()
studentWithTalent.dating()
studentWithTalent.dancing()
studentWithTalent.singing()
}
}
// 再定义一个特质
trait Knowledge {
var amount: Int = 0
def increase(): Unit
}
trait Talent {
def singing(): Unit
def dancing(): Unit
}
class Student14 extends Person13 with Young with Knowledge {
// 重写冲突的属性
override val name: String = "student"
// 实现抽象方法
def dating(): Unit = println(s"student $name is dating")
def study(): Unit = println(s"student $name is studying")
// 重写父类方法
override def sayHello(): Unit = {
super.sayHello()
println(s"hello from: student $name")
}
// 实现特质中的抽象方法
override def increase(): Unit = {
amount += 1
println(s"student $name knowledge increased: $amount")
}
}
package chapter06
object Test15_TraitOverlying {
def main(args: Array[String]): Unit = {
val student = new Student15
student.increase()
// 钻石问题特征叠加
val myFootBall = new MyFootBall
println(myFootBall.describe())
}
}
trait Knowledge15 {
var amount: Int = 0
def increase(): Unit = {
println("knowledge increased")
}
}
trait Talent15 {
def singing(): Unit
def dancing(): Unit
def increase(): Unit = {
println("talent increased")
}
}
class Student15 extends Person13 with Talent15 with Knowledge15 {
override def dancing(): Unit = println("dancing")
override def singing(): Unit = println("singing")
override def increase(): Unit = {
super[Person13].increase()
}
}
package chapter06
object Test15_TraitOverlying {
def main(args: Array[String]): Unit = {
val student = new Student15
student.increase()
// 钻石问题特征叠加
val myFootBall = new MyFootBall
println(myFootBall.describe())
}
}
// 定义球类特征
trait Ball {
def describe(): String = "ball"
}
// 定义颜色特征
trait ColorBall extends Ball {
var color: String = "red"
override def describe(): String = color + "-" + super.describe()
}
// 定义种类特征
trait CategoryBall extends Ball {
var category: String = "foot"
override def describe(): String = category + "-" + super.describe()
}
// 定义一个自定义球的类
class MyFootBall extends CategoryBall with ColorBall {
override def describe(): String = "my ball is a " + super[CategoryBall].describe()
}
trait Knowledge15 {
var amount: Int = 0
def increase(): Unit = {
println("knowledge increased")
}
}
trait Talent15 {
def singing(): Unit
def dancing(): Unit
def increase(): Unit = {
println("talent increased")
}
}
class Student15 extends Person13 with Talent15 with Knowledge15 {
override def dancing(): Unit = println("dancing")
override def singing(): Unit = println("singing")
override def increase(): Unit = {
super[Person13].increase()
}
}
1.优先使用特质。一个类扩展多个特质是很方便的,但却只能扩展一个抽象类。
2.如果你需要构造函数参数,使用抽象类。因为抽象类可以定义带参数的构造函数,而特质不行(有无参构造)。
package chapter06
object Test16_TraitSelfType {
def main(args: Array[String]): Unit = {
val user = new RegisterUser("alice", "123456")
user.insert()
}
}
// 用户类
class User(val name: String, val password: String)
trait UserDao {
_: User =>
// 向数据库插入数据
def insert(): Unit = {
println(s"insert into db: ${this.name}")
}
}
// 定义注册用户类
class RegisterUser(name: String, password: String) extends User(name, password) with UserDao
package chapter06
object Test17_Extends {
def main(args: Array[String]): Unit = {
// 1. 类型的检测和转换
val student: Student17 = new Student17("alice", 18)
student.study()
student.sayHi()
val person: Person17 = new Student17("bob", 20)
person.sayHi()
// 类型判断
println("student is Student17: " + student.isInstanceOf[Student17])
println("student is Person17: " + student.isInstanceOf[Person17])
println("person is Person17: " + person.isInstanceOf[Person17])
println("person is Student: " + person.isInstanceOf[Student17])
val person2: Person17 = new Person17("cary", 35)
println("person2 is Student17: " + person2.isInstanceOf[Student17])
// 类型转换
if (person.isInstanceOf[Student17]) {
val newStudent = person.asInstanceOf[Student17]
newStudent.study()
}
println(classOf[Student17])
// 2. 测试枚举类
println(WorkDay.MONDAY)
}
}
class Person17(val name: String, val age: Int) {
def sayHi(): Unit = {
println("hi from person " + name)
}
}
class Student17(name: String, age: Int) extends Person17(name, age) {
override def sayHi(): Unit = {
println("hi from student " + name)
}
def study(): Unit = {
println("student study")
}
}
// 定义枚举类对象
object WorkDay extends Enumeration {
val MONDAY = Value(1, "Monday")
val TUESDAY = Value(2, "TuesDay")
}
// 定义应用类对象
object TestApp extends App {
println("app start")
type MyString = String
val a: MyString = "abc"
println(a)
}
package chapter06
object Test17_Extends {
def main(args: Array[String]): Unit = {
// 1. 类型的检测和转换
val student: Student17 = new Student17("alice", 18)
student.study()
student.sayHi()
val person: Person17 = new Student17("bob", 20)
person.sayHi()
// 类型判断
println("student is Student17: " + student.isInstanceOf[Student17])
println("student is Person17: " + student.isInstanceOf[Person17])
println("person is Person17: " + person.isInstanceOf[Person17])
println("person is Student: " + person.isInstanceOf[Student17])
val person2: Person17 = new Person17("cary", 35)
println("person2 is Student17: " + person2.isInstanceOf[Student17])
// 类型转换
if (person.isInstanceOf[Student17]) {
val newStudent = person.asInstanceOf[Student17]
newStudent.study()
}
println(classOf[Student17])
// 2. 测试枚举类
println(WorkDay.MONDAY)
}
}
class Person17(val name: String, val age: Int) {
def sayHi(): Unit = {
println("hi from person " + name)
}
}
class Student17(name: String, age: Int) extends Person17(name, age) {
override def sayHi(): Unit = {
println("hi from student " + name)
}
def study(): Unit = {
println("student study")
}
}
// 定义枚举类对象
object WorkDay extends Enumeration {
val MONDAY = Value(1, "Monday")
val TUESDAY = Value(2, "TuesDay")
}
// 定义应用类对象
object TestApp extends App {
println("app start")
type MyString = String
val a: MyString = "abc"
println(a)
}
艰巨在于漫长,光荣在于平淡。