java - Write variables automatically into child class -
i’m trying achieve following. have several model has basic methods, such savemodel, loadmodel, laodmodel from. now, have other classes extend model, e.g. user , movie.
user has variables
id username usermail userpw
movie has
id movietitle movielength moviegenre
now, create new user object , load database. when load model movie, have json-object containing following information:
id = x movietitle = "bla" movielength = "bla" moviegenre = "bla"
as can see, json contains correct key , values, have write them child model. how possible, model writes field values user object? in php this
foreach ($row $key=>$val) { $this->$key = $val; }
in java, doesn’t seem possible. how can parent class automatically write variables child class. or isn’t possible , have manually? problem see here, is, have every model again, when in super class. how that?
one way achieve define parent class have maps holding possible fields; , have child class define fields contained in enums. parent class level serialize maps , key-value pairs. use generic enum parameter give flexibility on key / key type use.
having said so, it's best have code generator generates child classes; if wish, can generate individual method each field pointing specific key in maps well.
Comments
Post a Comment