问答详情
源自:10-1 Java 中的多态

第七题怎么写

591c2d660001599908641152.jpg
求解答

提问者:BonnieLLLL 2017-05-17 19:01

个回答

  • weibo_Mr路南_0
    2017-05-17 21:39:20
    已采纳

    //类
    public class Circle {
    	//私有属性radius
    	private int radius;
    	
    	//getter、setter
    	public int getRadius(){
    		return radius;
    	}
    	public void setRadius(int radius){
    		this.radius = radius;
    	}
    	
    	//构造方法
    	public Circle() {}  //无参构造
    	public Circle(int radius){
    		this.radius = radius;
    	}
    	
    	//比较方法
    	public void compareCircle(Circle c){
    		if(this.radius>c.radius){
    			System.out.println("该圆比较大!");
    		}else if(this.radius<c.radius){
    			System.out.println("该圆比较小!");
    		}else{
    			System.out.println("两个圆相等!");
    		}
    	}
    }
    
    //主函数入口
    public static void main(String[] args) {
    		//默认构造 半径为5
    		Circle circle1 = new Circle();
    		circle1.setRadius(5);
    		
    		//带参构造
    		Circle circle2 = new Circle(8);
    		Circle circle3 = new Circle(10);
    		Circle circle4 = new Circle(5);
    		
    		//比较
    		circle1.compareCircle(circle2);
    		circle1.compareCircle(circle3);
    		circle1.compareCircle(circle4);
    }



  • 岛有独鲸
    2017-05-17 20:58:44


    public class Geometry{

        private String color = "white";

        private boolean filled;

        private java.util.Date dateCreated;

        /** construct a default geometry object*/

        public Geometry(){

            dateCreated = new java.util.Date();

        }

        /** construct a geometry object with specified color and filled value */

        public Geometry( String color, boolean filled ){

            dateCreated = new java.util.Date();

            this.color = color;

            this.filled = filled;

        }

        /** return color */

        public String getColor(){

            return color;

        }

        /** set a new color */

        public void setColor( String color ){

            this.color = color;

        }

        /** return filled */

        public boolean isFilled(){

            return filled;

        }

        /** set a new filled */

        public void setFilled( boolean filled ){

            this.filled = filled;

        }

        /** get dateCreated */

        public java.util.Date getDateCreated(){

            return dateCreated;

        }

        /** return a string representation of the object */

        public String toString(){

            return "Geometry created on " + dateCreated + "\ncolor: " + color + "\nfilled is: " + filled;

        }

    }

    public class Circle extends Geometry{

    private double radius;

    public Circle(){

    }

    public Circle( double radius ){

    this.radius = radius;

    }

    public Circle( double radius, String color, boolean filled ){

    super( color, filled );

    this.radius = radius;

    // setColor( color );

    // setFilled( filled );

    // this.color = color;

    // this.filled = filled;

    }

    /** return radius */

    public double getRadius(){

    return radius;

    }

    /** set a new radius */

    public void setRadius( double radius ){

    this.radius = radius;

    }

    /** return area */

    public double getArea(){

    return Math.PI * radius * radius;

    }

    /** return perimeter */

    public double getPerimeter(){

    return 2 * radius * Math.PI;

    }

    /** return diameter */

    public double getDiameter(){

    return 2 * radius;

    }

    /** print circle info */

    public void printCircle(){

    System.out.println( "The circle is created " + super.getDateCreated() + " and radius is " + radius );

    }

    /** return a string representation of the object */

    public String toString(){

    return super.toString() + "\nradius is : " + radius;

    }

    public String getColorCircle(){

    return super.getColor();

    }

    public boolean equals( Object obj ){

    if( obj instanceof Circle ){

    return radius == ((Circle)obj).radius;

    }

    else

    return false;

    }

    }


    public class Rectangle extends Geometry{

    private double width;

    private double height;

    public Rectangle(){

    }

    public Rectangle( double width, double height ){

    this.width = width;

    this.height = height;

    }

    public Rectangle( double width, double height, String color, boolean filled ){

    super( color, filled );

    this.width = width;

    this.height = height;

    }

    /** return width */

    public double getWidth(){

    return width;

    }

    /** set a new width */

    public void setWidth(double width){

    this.width = width;

    }

    /** get height */

    public double getHeight(){

    return height;

    }

    /** set a new height */

    public void setHeight( double height ){

    this.height = height;

    }

    /** return area */

    public double getArea(){

    return width * height;

    }

    /** return perimeter */

    public double getPerimeter(){

    return 2 * ( width + height );

    }

    /** return a string representation of the object */

    public String toString(){

    return super.toString() + "\nwidth is : " + width + "\nheight is : " + height;

    }

    }


    public class TestGeometry{

    public static void main( String[] args ){

    Geometry geo = new Geometry();

    Rectangle rec = new Rectangle( 4.0, 5.0 );

    Circle cir = new Circle( 2.0 );

    System.out.println( geo.toString() );

    System.out.println( rec.toString() );

    System.out.println( rec.getArea() );

    System.out.println( rec.getPerimeter()  );

    System.out.println( cir.toString() );

    System.out.println( cir.getArea() );

    System.out.println( cir.getPerimeter() );

    }

    }