1. 程式人生 > >JAVA基礎篇—抽象類,抽象方法

JAVA基礎篇—抽象類,抽象方法

java b- rec package rate idt -c over stub

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();
    }
}

JAVA基礎篇—抽象類,抽象方法