= new Customer("John", 10, new NoDiscount()); System.out.println(String.format("%s pays %.2f per month", student.name, student.pricePerMonth())); System.out.println(String.format("%s pays %.2f per month", regular.name, regular.pricePerMonth()));
= new Customer("John", 10, new NoDiscount()); System.out.println(String.format("%s pays %.2f per month", student.name, student.pricePerMonth())); System.out.println(String.format("%s pays %.2f per month", regular.name, regular.pricePerMonth()));
= new Customer("John", 10, new NoDiscount()); System.out.println(String.format("%s pays %.2f per month", student.name, student.pricePerMonth())); System.out.println(String.format("%s pays %.2f per month", regular.name, regular.pricePerMonth()));
val fee: Double, val discount: Discount) { fun pricePerMonth() = discount(fee) } val studentDiscount = { raw: Double raw/2 } val noDiscount = { raw: Double raw }
val fee: Double, val discount: Discount) { fun pricePerMonth() = discount(fee) } val studentDiscount = { raw: Double raw/2 } val noDiscount = { raw: Double raw } val student = Customer("Ned", 10.0, studentDiscount) val regular = Customer("John", 10.0, noDiscount) println("${student.name} pays %.2f per month".format(student.pricePerMonth())) println("${regular.name} pays %.2f per month".format(regular.pricePerMonth()))
val fee: Double, val discount: Discount) { fun pricePerMonth() = discount(fee) } val studentDiscount = { raw: Double raw/2 } val noDiscount = { raw: Double raw } val student = Customer("Ned", 10.0, studentDiscount) val regular = Customer("John", 10.0, noDiscount) println("${student.name} pays %.2f per month".format(student.pricePerMonth())) println("${regular.name} pays %.2f per month".format(regular.pricePerMonth()))
val fee: Double, val discount: Discount) { fun pricePerMonth() = discount(fee) } val studentDiscount = { raw: Double raw/2 } val noDiscount = { raw: Double raw } val student = Customer("Ned", 10.0, studentDiscount) val regular = Customer("John", 10.0, noDiscount) println("${student.name} pays %.2f per month".format(student.pricePerMonth())) println("${regular.name} pays %.2f per month".format(regular.pricePerMonth()))
Car build(); } public final class Car { private int doors; private String color; public Car(int doors, String color) { this.doors = doors; this.color = color; } } public static CarBuilder newBuilder(){ … }
indicate to the {@link java.lang.Object#clone()} method that it * is legal for that method to make a * field-for-field copy of instances of that class. * <p> * Invoking Object's clone method on an instance that does not implement the * <code>Cloneable</code> interface results in the exception * <code>CloneNotSupportedException</code> being thrown. * <p> * By convention, classes that implement this interface should override * <tt>Object.clone</tt> (which is protected) with a public method. * See {@link java.lang.Object#clone()} for details on overriding this * method. * <p> * Note that this interface does <i>not</i> contain the <tt>clone</tt> method. * Therefore, it is not possible to clone an object merely by virtue of the * fact that it implements this interface. Even if the clone method is invoked * reflectively, there is no guarantee that it will succeed. * */ public interface Cloneable { }
indicate to the {@link java.lang.Object#clone()} method that it * is legal for that method to make a * field-for-field copy of instances of that class. * <p> * Invoking Object's clone method on an instance that does not implement the * <code>Cloneable</code> interface results in the exception * <code>CloneNotSupportedException</code> being thrown. * <p> * By convention, classes that implement this interface should override * <tt>Object.clone</tt> (which is protected) with a public method. * See {@link java.lang.Object#clone()} for details on overriding this * method. * <p> * Note that this interface does <i>not</i> contain the <tt>clone</tt> method. * Therefore, it is not possible to clone an object merely by virtue of the * fact that it implements this interface. Even if the clone method is invoked * reflectively, there is no guarantee that it will succeed. * */ public interface Cloneable { } Clone is broken http://www.artima.com/intv/bloch13.html
implements Text { … } public abstract class TextEffect implements Text { protected Text decorated; … } public class Background extends TextEffect { public Background(Text decorated) { super(decorated); } @Override public void draw() { … decorated.draw(); } … }
implements Text { … } public abstract class TextEffect implements Text { protected Text decorated; … } public class Background extends TextEffect { public Background(Text decorated) { super(decorated); } @Override public void draw() { … decorated.draw(); } … }
new PrintedText(“Underlined with Background”) ) ).draw(); new Background( new Underline( new Blinking( new PrintedText(“Underlined with Background, that blinks!”) ) ) ).draw();