偏函数、部分函数、模式匹配、伴生类伴生对象、抽象类
发布日期:2021-05-10 03:44:35 浏览次数:25 分类:精选文章

本文共 3964 字,大约阅读时间需要 13 分钟。

Partially functions in Scala are a fundamental concept, defined with the type signature PartialFunction[-T,+V]. Here, -T represents the input type the function can accept, and +V denotes the return type.

Partial Functions

A Partial Applied Function is a logical concept where a function lacks some parameters. For example:

def sum(x: Int, y: Int, z: Int) = x + y + z

When you call sum without all parameters, or with some parameters missing, you create a Partial Applied Function. For instance:

val num1: Int = 5
def sum: Int => Int = sumResult(num1, _)
println(sum(3))
// Output: 8

Pattern Matching

Pattern matching in Scala is performed using the match keyword to define blocks of code that execute when a match occurs. There are several types of pattern matches:

  • Value Matching

    def match1(x: Int): String = {
    x match {
    case 1 => "one"
    case 2 => "two"
    case _ => "many"
    }
    }
    println(match1(3)) // Output: many
  • Type Matching

    def match2(x: Any): Unit = {
    x match {
    case 1 => println("内容为1")
    case x: Int => println(s"内容是整型,值为$x")
    case s: String => println(s"这是一个字符串,内容为$s")
    case _ => println("我不知道是什么")
    }
    }
    match2("3") // Output: 这是一个字符串,内容为3
  • Case Classes

    case class Teacher(name: String, age: Int) extends Person {
    match3(teacher: Person): Unit = {
    teacher match {
    case Teacher(name, 21) => println(name)
    case Worker(name, age) if name == "repairWorker" => println(s"修理工 $age")
    case person: Student if person.name == "xiaoxuesheng" => println("祖国的花朵")
    case _ => println("不知道是谁")
    }
    }
    match3(Teacher("zzz", 21)) // Output: zzz
    match3(Teacher("zzz", 32)) // Output: hello zzz
  • Companion Objects and Classes

    A Companion Object in Scala is an object that is declared in the same file as the class and can access private members of the class. Here's an example:

    class Oop1(name: String, age: String) {
    // Default constructor
    println("--oop1 class one-----")
    println(name + age)
    var uname: String = name
    var uage: Int = age
    var uaddress: String = ""
    // One-parameter constructor
    def this(name: String) {
    this(name, 1)
    println("oop1 构造方法")
    }
    // No-parameter constructor
    def this() {
    this("meiyou", 1)
    }
    // Three-parameter constructor
    def this(name: String, age: Int, address: String) {
    this(name, age)
    uaddress = address
    }
    def showInfo(): Unit = {
    println("进入class oop1中的showInfo方法")
    println("访问object oop1中的showInfo方法")
    Oop1.showInfo()
    println("访问object oop1中的showInfo方法结束")
    println("name:" + uname + "age:" + uage + "address:" + uaddress + "country" + Oop1.country)
    }
    }
    object Oop1 {
    println("---oop1 object one------")
    val country: String = "中国"
    def showInfo(): Unit = {
    println("---oop1 object three---")
    println("this是oop1 object)
    }
    println("---oop1 object two----")
    def apply(name: String, age: Int): Oop1 = new Oop1(name, age)
    def apply(name: String, age: Int, address: String): Oop1 = new Oop1(name, age, address)
    def main(args: Array[String]): Unit = {
    Oop1.showInfo()
    Oop1.showInfo()
    val oopDemo1 = new Oop1("kb11", 6, "andemen")
    val oopObject = Oop1("wanglei", 18)
    oopObject.showInfo()
    }
    }

    Abstract Classes

    An abstract class in Scala is defined with the abstract keyword and cannot be instantiated directly. Here's an example:

    package cn.kgc.kb11
    abstract class Car {
    def brand: String
    def engine: String
    def laba(): Unit = {
    println("鸣笛)
    }
    }
    trait Type1 {
    def wheele: String = {
    println("四个轮子")
    }
    }

    Inheritance

    Inheritance allows a class to inherit properties and methods from another class. Here's an example:

    class Car extends Parent {
    override def brand: String = {
    println("BWM X6")
    return "BWM X6"
    }
    override def engine: String = {
    println("3.0T 自吸")
    return "3.0T 自吸"
    }
    }

    This structure leverages polymorphism and encapsulation, making it easier to extend and modify classes without breaking existing code.

    上一篇:协变、逆变、jdbc连接数据库、使用隐式类、函数大全
    下一篇:scala函数、匿名函数、柯里化、隐式函数、隐式参数、隐式类

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2025年04月11日 17时51分17秒