/**5-3
* Add a new Printitmyway (char) method to the printable interface,
* This method has a character-type parameter, and the return value is null.
* Its function is to use the given character to print, for example, if the given character is *, a length of 4
* The screen with a width of 3 rectangle is printed as:
* ****
* ****
* ****
**/
public class firstprogram{public static void main (String[] args) {Rectangle rectangle = new rectangle (5, 3); Square square = new square (4); Rectangle.printitmyway (' * '); Square.printitmyway (' * ');}} Interface printable{void printitmyway (); Void printitmyway (char n);} class rectangle implements printable{protected int length;protected int width; rectangle () {}rectangle (int l, int w) {this.length = l;this.width = w;} Void show () {System.out.println (" length: " + length + " width: " + width);} Void print (char n) {int i, j;for (i = 0; i < width; i + +) {for (j = 0; j < length; j++) System.out.print (" " + n + " "); System.out.println ();} System.out.println ();} int perimeter () {return (Length + width) * 2;} int area () {return length * width;} public void printitmyway () {this.show (); System.out.println (" permiter: " + this.perimeter () + " area: " + this.area ());} public void printitmyway (char n) { this.print (n); }} class square extends rectangle{ protected int side; square (int side) {super (); this.side = side;} Void show () {System.out.println (" side: " + side);} Void print (char n) {int i, j;for (i = 0; i < side; i+ +) {for (j = 0; j < side; j++) System.out.print (" " + n + " "); System.out.println ();}} Int perimeter () {return 4 * side;} int area () {return side * side;}}
This article is from the "hacker" blog, make sure to keep this source http://anglecode.blog.51cto.com/5628271/1619857
Those years, learn together Java 5-3