定义一个抽象基类Shape

更新时间:2023-08-28 13:15:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

//定义一个抽象基类Shape,其中包含area方法

abstract class Shape

{

public abstract double area();

}

//定义Cirele并且继承Shape类,实现area方法

class Circle extends Shape

{

private double r;

public Circle(double r)

{

this.r = r;

}

public double area()

{

return Math.PI*r*r;

}

}

//定义Rectangle并且继承Shape类,实现area方法,并且对其进行重载

class Rectangle extends Shape

{

private double r;

private double length;

public Rectangle(double r, double length)

{

this.r = r;

this.length = length;

}

public double area()

{

double temp;

temp = 2 * Math.PI * r * length;

return temp + 2 * Math.PI * r * r;

}

public double area(double r, double length)

{

double temp;

temp = 2 * Math.PI * r * length;

return temp + 2 * Math.PI * r * r;

}

}

//定义MyTest作为测试类

public class MyTest

{

public static void main(String[] args)

{

Circle d=new Circle(2);

Rectangle s=new Rectangle(2, 2);

System.out.println(d.area());

System.out.println(s.area());

System.out.println(s.area(3, 3));

}

}

import java.util.*;

class Mydate

{

private Calendar date = Calendar.getInstance();

public void setTime(int hour, int minute, int second)

{

Date temp= new Date();

Temp.sethous(hour);

Temp.setminutes(minute);

Temp.setseconds(second);

date.setTime(temp);

}

public void setDate(int year, int month, int day)

{

date.set(year, month-1, day);

}

public String getNowDate()

{

return date.get(Calendar.YEAR) + "年" + (date.get(Calendar.MONTH )+1) + "月" + date.get(Calendar.DAY_OF_MONTH) + "日";

}

public String getNowTime()

{

return date.get(Calendar.HOUR) + ":" + date.get(Calendar.MINUTE ) + ":" + date.get(Calendar.SECOND );

}

}

public class Test

{

public static void main(String[] args)

{

Mydate my=new Mydate();

System.out.println(my.getNowDate());

System.out.println(my.getNowTime());

my.setDate(2012, 9, 9);

my.setTime(1, 12, 12);

System.out.println(my.getNowDate());

System.out.println(my.getNowTime());

}

}

本文来源:https://www.bwwdw.com/article/ovhi.html

Top