BAEL-974 - Cleverson Santos | cleverson.ssantos1008@gmail.com (#2213)
* Different Types of Bean Injection in Spring code * Dataclasses in Kotlin * Revert "Different Types of Bean Injection in Spring code" This reverts commit 4b747726b93a9f6bf76d6518792fc77e0d5c2fc9.
This commit is contained in:
parent
67261595dd
commit
f3c99a7fcf
|
@ -0,0 +1,88 @@
|
|||
package com.baeldung.dataclass;
|
||||
|
||||
public class Movie {
|
||||
|
||||
private String name;
|
||||
private String studio;
|
||||
private float rating;
|
||||
|
||||
public Movie(String name, String studio, float rating) {
|
||||
this.name = name;
|
||||
this.studio = studio;
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getStudio() {
|
||||
return studio;
|
||||
}
|
||||
|
||||
public void setStudio(String studio) {
|
||||
this.studio = studio;
|
||||
}
|
||||
|
||||
public float getRating() {
|
||||
return rating;
|
||||
}
|
||||
|
||||
public void setRating(float rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
result = prime * result + Float.floatToIntBits(rating);
|
||||
result = prime * result + ((studio == null) ? 0 : studio.hashCode());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
|
||||
if (obj == null)
|
||||
return false;
|
||||
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
|
||||
Movie other = (Movie) obj;
|
||||
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
|
||||
if (Float.floatToIntBits(rating) != Float.floatToIntBits(other.rating))
|
||||
return false;
|
||||
|
||||
if (studio == null) {
|
||||
if (other.studio != null)
|
||||
return false;
|
||||
|
||||
} else if (!studio.equals(other.studio))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Movie [name=" + name + ", studio=" + studio + ", rating=" + rating + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package com.baeldung.dataclass
|
||||
|
||||
data class Movie(val name: String, val studio: String, var rating: Float)
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.dataclass
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
val movie = Movie("Whiplash", "Sony Pictures", 8.5F)
|
||||
|
||||
println(movie.name) //Whiplash
|
||||
println(movie.studio) //Sony Pictures
|
||||
println(movie.rating) //8.5
|
||||
|
||||
movie.rating = 9F
|
||||
|
||||
println(movie.toString()) //Movie(name=Whiplash, studio=Sony Pictures, rating=9.0)
|
||||
|
||||
val betterRating = movie.copy(rating = 9.5F)
|
||||
println(betterRating.toString()) //Movie(name=Whiplash, studio=Sony Pictures, rating=9.5)
|
||||
|
||||
movie.component1() //name
|
||||
movie.component2() //studio
|
||||
movie.component3() //rating
|
||||
|
||||
val(name, studio, rating) = movie
|
||||
|
||||
fun getMovieInfo() = movie
|
||||
val(namef, studiof, ratingf) = getMovieInfo()
|
||||
}
|
Loading…
Reference in New Issue