码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-08-18 23:47:23      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:generate   main   抽象方法   ring   sys   cdc   width   tco   shape   

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基础篇—抽象类,抽象方法

标签:generate   main   抽象方法   ring   sys   cdc   width   tco   shape   

原文地址:http://www.cnblogs.com/lc-java/p/7392409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!