实验四 类的继承
实验目的
理解抽象类与接口的使用;
了解包的作用, 掌握包的设计方法.
实验要求
掌握使用抽象类的方法.
掌握使用系统接口的技术和创建自定义接口的方法.
了解 Java 系统包的结构.
掌握创建自定义包的方法.
实验内容
(一) 抽象类的使用
1, 设计一个类层次, 定义一个抽象类 -- 形状, 其中包括有求形状的面积的抽象方法. 继承该抽象类定义三角型, 矩形, 圆. 分别创建一个三角形, 矩形, 圆存对象, 将各类图形的面积输出.
注: 三角形面积 s=sqrt(p(p-a)(p-b)*(p-c)) 其中, a,b,c 为三条边, p=(a+b+c)/2
2, 编程技巧
(1) 抽象类定义的方法在具体类要实现;
(2) 使用抽象类的引用变量可引用子类的对象;
(3) 通过父类引用子类对象, 通过该引用访问对象方法时实际用的是子类的方法. 可将所有对象存入到父类定义的数组中.
源代码
- package hello;
- import java.util.Scanner;
- abstract class Shape {
- public abstract double Area();
- }
- class Triangle extends Shape{
- private int a,b,c;
- public Triangle() {}
- public Triangle(int a,int b,int c){
- this.a = a;
- this.b = b;
- this.c = c;
- }
- public int getA() {
- return a;
- }
- public void setA(int a) {
- this.a = a;
- }
- public int getB() {
- return b;
- }
- public void setB(int b) {
- this.b = b;
- }
- public int getC() {
- return c;
- }
- public void setC(int c) {
- this.c = c;
- }
- public double Area() {
- double p = (a+b+c)/2;
- return Math.sqrt(p*(p-getA())*(p-getB())*(p-getC()));
- }
- }
- class Rectangle extends Shape{
- private double height,width;
- public Rectangle() {}
- public Rectangle(double height,double width){
- this.height = height;
- this.width = width;
- }
- public double getHeight() {
- return height;
- }
- public void setHeight(double height) {
- this.height = height;
- }
- public double getWidth() {
- return width;
- }
- public void setWidth(double width) {
- this.width = width;
- }
- public double Area() {
- return getHeight()*getWidth();
- }
- }
- class Circle extends Shape{
- private double radius;
- public Circle(){}
- public Circle(double radius) {
- this.radius = radius;
- }
- public double getRadius() {
- return radius;
- }
- public void setRadius(double radius) {
- this.radius = radius;
- }
- public double Area() {
- return Math.PI*Math.pow(getRadius(), 2);
- }
- }
- public class Demo2{
- public static void main(String[] args) {
- System.out.println("请输入三角形的边长:");
- Scanner s1 = new Scanner(System.in);
- int a = s1.nextInt();
- int b = s1.nextInt();
- int c = s1.nextInt();
- Triangle triangle = new Triangle(a,b,c);
- if(a+b>c && a+c>b && b+c>a) {
- System.out.println("三角形的面积为:"+triangle.Area());
- }
- else {
- System.out.println("这不是一个三角形, 无法输出面积.");
- }
- Scanner s2 = new Scanner(System.in);
- System.out.println("请输入矩形的长和宽:");
- double height = s2.nextDouble();
- double width = s2.nextDouble();
- Rectangle rectangle = new Rectangle(height,width);
- System.out.println("矩形的面积为:"+rectangle.Area());
- Scanner s3 = new Scanner(System.in);
- System.out.println("请输入圆的半径:");
- double radius = s3.nextDouble();
- Circle circle = new Circle(radius);
- System.out.println("圆的面积为:"+circle.Area());
- }
- }
运行截图
(二) 使用接口技术
1, 定义接口 Shape, 其中包括一个方法 size(), 设计 "直线","圆", 类实现 Shape 接口. 分别创建一个 "直线","圆" 对象, 将各类图形的大小输出.
2, 编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;
(2) 利用接口类型的变量可引用实现该接口的类创建的对象.
源代码
package hello; import java.util.Scanner; interface Shape{ public void Size(); } class Line implements Shape{ private double length; public Line() {} public Line(double length) { this.setLength(length); } @Override public void Size() { System.out.println("直线的长度:"+getLength()); } public double getLength() { return length; } public void setLength(double length) { this.length = length; } } class Circle implements Shape{ private double radius; public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return Math.PI*Math.pow(radius, 2); } @Override public void Size() { System.out.println("圆的面积:"+getArea()); } } public class Demo1{ public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("请输入直线的长度:"); int length = s.nextInt(); Line line = new Line(length); System.out.println("直线长度为:"+line.getLength()); Scanner p = new Scanner(System.in); System.out.println("请输入圆的半径:"); double radius = p.nextDouble(); Circle circle = new Circle(radius); System.out.println("圆的面积为:"+circle.getArea()); } }
运行截图
学习总结
通过以上两道题区分抽象类和接口
比较 | 抽象类 | 接口
-|-|-
实现方法 | 子类使用 extends 关键字来继承抽象类, 如果子类不是抽象类, 子类需要提供抽象类中所有声明方法的实现 | 子类使用 implements 来实现接口, 需要提供接口中所有声明的实现 |
和正常类区别 | 抽象类不能被实例化 | 接口则是完全不同的类型 |
访问修饰符 | 抽象方法可以有 public,protected 和 default 等修饰 | 接口默认是 public, 不能使用其他修饰符 |
多继承 | 一个子类只能存在一个父类 | 一个子类可以存在多个接口 |
添加新方法 | 向抽象类中添加新方法, 可以提供默认的实现, 因此可以不修改子类现有的代码 | 如果往接口中添加新方法, 则子类中需要实现该方法.|
来源: http://www.bubuko.com/infodetail-3235936.html