Android java: 内联对象初始化未按预期工作
Android java: inline object initialization not working as expected
我的应用程序中有一个 AWTrack class,如下所示:
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import kaaes.spotify.webapi.android.models.Track;
public class AWTrack implements Serializable {
private int id;
private String idSpotify;
private String name;
private String album;
private String author;
private String spotifyPreviewUrl;
private int musicType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdSpotify() {
return idSpotify;
}
public void setIdSpotify(String idSpotify) {
this.idSpotify = idSpotify;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int getMusicType() {
return musicType;
}
public void setMusicType(int musicType) {
this.musicType = musicType;
}
public String getSpotifyPreviewUrl() {
return spotifyPreviewUrl;
}
public void setSpotifyPreviewUrl(String spotifyPreviewUrl) {
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
public AWTrack(){
}
public AWTrack(int id)
{
this.id = id;
}
public static AWTrack map(Track track, int trackId)
{
/*AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};*/
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
}
在这个 class 中,我创建了一个静态映射方法,旨在将 kaaes.spotify.webapi.android.models.Track 对象转换为 AWTrack(仅需要的属性),并且我尝试在以下内容中使用内联对象初始化方式:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};
}
但是我遇到了一个奇怪的问题,那就是生成的 AWTrack 对象中有一个嵌入的(意外的)Track 对象。
如果相反,我以传统方式进行对象初始化 - 就像下一个 -:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
它工作正常,但事实是我希望能够像第一种方法中所示那样进行对象初始化,绝对更喜欢第一种语法。
编辑:为了清楚起见,我的主要问题是在创建对象之后,我用 Gson 将其序列化,如下所示:
ArrayList<AWTrack> list = .....
Gson gson = new Gson();
String json = gson.toJson(list);
如果我对以 classic 方式实例化的对象应用序列化,我会得到如下字符串:
[{"author":"Philip Wesley","id":70,"musicType":0,"name":"Loves Crush","spotifyPreviewUrl":"http://xxx"}]
但是通过内联实例化,我在使用 Gson 序列化后得到的是:
"[null]"
任何帮助将不胜感激。
好的,我会回答我自己的问题。
答案很简单,据我所知,使用 {{}} 的内联初始化会创建一个匿名内部 class (What is Double Brace initialization in Java?),所以这不是您期望的方式它可以工作,如果你做了很多初始化,可能会产生内存泄漏。在许多情况下它可以工作,我喜欢这种语法,但 Gson 不会接受该对象作为序列化的参数。
我最终完成了传统的构造函数方式。最后很清楚,只有一行代码:
public AWTrack(int id, String name, String author, String spotifyPreviewUrl) {
this.id = id;
this.name = name;
this.author = author;
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
然后:
AWTrack awTrack = new AWTrack(trackId, track.name, track.artists.get(0).name, track.preview_url);
我的应用程序中有一个 AWTrack class,如下所示:
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import kaaes.spotify.webapi.android.models.Track;
public class AWTrack implements Serializable {
private int id;
private String idSpotify;
private String name;
private String album;
private String author;
private String spotifyPreviewUrl;
private int musicType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdSpotify() {
return idSpotify;
}
public void setIdSpotify(String idSpotify) {
this.idSpotify = idSpotify;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int getMusicType() {
return musicType;
}
public void setMusicType(int musicType) {
this.musicType = musicType;
}
public String getSpotifyPreviewUrl() {
return spotifyPreviewUrl;
}
public void setSpotifyPreviewUrl(String spotifyPreviewUrl) {
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
public AWTrack(){
}
public AWTrack(int id)
{
this.id = id;
}
public static AWTrack map(Track track, int trackId)
{
/*AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};*/
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
}
在这个 class 中,我创建了一个静态映射方法,旨在将 kaaes.spotify.webapi.android.models.Track 对象转换为 AWTrack(仅需要的属性),并且我尝试在以下内容中使用内联对象初始化方式:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};
}
但是我遇到了一个奇怪的问题,那就是生成的 AWTrack 对象中有一个嵌入的(意外的)Track 对象。
如果相反,我以传统方式进行对象初始化 - 就像下一个 -:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
它工作正常,但事实是我希望能够像第一种方法中所示那样进行对象初始化,绝对更喜欢第一种语法。
编辑:为了清楚起见,我的主要问题是在创建对象之后,我用 Gson 将其序列化,如下所示:
ArrayList<AWTrack> list = .....
Gson gson = new Gson();
String json = gson.toJson(list);
如果我对以 classic 方式实例化的对象应用序列化,我会得到如下字符串:
[{"author":"Philip Wesley","id":70,"musicType":0,"name":"Loves Crush","spotifyPreviewUrl":"http://xxx"}]
但是通过内联实例化,我在使用 Gson 序列化后得到的是:
"[null]"
任何帮助将不胜感激。
好的,我会回答我自己的问题。
答案很简单,据我所知,使用 {{}} 的内联初始化会创建一个匿名内部 class (What is Double Brace initialization in Java?),所以这不是您期望的方式它可以工作,如果你做了很多初始化,可能会产生内存泄漏。在许多情况下它可以工作,我喜欢这种语法,但 Gson 不会接受该对象作为序列化的参数。
我最终完成了传统的构造函数方式。最后很清楚,只有一行代码:
public AWTrack(int id, String name, String author, String spotifyPreviewUrl) {
this.id = id;
this.name = name;
this.author = author;
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
然后:
AWTrack awTrack = new AWTrack(trackId, track.name, track.artists.get(0).name, track.preview_url);