In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to check prerequisites, add fields and self-pointing in Scala". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Currently, the main constructor accepts passing zeros to d:
Scala > new Rational (5,0) res6: Rational = 5 Universe 0
One of the advantages of object-oriented programming is that it allows you to encapsulate data in objects so that you can ensure that the data is valid throughout the lifecycle. Immutable objects like Rational, which means that you have to make sure that the data is valid when the object is created (and that the object is indeed immutable so that the data does not become invalid later). Since zero as the denominator is not valid for Rational, it is important that Rational is not constructed when passing zero to d.
The solution to this problem is to define a prerequisite for the primary constructor: the precondition states that d must be a non-zero value. The prerequisite is a restriction on the value passed to the method or constructor, which is a requirement that the caller must meet. One way is to use the require method, which is defined on the orphaned object Predef in the scala package. Such as:
Class Rational (n: Int, d: Int) {require (d! = 0) override def toString = n + "/" + d}
The require method takes a Boolean parameter. If the value passed in is true, require will return normally. Instead, require will prevent the object from being constructed by throwing an IllegalArgumentException.
Add a field
Now that the main constructor can correctly implement the prerequisites, we will focus on supporting addition. To do this, we will define an exposed add method on the class Rational, which takes another Rational as a parameter. In order to keep the Rational immutable, the add method must not add the incoming score to itself. Instead, you must create and return a brand new Rational with cumulative values. You might think you could write add:
Class Rational (n: Int, d: Int) {/ / compile but require (d! = 0) override def toString = n + "/" + d def add (that: Rational): Rational = new Rational (n * that.d + that.n * d, d * that.d)}
Unfortunately, the above code will prompt the compiler to say:
< console>: 11: error: value d is not a member of Rational new Rational (n * that.d + that.n * d, d * that.d)
< console>: 11: error: value d is not a member of Rational new Rational (n * that.d + that.n * d, d * that.d)
Although the class parameters n and d are within the range referenced by your add code, only their values can be accessed in the object that calls add. Therefore, when you say n or d in your add implementation, the compiler will gladly provide you with the values of these class parameters. But you will never be allowed to use that.n or that.d, because that does not point to the Rational object to which add is called. In fact, when that refers to an object that calls add, Rational can be added to itself. But because you can pass any Rational object to add, the compiler still won't let you say that.n. To access the n and d of that, you need to put them in the field. Code 6. 1 shows how to add these fields to the class Rational.
In the Rational version shown in Code 6.1, we added two fields, numer and denom, and initialized them with the class parameters n and d. Although n and d are used in the function body of the class, because they are only used within the constructor, the Scala compiler will not automatically construct fields for them. So in terms of this code, the Scala compiler will produce a class with two int fields, one is numer and the other is denom. We also changed the implementation of toString and add to use fields instead of class parameters. This version of the class Rational can be compiled and tested by adding fractions:
Class Rational (n: Int, d: Int) {require (d! = 0) val numer: Int = n val denom: Int = numer+ "/" + denom def add (that: Rational): Rational = new Rational (numer * that.denom + that.numer * denom, denom * that.denom)}
Code 6.1 Rational with fields
Scala > val oneHalf = new Rational (1, 2) oneHalf: Rational = 1 scala > val twoThirds = new Rational (2, 3) twoThirds: Rational = 2 scala 3 scala > oneHalf add twoThirds res0: Rational = 7 max 6
Another thing that could not be done before but can now be done is to access numerators and denominators outside the object. Simply access the public numer and denom fields:
Scala > val r = new Rational (1,2) r: Rational = 1 / 2 scala > r.numer res7: Int = 1 scala > r.denom res8: Int = 2
Self pointing
The keyword this refers to the object instance that the current execution method is called or, if used in the constructor, the object instance being built. For example, consider adding a method, lessThan, to test whether the given score is less than the passed parameter:
Def lessThan (that: Rational) = this.numer * that.denom < that.numer * this.denom
Here, this.numer points to the molecule of the object on which the lessThan is called. You can also remove the this prefix and just write numer; in the same way.
To give an example where you can't do without this, consider adding the max method to the Rational class to return the larger of the specified scores and parameters:
Def max (that: Rational) = if (this.lessThan (that)) that else this
Here, * this is redundant, and so is your (lessThan (that)). But the second this represents the result of the method when the test is false; if you omit it, nothing is returned.
That's all for "how to check prerequisites, add fields, and point to yourself in Scala". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 276
*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.