抽象类的继承并实现打印类引用顺序

package test;
import java.math.*;
   abstract class Shape{
	String color;
	public  abstract double  area() ;
	public abstract String toString();
	public abstract  String getColor();	
	public Shape(){
		  System.out.println("Shape constructor called");
		 }
}
 
   class Circle extends Shape{
	   
	   String color;
	   double radius;
    	public Circle(String color,double radius) {
    	this.color=color;
    	this.radius=radius;
    	System.out.println("Circle constructor called");
    	}
    	
	public double area() {
		return radius*radius*Math.PI;	
	}
	public String toString() {
		return "Circle constructor called";
	}
	public String getColor() {
		return color;
	}
	
	}
   
class Rectangle extends Shape{

	 String color;
	 double length;
	 double width;
  	public Rectangle(String color,double length,double width) {
  	this.color=color;
  	this.length=length;
  	this.width=width;
  	System.out.println("Rectangle constructor called");
	}

	public double area() {
		
		return length*width;
	}

	public String toString() {
		return "Rectangle constructor called";
	}

	public String getColor() {
		return color;
	}
	

}

public class Test001 {
public static void main(String[] args) {
	Shape s1=new Circle("Green",3.1);
	Shape s2=new Rectangle("Blue",3,4);
	System.out.println("Circle color is "+s1.getColor()+" and area is "+s1.area());
	System.out.println("Rectangle color is "+s2.getColor()+" and area is "+s2.area());

	
	
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值