OO의 기초와 함께 디자인 모델을주의 깊게 연구하기 시작했습니다! 디자인 패턴은 Java 디자인에서 필수적입니다!
Apple.java
패키지 전략;/** * * @Author Andy * */public class Apple은 할인 가능한 {// weight private double weight; // 실제 단가는 설계되었으며 돈과 기타 정확한 계산은 큰 비록입니다. 개인 이중 가격; // 구매 수량으로 할인 된 // 개인 할인 D = New AppleweightDiscountor (); // 구매에 의해 할인 된 총 가격 개인 할인 D = New ApplePrediscountor (); public double getweight () {return weight; } public void setweight (이중 무게) {this.weight = weight; } public double getPrice () {반환 가격; } public void setPrice (이중 가격) {this.price = 가격; } public Apple (이중 무게, 이중 가격) {super (); this.weight = weight; this.price = 가격; } @override public void 할인 셀 () {d.discount (this); }}바나나. 자바
패키지 전략;/** * * @Author Andy * */Public Class Banana는 할인 가능한 {// Weight Private Double Weight; //// 실제 단가 개발에는 돈 및 기타 것과 같은 정확한 계산이 포함됩니다. 퍼블릭 바나나 (이중 무게, 이중 가격) {super (); this.weight = weight; this.price = 가격; } public double getweight () {return weight; } public void setweight (이중 무게) {this.weight = weight; } public double getPrice () {반환 가격; } public void setPrice (이중 가격) {this.price = 가격; } @override public void disslicountselsell () {// 할인 알고리즘 if (weight <5) {System.out.println ( "바나나 미분식 가격 :" + weight * price); } else if (weight> = 5 && weight <10) {System.out.println ( "바나나는 80% 할인 가격이 있습니다." + weight * price * 0.88); } else if (weight> = 10) {System.out.println ( "바나나 50% 할인 가격 :" + weight * price * 0.5); }}}마켓. 자바
패키지 전략;/** * * @Author Andy * */Public Class Market {/** * 할인 된 품목에 대한 할인 * @Param Apple */public static void 할인 셀 (할인 가능한 d) {d.discountsell ();}}할인. 자바
패키지 전략;/** * * @Author Andy * */public interface 할인 {public void 할인 셀 ();}test.java
패키지 전략;/** * * @Author Andy * */public class test {/** * * @param args */public static void main (string [] args) {// Apple 만 할인 할 수는 없지만 일반적인 목적 만 할인 할 수는 없으며 실제로 판매 할 때 할인 된 알고리즘을 작성할 때 // 모든 유형의 할인 알고리즘을 작성할 때 할인 된 알고리즘을 작성하십시오. 3.6); 할인 가능한 D1 = 새로운 바나나 (5.4, 1.1); Market.Discountsell (d); Market.Discountsell (D1); }}