代码改变世界

JAVA基础篇—抽象类,抽象方法

2017-08-18 21:57  lc_java  阅读(269)  评论(0编辑  收藏  举报

class Shape

 1 package com.shape;
 2 
 3 public abstract class Shape {
 4     double area;//
 5     double per;//
 6     String color;//
 7     public  Shape() {
 8     }
 9     public  Shape(String c){
10         this.color=c;
11     }
12     abstract  double getArea();
13     abstract  double getPer();
14     abstract  void showAll();
15     public  void getColor(String c){
16         this.color=c;
17         
18     }
19     
20 }
class Shape

class Rectangle

 1 package com.shape;
 2 
 3 public class Rectangle  extends Shape{
 4     double Width;//
 5     double height;//
 6      
 7     @Override
 8     double getArea() {
 9         return super.area=Width*height;
10     }
11 
12     @Override
13     double getPer() {
14         return super.per=Width*2+height*2;
15     }
16 
17     @Override
18     void showAll() {
19         // TODO Auto-generated method stub
20         System.out.println("���ε����Ϊ��"+getArea()+"�ܳ�Ϊ"+getPer());
21     }
22     public Rectangle(){}
23     public  Rectangle(double w,double h){
24         this.Width=w;
25         this.height=h;
26     } 
27 
28 }
Rectangle

class Circle

 1 package com.shape;
 2 
 3 public class Circle  extends Shape{
 4     double radius;//�뾶
 5 
 6     @Override
 7     double getArea() {
 8         return super.area=radius*3.14*radius;
 9     }
10 
11     @Override
12     double getPer() {
13         return super.per=2*radius*3.14;
14     }
15 
16     @Override
17     void showAll() {
18         // TODO Auto-generated method stub
19         System.out.println("Բ�ε����Ϊ��"+getArea()+"�ܳ�Ϊ"+getPer());
20     }
21     public Circle(double r){
22         this.radius=r;
23     }
24    public Circle(String c){
25        this.color=c;
26        
27    }
28 }
Circle

text

package com.shape;

public class text {
    public static void main(String[] args) {
        Rectangle r=new Rectangle(3.0,4.0);
        r.showAll();
        Circle c=new Circle(5.3);
        c.showAll();
    }
}