zoukankan      html  css  js  c++  java
  • java中的抽象类

                                                                                     java中的抽象类

    abstract class PlaneGraphics //平面类,抽象类
    {
        private String shape; //形状
       //构造方法,有参数
        public PlaneGraphics(String shape)
        {
            this.shape=shape;
        }
     //无参数,构造方法
        public PlaneGraphics()
        {
            this("未知图形");
        }
        //抽象方法
        public abstract double area();
        //非抽象方法,显示面积
        public void print()
        {
            System.out.println(this.shape+"面积为:"+this.area());
        }
        
        }
    //长方形类,继承平面类
    class Rectangle extends PlaneGraphics
    {
        protected double length; //长
        protected double width; //宽
        //有参数的构造方法
        public Rectangle(double length,double width)
        {
            this.length=length;
            this.width=width;
        }
        //正方形构造方法
        public Rectangle(double width)
        {
            super("正方形");
            this.width=width;
            this.length=width;
            
        }
        //无参数构造方法
        public Rectangle()
        {
            
        }
        //计算长方形的构造方法,实现父类的抽象方法
        public double area()
        {
            return width*length;
        }
        
        }
    //设计椭圆类,继承平面类
    class Eclipse extends PlaneGraphics
    {
        protected double radius_a;  //a轴半径
        protected double radius_b;
        //构造方法
        public Eclipse(double radius_a,double radius_b)
        {
            this.radius_a=radius_a;
            this.radius_b=radius_b;
        }
        //圆的构造方法
        public Eclipse(double radius_a)
        {
            super("圆形:");
            this.radius_a=radius_a;
            this.radius_b=radius_a;
                    
        }
        //无参数构造方法
        public Eclipse()
        {
            
        }
        //计算椭圆的面积
        public double area()
        {
            return Math.PI*radius_a*radius_b;
        }
        }

    public class PlaneGraphics_ex {
        public static void main(String[]args)
        {
            PlaneGraphics g=new Rectangle(10,20);
            g.print();
            g=new Rectangle(10); //正方形
            g.print();
            g=new Rectangle(); //未知图形方法
            g.print();
            g=new Eclipse(10,20); //椭圆
            g.print();
            g=new Eclipse(10); //圆
            g.print();
            g=new Eclipse(); //圆形形状未知
            g.print();
        }

    }

    1.抽象类中只包括抽象方法,方法的声明。而方法的具体的实现是由其子类实现的。

    2.抽象类不能实例化,不能基于抽象类来创建新的对象。

    3.抽象类中可以包含成员变量和成员方法,但是抽象方法只能出现抽象类,不能出现在其他的类中。

    编程是一门艺术,要爱就要深爱。
  • 相关阅读:
    违反了引用完整性约束。Dependent Role 具有多个具有不同值的主体。S级乌龙,自己制造的笑话
    用MVC5+EF6+WebApi 做一个小功能(二) 项目需求整理
    用MVC5+EF6+WebApi 做一个小功能(四) 项目分层功能以及文件夹命名
    用MVC5+EF6+WebApi 做一个小功能(三) 项目搭建
    ASP.NET WebApi总结之自定义权限验证
    用MVC5+EF6+WebApi 做一个小功能(一)开场挖坑,在线答题系统
    Javascript 535种方式!!!实现页面重载
    MVC页面移除HTTP Header中服务器信息
    为什么JavaScript要有null?(翻译)
    可编程渲染管线与着色器语言
  • 原文地址:https://www.cnblogs.com/pwhit/p/4941157.html
Copyright © 2011-2022 走看看