JAVA 抽象类,接口,包

抽象类

Shape类

abstract   public class Shape {
    double radius,length,width,height;
    Shape(double radius,double height)
    {
    	this.radius=radius;
    	this.height=height;
    }
    Shape(double lengh,double width,double height)
    {
    	this.length=lengh;
    	this.width=width;
    	this.height=height;
    }
    abstract double getVol();
}

Cylinder 类

public class Cylinder extends Shape{
    Cylinder(double radius,double height)
    {
    	super(radius,height);    	
    }
    double getVol()
    {
    	return Math.PI*radius*radius*height;
    }    
}

Rectangle 类

public class Rectangle extends Shape{
	Rectangle(double length,double width,double height)
	{
	    super(length,width,height);
	}
	double getVol()
	{
		//抽象方法
	    return length*width*height;	
	}
}

主程序

Cylinder c1=new Cylinder(1,2);
Rectangle ra=new Rectangle(2,1,3);
System.out.println("圆柱体体积为:"+c1.getVol());
System.out.println("长方体体积为:"+ra.getVol());

 接口:

public interface InterfaceA {
    int X=10;
    void printX();
}
public interface InterfaceB extends InterfaceA {
    int Y=20;
    void printY();
}
interface InterfaceC extends InterfaceB{
    int Z=30;
    void printZ();
}
public class InterfaceDemo implements InterfaceC{
    public void printX()
    {
        System.out.println("接口A中X="+X);
    }
    public void printY()
    {
        System.out.println("接口B中Y="+Y);
    }
    public void printZ()
    {
        System.out.println("接口C中Z="+Z);
    }
}

...
InterfaceDemo id=new InterfaceDemo();
id.printX();
id.printY();
id.printZ();

 

posted @ 2013-07-01 15:25  jhtchina  阅读(180)  评论(0)    收藏  举报