构造一组变量的最佳方法是什么?一组变量有一组变量等等?
What is the best way to structure a group of variables that have a group of variables that have a group of variables and so on?
我正在创建一个计算费率的 Java 程序。费率有一个有高峰的季节,这些高峰有自己的时间和价格。我正在考虑做嵌套 类,但必须有更好的方法,我只是没有考虑它,对吗?
我需要存储的变量:
Rate
Summer
peak
hours
price
midpeak
hours
price
off-peak
hours
price
Non-summer
peak
hours
price
midpeak
hours
price
off-peak
hours
price
这里有一个方法:
class BaseRate {
int hours;
int price;
public BaseRate(int hours, int price) {
this.hours = hours;
this.price = price;
}
}
class SeasonalRate {
BaseRate peakRate;
BaseRate midPeakRate;
BaseRate offPeakRate;
public SeasonalRate(BaseRate peakRate, BaseRate midPeakRate, BaseRate offPeakRate) {
this.peakRate = peakRate;
this.midPeakRate = midPeakRate;
this.offPeakRate = offPeakRate;
}
}
class AnnualRates {
SeasonalRate summerRate;
SeasonalRate winterRate;
public AnnualRates(SeasonalRate summerRate, SeasonalRate winterRate) {
this.summerRate = summerRate;
this.winterRate = winterRate;
}
public static void main(String[] args) {
BaseRate peakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate peakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakSummerRateFor2019 = new BaseRate(1, 3);
SeasonalRate winterRateFor2019 = new SeasonalRate(peakWinterRateFor2019, midPeakWinterRateFor2019, offPeakWinterRateFor2019);
SeasonalRate summerRateFor2019 = new SeasonalRate(peakSummerRateFor2019, midPeakSummerRateFor2019, midPeakWinterRateFor2019);
AnnualRates ratesFor2019 = new AnnualRates(summerRateFor2019, winterRateFor2019);
}
}
我正在创建一个计算费率的 Java 程序。费率有一个有高峰的季节,这些高峰有自己的时间和价格。我正在考虑做嵌套 类,但必须有更好的方法,我只是没有考虑它,对吗?
我需要存储的变量:
Rate
Summer
peak
hours
price
midpeak
hours
price
off-peak
hours
price
Non-summer
peak
hours
price
midpeak
hours
price
off-peak
hours
price
这里有一个方法:
class BaseRate {
int hours;
int price;
public BaseRate(int hours, int price) {
this.hours = hours;
this.price = price;
}
}
class SeasonalRate {
BaseRate peakRate;
BaseRate midPeakRate;
BaseRate offPeakRate;
public SeasonalRate(BaseRate peakRate, BaseRate midPeakRate, BaseRate offPeakRate) {
this.peakRate = peakRate;
this.midPeakRate = midPeakRate;
this.offPeakRate = offPeakRate;
}
}
class AnnualRates {
SeasonalRate summerRate;
SeasonalRate winterRate;
public AnnualRates(SeasonalRate summerRate, SeasonalRate winterRate) {
this.summerRate = summerRate;
this.winterRate = winterRate;
}
public static void main(String[] args) {
BaseRate peakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakWinterRateFor2019 = new BaseRate(1, 3);
BaseRate peakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate midPeakSummerRateFor2019 = new BaseRate(1, 3);
BaseRate offPeakSummerRateFor2019 = new BaseRate(1, 3);
SeasonalRate winterRateFor2019 = new SeasonalRate(peakWinterRateFor2019, midPeakWinterRateFor2019, offPeakWinterRateFor2019);
SeasonalRate summerRateFor2019 = new SeasonalRate(peakSummerRateFor2019, midPeakSummerRateFor2019, midPeakWinterRateFor2019);
AnnualRates ratesFor2019 = new AnnualRates(summerRateFor2019, winterRateFor2019);
}
}