Case: rectangular and circular print
Rectangle: x (abscissa), y (ordinate), length (length), wid (width)
Round: x (abscissa), y (vertical axis), r (radius)
Ideas:
Rectangular and circular are common elements: x (abscissa), y (ordinate)
Then can be x, y define a shape of the extracted class as a parent class, the shape class contains two attributes.
Define a rectangle class, the class inherits shape. Extension length (length), wid (widths) in their class attributes
Define a circular class, the class inherits shape. R & lt extension (radius) in its own class properties
Write a test class, a corresponding method is defined to implement the functions.
Code:
1. Define a shape classes:
public class the Shape { // shape classes Private int X; // described abscissa Private int Y; // described ordinate public Shape() { super(); } public the Shape ( int X, int Y) { Super (); // in this.x = X; // this.y from = Y; setX (X); // recommended such an approach, it is possible to make a judgment on the x, y values . Just in the corresponding process, to set the judgment condition setY (y); } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY () { return y; } public void setY ( int y) { esta y = y; } public void show() { System.out.println ( "horizontal and vertical coordinates :(" getX + () + "," + getY () + ")" ); } }
2. Define a rectangle class
// Rectangular Class public class Rect the extends the Shape { Private int len; // description length information Private int WID; // described width information public Rect() { super(); } public Rect(int x, int y,int len, int wid) { super(x, y); setLen(len); setWid(wid); } public int getLen() { return len; } public void setLen ( int len) { this .len = len; } public int getWid() { return wid; } public void setWid(int wid) { this.wid = wid; } @Override public void show() { System.out.println ( "I'm a rectangular" ); Super .Show (); System.out.println ( "length:" + getLen () + "Width:" + getWid ()); } }
3. Define a circular class
// define circular class public class Circle the extends the Shape { Private int R & lt; // described radius public Circle() { super(); } public Circle(int x, int y, int r) { super(x, y); setR(r); } public int getR() { return r; } public void setR(int r) { this.r = r; } @Override public void show() { System.out.println ( "I'm a circular" ); Super .Show (); System.out.println ( "Radius:" + GETR ()); } }
4. Define a measurement method depending on the class, and implements the functions corresponding to prepare
package com.monkey1025; public class the Test { // custom a method: The method is preferably defined as static, no new object class name static method () system call name @ requirements: the printing method can print circular but rectangular, parameters passed by the specific pattern
public static void Draw (shape s) { // make a parent class shape parameter, so not only is the argument of this class of objects, and objects can pass subclass // shape type s Shape can only be invoked at compile their own type of show () method // but any Shape of a sub-class of the show () method has been rewritten in the operational phase subclass own rewrite of the call s.show () ; } public static void main(String[] args) { // Test.draw (S); the process parameter is needed is a reference type Shape, both parameters are objects of the class, and can pass an object subclass // create an anonymous object rectangle class passed as an argument to parameter // is the equivalent: Shape s = new Rect (1,2,3,4 ); polymorphic form Test.draw ( new new Rect (1,2 , 3,4 )); System.out.println("-----------------------------"); // Create circular anonymous object class as an argument passed to the parameter @ is the equivalent: Shape s = new Circle (5,6,7 ); polymorphic form Test.draw ( new new Circle (5,6, 7 )); } }
Polymorphic benefits: