Case Classes
حاملات بيانات غير قابلة للتغيير
Case Classes درس مجاني في Scala for Backend Engineering & Functional Programming على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Scala for Backend Engineering & Functional Programming، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What Is a Case Class?
A case class is a special kind of class designed to hold immutable data. You declare it with case class.
Scala generates a lot of useful code for you automatically, making case classes the go-to way to model data.
Declaring One
Just list the fields in the parameter list. Each parameter becomes an immutable field.
No new keyword is required to create an instance.
case class Point(x: Int, y: Int)
object Main {
def main(args: Array[String]): Unit = {
val p = Point(3, 4)
println(p.x)
println(p.y)
}
}Automatic toString
Case classes get a readable toString for free, showing the class name and field values.
This makes debugging and logging effortless.
case class Point(x: Int, y: Int)
object Main {
def main(args: Array[String]): Unit = {
val p = Point(1, 2)
println(p)
}
}Value Equality
Case classes compare by value, not by reference. Two instances with the same fields are equal.
Regular classes would compare by identity instead.
case class Point(x: Int, y: Int)
object Main {
def main(args: Array[String]): Unit = {
val a = Point(1, 2)
val b = Point(1, 2)
println(a == b)
}
}Immutable Fields
Case class fields are val by default, so they cannot be reassigned.
To get a modified version, you create a new instance instead of mutating the old one.
case class Point(x: Int, y: Int)
object Main {
def main(args: Array[String]): Unit = {
val p = Point(1, 2)
println(p.x)
}
}The copy Method
Every case class has a copy method. It creates a new instance, letting you change just the fields you name.
This is the idiomatic way to update immutable data.
case class Point(x: Int, y: Int)
object Main {
def main(args: Array[String]): Unit = {
val p = Point(1, 2)
val moved = p.copy(y = 99)
println(p)
println(moved)
}
}Pattern Matching Support
Case classes work seamlessly with match. You can deconstruct them by naming their fields in a pattern.
This is one of the biggest reasons to use them.
case class Point(x: Int, y: Int)
object Main {
def describe(p: Point): String = p match {
case Point(0, 0) => "origin"
case Point(x, 0) => s"on x-axis at $x"
case Point(x, y) => s"point ($x, $y)"
}
def main(args: Array[String]): Unit = {
println(describe(Point(0, 0)))
println(describe(Point(5, 0)))
println(describe(Point(2, 3)))
}
}Default Parameter Values
Like normal classes, case classes can have default values for parameters, so you can omit them when constructing.
case class User(name: String, active: Boolean = true)
object Main {
def main(args: Array[String]): Unit = {
val u1 = User("Ada")
val u2 = User("Bo", false)
println(u1)
println(u2)
}
}Adding Methods
A case class can have its own methods in the body, just like any class. They operate on the immutable fields.
case class Rect(w: Int, h: Int) {
def area: Int = w * h
}
object Main {
def main(args: Array[String]): Unit = {
val r = Rect(3, 4)
println(r.area)
}
}Why Case Classes?
Case classes give you, for free:
- A concise constructor with immutable fields
- Readable
toString - Value-based
equalsandhashCode - A
copymethod - First-class pattern matching
They are the foundation for modeling data in Scala.
Putting It Together
Here a case class is created, copied, compared, and pattern matched in one example.
case class Money(amount: Int, currency: String)
object Main {
def main(args: Array[String]): Unit = {
val price = Money(10, "USD")
val sale = price.copy(amount = 8)
println(price == sale)
sale match {
case Money(a, "USD") => println(s"$a dollars")
case Money(a, c) => println(s"$a $c")
}
}
}Quick Check
Test your understanding of case classes.
Recap
You learned that a case class models immutable data with generated helpers:
- Construct without
new; fields are immutablevals - Free
toString, value-basedequals/hashCode copycreates modified clones- Built-in support for pattern matching
- Default values and custom methods are allowed
الأسئلة الشائعة
هل درس «Case Classes» مجاني؟
نعم — نص درس «Case Classes» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Scala for Backend Engineering & Functional Programming، انتقل إلى CoddyKit PRO. تتضمن دورة Scala for Backend Engineering & Functional Programming 4 دروس في المجموع.
ماذا ستتعلم في «Case Classes»؟
حاملات بيانات غير قابلة للتغيير تتمرن على Scala for Backend Engineering & Functional Programming مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Scala for Backend Engineering & Functional Programming؟
لا تُشترط خبرة سابقة. Scala for Backend Engineering & Functional Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «Case Classes»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Scala for Backend Engineering & Functional Programming هذا؟
نعم. كل درس في Scala for Backend Engineering & Functional Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.