我需要用Java编写代码this class。我想到了做一堆构造函数,但我认为那不是最好的方法。我想知道Java是否具有某种可选参数或属性来简化此过程。
答案 0 :(得分:0)
您可以使用一个带有varargs的构造函数来完成此操作,因为类的所有字段的类型均为db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'UMGC-SDEV300-Key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db.init_app(app)
login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)
from .models import User
@login_manager.user_loader
def load_user(user_id):
# since the user_id is just the primary key of our user table, use it in the query for the user
return User.query.get(int(user_id))
# blueprint for auth routes in our app
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
# blueprint for non-auth parts of app
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
:
String
然后像这样调用它以仅设置街道和城市:
public class Address {
private String street;
private String city;
private String postalCode;
private String state;
private String country;
public Address(String... params) {
street = params[0];
city = params[1];
//etc. Just take care to pass the arguments in the order you are
//assigning them when you call the constructor. Also check the size
//of varargs first so you don't assign arguments that don't exist.
}
}
另一种选择是让一个带有所有参数的构造函数,如果您不想设置一个值,请将Address adress = new Address("myStreet", "myCity");
传递给该构造函数以代替相应的参数:
null