实训抽象类 一 实训目标 掌握抽象类的定义 使用 掌握运行时多态 二 知识点 抽象类的语法格式如下 : public abstract class ClassName abstract void 方法名称 ( 参数 ); // 非抽象方法的实现代码 在使用抽象类时需要注意如下几点 : 1 抽象类不能被实例化, 实例化的工作应该交由它的子类来完成 2 抽象方法必须由子类来进行重写 3 只要包含一个抽象方法的抽象类, 该方法必须要定义成抽象类, 不管是 否还包含有其他方法 4 抽象类中可以包含具体的方法, 当然也可以不包含抽象方法 5 子类中的抽象方法不能与父类的抽象方法同名 6 abstract 不能与 final 并列修饰同一个类 7 abstract 不能与 private static final 或 native 并列修饰同一个方法 Email:18442056@qq.com http://www.kmdin.com 第 1 页, 共 9 页
三 案例 编写一个 java 抽象类, 源代码如下 : public abstract class Employee private String name; private String address; private int number; public Employee(String name, String address, int number) System.out.println("Constructing an Employee"); this.name = name; this.address = address; this.number = number; public double computepay() System.out.println("Inside Employee computepay"); return 0.0; public void mailcheck() System.out.println("Mailing a check to " + this.name + " " + this.address); public String tostring() return name + " " + address + " " + number; public String getname() return name; public String getaddress() return address; Email:18442056@qq.com http://www.kmdin.com 第 2 页, 共 9 页
public void setaddress(string newaddress) address = newaddress; public int getnumber() return number; 继承 Employee 类的源代码如下 : public class Salary extends Employee private double salary; //Annual salary public Salary(String name, String address, int number, double salary) super(name, address, number); setsalary(salary); public void mailcheck() System.out.println("Within mailcheck of Salary class "); System.out.println("Mailing check to " + getname() + " with salary " + salary); public double getsalary() return salary; public void setsalary(double newsalary) if(newsalary >= 0.0) salary = newsalary; public double computepay() System.out.println("Computing salary pay for " + getname()); Email:18442056@qq.com http://www.kmdin.com 第 3 页, 共 9 页
return salary/52; 不能实例化一个 Employee 类的对象, 但是如果我们实例化一个 Salary 类对 象, 该对象将从 Employee 类继承 7 个成员方法, 且通过该方法可以设置或获取三个成员变量 public class SalaryTest public static void main(string [] args) Salary objsalary = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00); Employee objemployee = new Salary("John Adams", "Boston, MA", 2, 2400.00); System.out.println("Call mailcheck using Salary reference "); objsalary.mailcheck(); System.out.println("\nCall mailcheck using Employee reference "); objemployee.mailcheck(); 编写一个 java 程序, 源代码如下 : public abstract class Shapes public int x, y; public int width, height; public Shapes(int x, int y, int width, int height) this.x = x; this.y = y; this.width = width; this.height = height; abstract double getarea(); abstract double getperimeter(); Email:18442056@qq.com http://www.kmdin.com 第 4 页, 共 9 页
public class Circle extends Shapes public double r; public double getarea() return (r * r * Math.PI); public double getperimeter() return (2 * Math.PI * r); public Circle(int x, int y, int width, int heigh) super(x, y, width, heigh); r = (double) width / 2.0; public class Square extends Shapes public double getarea() return (width * height); public double getperimeter() return (2 * width + 2 * height); public Square(int x, int y, int width, int height) super(x, y, width, height); public class Triangle extends Shapes public double c; public double getarea() return (0.5 * width * height); public double getperimeter() return (width + height + c); Email:18442056@qq.com http://www.kmdin.com 第 5 页, 共 9 页
public Triangle(int x, int y, int base, int height) super(x, y, base, height); c = Math.sqrt(width * width + height * height); import java.applet.applet; import java.awt.graphics; public class Test extends Applet Square Box = new Square(5, 15, 25, 25); Triangle tri = new Triangle(5, 50, 8, 4); Circle Oval = new Circle(5, 90, 25, 25); public void paint(graphics g) g.drawrect(box.x, Box.y, Box.width, Box.height); g.drawstring("box Area:" + Box.getArea(), 50, 35); g.drawstring("box Perimeter:" + Box.getPerimeter(), 50, 55); g.drawstring("tri Area:" + tri.getarea(), 50, 75); g.drawstring("tri Perimeter:" + tri.getperimeter(), 50, 95); g.drawoval(oval.x, Oval.y, Oval.width, Oval.height); g.drawstring("oval Area:" + Oval.getArea(), 50, 115); 编写一个 java 程序, 实现三种不同事物的行为, 具体要求如下 : 机器人 : 充电, 工作 ; 人 : 吃饭, 工作, 睡觉 ; 猪 : 进食, 睡觉 抽象行为类源代码如下 : public abstract class Action public static final int EAT = 1; public static final int SLEEP = 3; public static final int WORK = 5; public abstract void eat(); public abstract void sleep(); public abstract void work(); Email:18442056@qq.com http://www.kmdin.com 第 6 页, 共 9 页
public void commond(int flags) switch(flags) case EAT: this.eat(); case SLEEP: this.sleep(); case WORK: this.work(); case EAT + SLEEP: this.eat(); this.sleep(); case SLEEP + WORK: this.sleep(); this.work(); default: 机器人类源代码如下 : public class Robot extends Action public void eat() System.out.println(" 机器人充电 "); public void sleep() public void work() System.out.println(" 机器人工作 "); Email:18442056@qq.com http://www.kmdin.com 第 7 页, 共 9 页
人类的源代码如下 : public class Human extends Action public void eat() System.out.println(" 人吃饭 "); public void sleep() System.out.println(" 人睡觉 "); public void work() System.out.println(" 人工作 "); 猪类源代码如下 : public class Pig extends Action public void eat() System.out.println(" 猪进食 "); public void sleep() System.out.println(" 猪睡觉 "); public void work() 测试类代码如下 : public class AbstractTest Email:18442056@qq.com http://www.kmdin.com 第 8 页, 共 9 页
public static void main(string[] args) fun(new Robot()); fun(new Human()); fun(new Pig()); public static void fun(action act) act.commond(action.eat); act.commond(action.sleep); act.commond(action.work); Email:18442056@qq.com http://www.kmdin.com 第 9 页, 共 9 页