1 成员变量
package com._51doit.day05.demo.oob
import scala.beans.BeanProperty
/**
* FileName: Demo
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 成员变量
* 1 位置 柱构造器
* 2 类的主体位置
* 3 val var修饰 var 修饰的可以不赋值 _ val 必须赋值 在主构造器中的除外
*/
class Demo1(val name:String){
// val name = "zss" // 一定辅助
var age:Int = _ // get 和 set
@BeanProperty
var job:String = _
}
object Demo {
def main(args: Array[String]): Unit = {
val demo1 = new Demo1("lss")
demo1.age=23
/*demo1.getJob
demo1.job*/
println(demo1.name)
println(demo1.age)
}
}
2 成员方法
package com._51doit.day05.demo.oob
/**
* FileName: Demo2
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
class Demo2{
// 成员方法
def show: Unit ={
}
//成员函数
val f = (x:Int,t:Int)=>{
x+t
}
val f2:(String,String)=>String = (str1,str2)=>{
str1+str2
}
val f3= (str1:String, str2:String)=>{
str1+str2
}
val f4 = show _
}
object Demo2 {
// 成员方法
def show2: Unit ={
}
//成员函数
val f2 = (x:Int,t:Int)=>{
x+t
}
}
3 成员代码块
package com._51doit.day05.demo.oob
/**
* FileName: Demo3
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
class Demo3 {
//每次创建实例都会执行
{
println("hello")
}
}
object Demo3 {
// 只在加载的时候执行一次
{
println("hello2")
}
}
4 apply函数
这是一个特殊的方法
1 不用声明式的调用方法
2 创建对象的时候不用new
使用此方法时,可以在main函数中不通过new来创建一个对象,即可以不用专门的一次一次地进行实例化,加载创建对象的这个类的时候,会自动调用apply这个方法,类似Java中的static静态块。
建议将这个方法写在半生对象中 , 如果将这个方法写在一个没有半生对象的类中是无法使用的
package com._51doit.day05.demo.oob
/**
* FileName: AppDemo
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* apply方法建议定义在object中
*/
class AppDemo {
// 定义两个成员属性
var x:Int = _
var y:Int = _
// 定义一个两个参数的构造器
def this(x:Int,y:Int){
this()
this.x=x
this.y=y
}
// 普通的方法
def show(): Unit ={
println("show....")
}
}
object AppDemo{
// 在伴生对象中实现apply方法
def apply(): AppDemo ={
new AppDemo
}
// 在伴生对象中实现apply方法 连个参数 调用两个参数的构造器
def apply(x:Int,y:Int): AppDemo ={
new AppDemo(x,y)
}
def main(args: Array[String]): Unit = {
val demo1 = new AppDemo
val demo2 =AppDemo() //-->apply new AppDemo -->
val demo3: AppDemo = AppDemo(22, 33)
println(demo3.x)
}
}
object TestApply {
def main(args: Array[String]): Unit = {
val demo: AppDemo = AppDemo(33, 44)
println(demo.x)
}
}
5 classOf、isInstanceOf、asInstanceOf
package com._51doit.day05.demo.oob
/**
* FileName: TestInstance
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
object TestInstance {
/**
*
* @param args
*
*/
def main(args: Array[String]): Unit = {
val a = new A
println(a.isInstanceOf[A])
val b = new B
// 强制类型转换 有继承关系的强转
b.asInstanceOf[A]
// 数值类型 toInt toDouble .....
// 集合类型 toArray toList toMap ....
println(classOf[A]) // 类似于全类名
println(Class.forName("com._51doit.day05.demo.oob.A"))
val clazz: Class[_] = Class.forName("com._51doit.day05.demo.oob.A")
clazz.getName
// 获取类的字节码
val c: Class[A] = classOf[A]
}
}
6 权限修饰符
package com._51doit.day05.demo.xsf
/**
* FileName: Demo1
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 1 成员变量 定义在类中 (或者主构造器中使用val|var)
* 2 private 修饰 private val gender:String = "M"
* 1) private修饰的变量可以在本类和伴生对象中直接访问 , 其他类无法访问
*/
class Demo1(private val name:String) {
val ID:Int=123456
//私有的属性
private val gender:String = "M"
def show(): Unit ={
// 在本类中可以访问
print(gender)
print(name)
}
}
object Demo1{
def main(args: Array[String]): Unit = {
val demo = new Demo1("zss")
}
}
7特质和抽象类
package com._51doit.day05.demo.trait_dmeo
/**
* FileName: Animal
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 特质 就是接口
*/
trait Animal {
def name:String
}
package com._51doit.day05.demo.trait_dmeo
/**
* FileName: T1
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
trait T1 {
def i:Int
}
package com._51doit.day05.demo.trait_dmeo
/**
* FileName: Dog
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
class Dog extends Animal with T1 {
// 方法的重写
override def name: String = {
"汪汪....."
}
override def i: Int = {
12
}
}
8 样例类
package com._51doit.day05.demo.case_class
/**
* FileName: People
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 样例类 : 用来防止数据使用 就是javaBean
* 必须声明主构造器
* 1 样例类实现了序列化接口
* 2 toString
* 3 hashCode equals
* 4 属性() set属性()
* 5 可以指定排序规则
* 隐式转换 ????
*/
case class People(val id:Int , val name:String , val age:Int)
/* extends Ordered[People] {
override def compare(that: People): Int = {
-1
}
}*/
9 高阶函数
方法的参数或者返回值是函数的方法
编码灵活 指定操作规则
Collecttions.sort(list , 排序规则) : java中的排序 参数一 排序集合 , 参数二 ; 指定的排序规则 传入一个匿名内部类[臃肿] , 效率低
函数: 能实现方法的功能 单独存在 , 特殊的对象 , 可以作为方法的参数
Collecttions.sort(list , 函数) //f: String => String = <function>
package com._51doit.day05.demo.hi_func
/**
* FileName: Demo1
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
object Demo1 {
val f = (x: Int) => x * 100
val f1 = (x: Int) => x * 10
val f2 = (x: Int) => x * x
def main(args: Array[String]): Unit = {
val ls = List(1, 2, 3, 4)
println(ls.map(f).map(f1).map(f2))
}
}
package com._51doit.day05.demo.hi_func
/**
* FileName: Demo2
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
class Demo2 {
/**
* 方法返回值是一个函数
* 参数Int 返回值是Int
* @param x
* @return
*/
def opt(x:Int) ={
(y:Int)=>y*x
}
}
object Demo2{
def main(args: Array[String]): Unit = {
val demo = new Demo2
val f: Int => Int = demo.opt(12)
val res: Int = f(22) // 高级
println(res)
println(demo.opt(10)(11))
}
}
10 偏函数
偏函数是一种特质 , 专门用来处理某一种数据类型的函数 注意 map不支持偏函数 collect支持偏函数
package com._51doit.day05.demo.part
/**
* FileName: Demo1
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
object Demo1 {
def main(args: Array[String]): Unit = {
val ls: List[Any] = List[Any](1, 2, 3, 4, "java", "js", true, 12.34)
// 1 使用过滤器
/* val ls2: List[Any] = ls.filter(_.isInstanceOf[Int])
println(ls2.map(_.asInstanceOf[Int] * 10).toList)*/
// 2 可以使用偏函数处理
/**
* 偏函数 特质 new 特质() 重写抽象方法
* 泛型:
* 1参数一 集合存储的数据类型 Any
* 2参数二 需要处理的数据类型
*/
val f = new PartialFunction[Any, Int] {
// x:接收每个元素 每个元素都会调用一次这个方法 判断元素的数据类型是否是要处理的类型
override def isDefinedAt(x: Any): Boolean = {
// 如果是true 执行下一个方法进行处理数据
x.isInstanceOf[Int]
}
// v1 接收上个方法返回true数据
override def apply(v1: Any): Int = {
//将接收数据强制转换成 对应的类型
val i: Int = v1.asInstanceOf[Int]
i * 10
}
}
/**
* collect和map的执行一样的 但是接收的是偏函数
* map是不支持偏函数
*/
println(ls.collect(f))
val res = ls.collect({
// 遍历集合中的每个元素 判断每个元素是否是 Int 如果是 =>
case x: Int => x * 100
// case x:String=> (x+"sadsfds").toUpperCase
})
res.foreach(println)
}
}
11 匹配模式
11.1 数据类型
package com._51doit.day05.demo.cas
/**
* FileName: Demo1
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 匹配模式 很灵活的匹配需要处理的数据 减少判断和循环次数 简洁 效率高
* 1 匹配数据类型
*/
object Demo1 {
def main(args: Array[String]): Unit = {
val arr = Array(1, 2, 3, "hello", "java", 12.12d)
/*val res1: Array[Int] = arr.filter(_.isInstanceOf[Int]).map(_.asInstanceOf[Int] * 10)
val res2: Array[String] = arr.filter(_.isInstanceOf[String]).map(_.asInstanceOf[String].toUpperCase)
val res3: Array[Double] = arr.filter(_.isInstanceOf[Double]).map(_.asInstanceOf[Double] * 100)
val res: Array[Any] = res1 ++ res2 ++ res3
val array: Array[Any] = res1.union(res2).union(res3)
res.foreach(println)*/
val res = arr.map(e=>e match {
case x:Int=>x*10
case x:String=>x.toUpperCase
case x:Double=>x*100
})
res.foreach(println)
}
}
package com._51doit.day05.demo.cas
/**
* FileName: Demo2
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
object Demo2 {
def main(args: Array[String]): Unit = {
val ls = List(
List[Int](1,2,3,4),
List[String]("a","b","c","d"),
"hello",
Array[Int](2,4,6,8) ,
Map[String ,Int]("a"->22,"b"->33) ,
Map[Int,Int]((11,11) ,(22,22))
)
val res = ls.map(e=>e match {
case x:List[Int]=> x // 无法匹配几集合的泛型
case x:Array[Int]=>x.map(_*100)
case x:Map[_,_]=>x.keys // 无法匹配map几集合的泛型
case _ => "a"
})
res.foreach(println)
}
}
package com._51doit.day05.demo.cas
/**
* FileName: Demo3
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
*/
object Demo3 {
def main(args: Array[String]): Unit = {
val ls = List(new A, new B, new A)
val res = ls.map(e => e match {
case x: A => "aaaa"
case x: B => "bbbb"
})
res.foreach(println)
}
}
11.2 元祖匹配
package com._51doit.day05.demo.cas
/**
* FileName: Demo4
* Author: 多易教育-DOIT
* Date: 2020/11/5 0005
* Description:
* 元组匹配
* 1 (x,y,z) 匹配指定个数的元祖
* 2 (x:Int,y:Int,z:Int) 匹配指定个数指定类型的元祖
* 3 case 从上向下执行 , 上面匹配过的元素 下面不会再匹配
* 4 不仅可以指定元祖的个数 指定元祖对应位置的数据类型
*/
object Demo4 {
def main(args: Array[String]): Unit = {
val arr = Array(
(1, 5, 4),
(1, "a", 4),
("hello", "tom", "jim") ,
("hello", 23, "tom", 33)
)
val res = arr.map(tp=>tp match {
case (x,y:String,z)=>(x,y,z)
// case (x,y,z)=>(x,y,z)
case (x,y,z,a)=>(x,y,z,a)
case _ => // 一定有
})
res.foreach(println)
}
}