实验目的:在集成开发环境下,实现类的继承及多态性,运用抽象方法、接口及抽象类来对应实际问题来进行程序编写。
实验目标:
(1)能够运用Java语言来实现类的继承及多态性、熟练使用包来扩展程序功能并能建立自己的包;
(2)能够灵活运用Java语言的抽象方法、接口、抽象类来编写应用程序解决实际问题。
实验内容:
1.设计一个抽象类图形类,在该类中包含有至少两个抽象方法求周长和求面积,分别定义圆形类、长方形类、等边三角形类来继承图形类,并实现上述两个方法。并创建实例验证。
2.创建一个抽象类动物类以及以下几个接口:进食的接口、睡觉的接口、思考的接口、说话的接口、飞翔的接口。分别定义猫类、人类、鸟类继承动物类并选择实现上述的接口,并创建实例验证。
1. 抽象类
//图形抽象类
public abstract class Graphic {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Graphic(String type) {
this.type = type;
}
public abstract void area();
public abstract void perimeter();
}
下面继承上面
//圆形
public class Circle extends Graphic{
private double r; //半径
private static final double PI = Math.PI;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Circle(String type, double r) {
super(type);
setR(r);
}
public void area() {
System.out.print("圆形的面积为: ");
System.out.println(PI * r * r);
}
public void perimeter() {
System.out.print("圆形的周长为: ");
System.out.println(PI * r * 2);
}
}
//等边三角形
public class EquilateralTriangle extends Graphic{
private double l; //等边三角形的边长
public double getL() {
return l;
}
public void setL(double l) {
this.l = l;
}
public EquilateralTriangle(String type, double l) {
super(type);
setL(l);
}
public void area() {
System.out.print("等边三角形的面积为: ");
System.out.println((Math.sqrt(3) * l * l ) / 4 );
}
public void perimeter() {
System.out.print("等边三角形的周长为: ");
System.out.println(3 * l);
}
}
//长方形
public class Rectangle extends Graphic{
private double a; //矩形的长
private double b; //矩形的宽
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public Rectangle(String type, double a, double b) {
super(type);
setA(a);
setB(b);
}
public void area() {
System.out.print("矩形的面积为: ");
System.out.println(a * b);
}
public void perimeter() {
System.out.print("矩形的周长为: ");
System.out.println((a + b) * 2);
}
}
测试类
public class Test {
public static void main(String[] args) {
Graphic graphic1 = new Circle("圆形", 5);
graphic1.area();
graphic1.perimeter();
Graphic graphic2 = new Rectangle("矩形", 5, 6);
graphic2.area();
graphic2.perimeter();
Graphic graphic3 = new EquilateralTriangle("等边三角形", 4);
graphic3.area();
graphic3.perimeter();
}
}
2. 动物抽象类
动物类
public abstract class Animal {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Animal(String type) {
setType(type);
}
public abstract void eat();
public abstract void sleep();
public abstract void think();
public abstract void talk();
public abstract void fly();
}
下面继承上面
public class Bird extends Animal{
public Bird(String type) {
super(type);
}
public void eat() {
System.out.println("鸟儿可以吃饭~");
}
public void sleep() {
System.out.println("鸟儿可以睡觉~");
}
public void think() {
System.out.println("鸟儿可以思考~");
}
public void talk() {
System.out.println("鸟儿不能说话!");
}
public void fly() {
System.out.println("鸟儿当然可以飞~");
}
}
public class Cat extends Animal{
public Cat(String type) {
super(type);
}
public void eat() {
System.out.println("猫猫可以吃饭~");
}
public void sleep() {
System.out.println("猫猫可以睡觉~");
}
public void think() {
System.out.println("猫猫可以思考~");
}
public void talk() {
System.out.println("猫猫不能说话!");
}
public void fly() {
System.out.println("猫猫不能飞!");
}
}
public class Person extends Animal{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//构造方法
public Person(String type, String name) {
super(type);
setName(name);
}
public void eat() {
System.out.println(name + "可以吃饭~");
}
public void sleep() {
System.out.println(name + "可以睡觉~");
}
public void think() {
System.out.println(name + "可以思考~");
}
public void talk() {
System.out.println(name + "可以说话~");
}
public void fly() {
System.out.println(name + "不能飞!");
}
}
测试类
public class Test {
public static void main(String[] args) {
Animal person = new Person("人", "老杨");
person.eat();
person.sleep();
person.think();
person.talk();
person.fly();
Animal cat = new Cat("猫");
cat.eat();
cat.sleep();
cat.think();
cat.talk();
cat.fly();
Animal bird = new Bird("鸟");
bird.eat();
bird.sleep();
bird.think();
bird.talk();
bird.fly();
}
}