In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Scala superclass how to construct", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Scala superclass how to construct" it!
Extended class
Classes are extended in Scala in the same way as Java by using the extends keyword
You can declare a class as final so that it cannot be expanded
You can also declare a single method or field as final to ensure that it cannot be overridden. Note that unlike Java, final-decorated fields in Java mean immutable.
Rewriting method
Overriding a non-abstract method in Scala must use the override modifier
Call the method of the superclass in the subclass, using super, which is the same as Java.
Fields and methods modified by protected
Fields or methods modified by protected can be accessed by their subclasses
Unlike Java, protected-decorated members are not visible to the package to which the class belongs
Protected [this], access is limited to current objects
You can use package modifiers to change the visibility of protected-decorated members
Type checking and conversion
Scala
Java
Test whether an object belongs to a given class
Obj.isInstanceOf [C]
Obj instanceof C
Forced type conversion
Obj.asInstanceOf [C]
(C) obj
Get the class information of an object
ClassOf [C]
C.class
Construction of superclass
The following code defines a subclass and a main constructor that invokes the superclass constructor
Class Employ (name: String, age: Int, val Salary:Double) extends Person (name,age)
The following code shows the inheritance of the class, the implementation of the interface, etc.
Object ClassDemo {def main (args: Array [String]) {val h = new Human println (h.byScala ())}} / / equivalent to Java interface trait Coding {def byJava (flag: Boolean): Unit = {if (flag) {println ("I can code byJava")} else {println ("I can't code byJava")} / / the unimplemented method defaults to the abstract method def byScala (): String} / / How many times does the class abstract class Animal {def run (): Int val name: String} class Human extends Animal with Coding {val name = "abc" / / print "ABC"? Val T1 Unit T2, (a, b, c) = {println ("ABC") (1p2)} println (a) println (t1.abstract 1) / / rewriting a non-abstract method in Scala must modify override def byJava (flag: Boolean) with override: Unit = {println ("Coding byJava and Scala")} def byScala (): String= {"coding byScala"} / / when overriding a superclass abstract method in a subclass, there is no need to use the override keyword You can also write def run (): Int = {1}}
Thank you for your reading, the above is the content of "how to construct Scala superclass". After the study of this article, I believe you have a deeper understanding of how to construct Scala superclass, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.