Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Scala inheritance level?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Scala inheritance level is how", 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 inheritance level is how" it!

1. Extends

It's basically the same as in java, so I won't repeat it.

II. Override

Must explicitly use the override keyword

Override def toString () = length + "*" + width + "*" + height

Type checking and conversion, similar to java's instanceOf and type casting

P.isInstanceOf [Person]

Val s = p.asInstanceOf [student]

IV. Protected

Java protected: subclasses and other classes of the same package

Scala protected: subclass

There is also protected [this], which is similar to private [this].

5. the constructor of the parent class

Class TeaTable (length: Int, width: Int, height: Int, cupNum: Int) extends Table (length, width, height) {

Override def toString () = super.toString () + "cup number is" + cupNum

}

New TeaTable (1, 2, 3, 5)

VI. Override of attributes

Overrideval: you can override the val and def of the parent class (no parameters)

Override def: you can override the def of the parent class, which is actually the override of the method

Override var: you can override the def of a parent class, but it must be a pair of get and set, or an abstract var of the parent class

All the above rules, in fact, are in line with the rules of java in override, which we will analyze the class file generated by scala dialectically.

Abstract classes, abstract methods, abstract attributes

Abstract classes need to be defined by abstract, abstract methods and abstract properties are not needed, abstract methods refer to methods that have no concrete implementation, and abstract properties refer to properties without initial values.

Abstract class AbstractTest {

Def cnt: Int / / abstract

Def sum = 2 / / not

Val name: String / / abstract

Val age = 2 / / not

}

VIII. Scala inheritance level

Thank you for your reading, the above is the "Scala inheritance level is how" the content, after the study of this article, I believe you have a deeper understanding of the Scala inheritance level is what this problem, the specific use of the need for you to practice and verify. 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report