我有一个类(bot)和一个子类(播放器),我用api填充了json。我想创建一个新的子类并对其进行序列化,但我不确定如何创建一个实际上是子类成员的新子类。如何添加这个新的子类? console.writeline正在按预期工作,但一旦序列化,预期的数据就不存在了。它就好像正在添加子类但不是父类的成员。
public class bot {
public clan clan { get; set; }
public war war { get; set; }
public player player{get;set;}
}
public class player : bot {
public string tag { get; set; }
public long discordId { get; set; }
public attacks[] attacks { get; set; }
public attacks[] defenses { get; set; }
public aliases[] aliases { get; set; }
}
...
bot bot = new bot();
bot.clan = JsonConvert.DeserializeObject<clan>(json.ToString());
//assign aliases
foreach (Memberlist member in bot.clan.memberList) {
Console.WriteLine(member.tag);
player a = new player();
a.tag = member.tag;
}
答案 0 :(得分:1)
我将播放器类更改为列表,然后我可以使用bot.player.add将新成员添加到对象中。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
final TextView lefty = (TextView)findViewById(R.id.left);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
Long preTime = calendar.getTimeInMillis();
calendar.add(Calendar.HOUR_OF_DAY,8);
Long postTime = calendar.getTimeInMillis();
Long delay = postTime - preTime;
Intent intent = new Intent(getApplicationContext(),NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
CountDownTimer timer = new CountDownTimer(delay, 1) {
@Override
public void onTick(long millisUntilFinished) {
final int seconds = (int) (millisUntilFinished / 1000) % 60;
final int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
final int hour = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
runOnUiThread(new Runnable() {
@Override
public void run() {
lefty.setText( hour + " : " + minutes + " : " + seconds);
}
});
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
timer.start();
}
});
button2.setOnClickListener(new View.OnClickListener() {
"some code"
}
}
...
public class bot {
public List<player> player { get; set; }
}