定义抽象类形状。形状包含专用字符串颜色。

这篇文章探讨了Java中抽象类Shape的概念,详细介绍了Circle和Cone的定义,以及它们如何通过继承Shape抽象类实现公共方法。重点讲解了圆形和圆锥体的属性和计算方法,展示了如何创建对象并打印其信息。
摘要由CSDN通过智能技术生成

定义抽象类形状。形状包含专用字符串颜色。形状包含抽象方法:Shape(), public Shape(String s) 、public double getArea()  、double getVolume()、ToString()、定义圆从形状继承。圆包含私有数据双 x,y 作为中心位置,双半径。圆圈覆盖形状中的所有抽象方法。定义圆锥体从圆圈继承。锥包含双高的私人数据。锥体从圆继承的方法。

package experience6;

import java.util.*;

public class experience6____shapeabstract {

public static void printfo (Object o) {

    if(o instanceof Circle) {

        System.out.println(((Circle)o).toString());

    }

    else if(o instanceof Cone) {

        System.out.println(((Cone)o).toString());

    }

    else  System.out.println("can not know what the shape is");

}

public static void main(String[] args) {

    Object c1=new Circle(1,1,1);

    Object c2=new Cone(1);

    printfo(c1);

    printfo(c2);

}

}

 abstract class Shape{

    private String color;

    public Shape() {

        color="whilte";

    }

    public  Shape(String s) {

        color=s;

    }//构造方法不能抽象

    public String getcolor() {

        return color;

    }

    public abstract double getarea();

    public abstract double getvolume();

    public String toString() {

        return "the color is"+color;

    }

}

 class Circle extends Shape{

     private double x;

     private double y;

     private double radious;

     public Circle(){

         this(1,1,1);

     }

     public Circle(double x,double y,double radious) {

         this.x=x;

         this.y=y;

         this.radious=radious;

     }

     public  double getarea() {

         return 3.14*radious*radious;

     }

     public double getvolume() {

         return 0;

        }

     public double  getradious() {

         return radious;

     }

     public String toString() {

            return "     the color is "+this.getradious()+"     the area is "+this.getarea()+"     the volumen is  "+this.getvolume();

        }

 }

 class Cone extends Circle{

     private Date createddated;

     private double height;

     public Cone(double height){

         this.height=height;

         createddated=new Date();

         

     }//无论子类构造方法有参无参,只要没有显示调用父类的构造方法,那么在子类构造方法第一句一定会加上父类的无参构造

     public  double getarea() {

         return 3.14*this.getradious()*this.getradious();

     }

     public double getvolume() {

         return height*3.14*this.getradious();

        }

     public String toString() {

            return "    the color is "+this.getcolor()+"    the cone heigh is    "+this.height+"    the area is   "+this.getarea()+"   the volume is   "+this.getvolume();

        }

 }

1、抽象类与操作符重载 定义表示形状抽象类及相应的派生,并实现相关操作符重载。 (1)定义表示形状抽象类Shape: 添加公有成员函数double Area(),用于计算形状面积;定义为纯虚函数; 添加公有成员函数void Show(),用于显示形状信息,定义为纯虚函数; 定义虚的析构函数; 重载比较操作符:==、>和<,用于比较两个形状面积的大小关系,返回值型为bool,可以定义为成员函数或友元函数。 、、、、、、 2、虚函数 利用虚函数实现多态: (1)设计Person,要求具有用于表示姓名的保护数据成员:string szName; 实现信息打印的公有成员函数:void Print()。其中,Print函数设计为虚函数,输出的信息格式为:“Person 姓名”。 、、、、、、 3、操作符重载 定义有理数(分母不为0的分数,分子分母均为整数)Rational,实现相应操作符的重载。 (1)定义私有数据成员:分子int iUp; 分母 int iDown。 (2)定义私有成员函数:void Reduce() 和 int Gcd(int l, int r),分别用于有理数的约简和求两个整数的最大公约数。其中,在约简时需要求取分子与分母的最大公约数。 、、、、、、 4、记录文件的读写操作 源数据文件(文本格式)中包含有每个学生的记录:ID(身份识别号)、 Gender(性别)、 Birthday(生日)和EnrollmentDate(入学时间),字段之间以半角逗号分隔,记录之间以换行符分隔。要求从源数据文件中读取学生记录并删除重复记录,然后根据ID大小对所有记录按从小到大排序,将排序后的记录保存到目标文件中并同时输出到屏幕上。 、、、、、、 5、简单文本文件的读写 、、、、、
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值