1、通过程序设计几何图形:矩形、圆形、正方形等类型,能够利用接口和多态性计算几何图形的面积和周长并显示。
2、按以下要求编写程序
1)编写Animal接口,接口中声明run()方法;
2)定义Bird类和Fish类实现Animal接口;
3)编写Bird类和Fish类的测试程序,并调用其中的run()方法
1)编写Animal接口,接口中声明run()方法;
2)定义Bird类和Fish类实现Animal接口;
3)编写Bird类和Fish类的测试程序,并调用其中的run()方法
代码
interface zhouchang{
public void zhouchang();
}
interface mianji{
public void mianji();
}
abstract class zong implements zhouchang,mianji {
int chang,kuan,banjing,zhouchang,mianji;
public zong(int chang,int kuan,int banjing) {
this.chang = chang;
this.kuan = kuan;
this.banjing = banjing;
}
}
class Rectangle extends zong{
public Rectangle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("矩形的周长是:"+zhouchang);
}public void mianji(){
mianji = chang*kuan;
System.out.println("矩形的面积是:"+mianji);
}
}
class Circle extends zong{
public Circle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (int) (2*3.14*banjing);
System.out.println("圆形的周长是:"+zhouchang);
}
public void mianji(){
mianji = (int) (3.14*(banjing*banjing));
System.out.println("圆形的面积是:"+mianji);
}
}
class Square extends zong{
public Square(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("正方形的周长是:"+zhouchang);
}
public void mianji(){
mianji = chang*kuan;
System.out.println("正方形的面积是:"+mianji);
}
}
public class Shape {
public static void main(String[] args) {
Rectangle re = new Rectangle(10,12, 0);
re.zhouchang();
re.mianji();
Circle ci = new Circle(0, 0, 10);
ci.zhouchang();
ci.mianji();
Square sq = new Square(12, 12, 0);
sq.zhouchang();
sq.mianji();
}
}
public void zhouchang();
}
interface mianji{
public void mianji();
}
abstract class zong implements zhouchang,mianji {
int chang,kuan,banjing,zhouchang,mianji;
public zong(int chang,int kuan,int banjing) {
this.chang = chang;
this.kuan = kuan;
this.banjing = banjing;
}
}
class Rectangle extends zong{
public Rectangle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("矩形的周长是:"+zhouchang);
}public void mianji(){
mianji = chang*kuan;
System.out.println("矩形的面积是:"+mianji);
}
}
class Circle extends zong{
public Circle(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (int) (2*3.14*banjing);
System.out.println("圆形的周长是:"+zhouchang);
}
public void mianji(){
mianji = (int) (3.14*(banjing*banjing));
System.out.println("圆形的面积是:"+mianji);
}
}
class Square extends zong{
public Square(int chang, int kuan, int banjing) {
super(chang, kuan, banjing);
}
public void zhouchang(){
zhouchang = (chang+kuan)*2;
System.out.println("正方形的周长是:"+zhouchang);
}
public void mianji(){
mianji = chang*kuan;
System.out.println("正方形的面积是:"+mianji);
}
}
public class Shape {
public static void main(String[] args) {
Rectangle re = new Rectangle(10,12, 0);
re.zhouchang();
re.mianji();
Circle ci = new Circle(0, 0, 10);
ci.zhouchang();
ci.mianji();
Square sq = new Square(12, 12, 0);
sq.zhouchang();
sq.mianji();
}
}
interface Animal{
public void run();
}
class Bird implements Animal{
public void run(){
System.out.println("鸟:我的行走方式是飞行");
}
}
class Fish implements Animal{
public void run(){
System.out.println("鱼:我的行走方式是游泳");
}
}
public class dongwu {
public static void main(String[] args) {
Bird b1 = new Bird();
b1.run();
Fish f1 = new Fish();
f1.run();
}
}
public void run();
}
class Bird implements Animal{
public void run(){
System.out.println("鸟:我的行走方式是飞行");
}
}
class Fish implements Animal{
public void run(){
System.out.println("鱼:我的行走方式是游泳");
}
}
public class dongwu {
public static void main(String[] args) {
Bird b1 = new Bird();
b1.run();
Fish f1 = new Fish();
f1.run();
}
}