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


    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();
        }
    }
  • 相关阅读:
    转载:每一个程序员要遵守的一些优秀编程风格
    Eclipse使用技巧及个性化设计
    博客园颜色、粗细、字体及大小
    转载:开发移动应用的7大设计要点
    转载:15件事造就有理想的程序员
    程序员理想
    找工作主要看什么
    转载:程序员应该扪心自问的10个问题
    程序员保值的4个秘密
    HTML5标签一览
  • 原文地址:https://www.cnblogs.com/lc-java/p/7392409.html
Copyright © 2020-2023  润新知