第七周课程总结&实验报告(五)

实验四 类的继承

实验目的

理解抽象类与接口的使用;
了解包的作用,掌握包的设计方法。

实验要求

掌握使用抽象类的方法。
掌握使用系统接口的技术和创建自定义接口的方法。
了解 Java 系统包的结构。
掌握创建自定义包的方法。

实验内容

(一)抽象类的使用

设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2

2.编程技巧

(1) 抽象类定义的方法在具体类要实现;
(2) 使用抽象类的引用变量可引用子类的对象;
(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

代码:

package zy123;

abstract class shape{

 public  abstract double print();   
	}
	class sjx extends shape{
	    private double a;
	    private double b;
	    private double c;
	    public sjx(double a,double b,double c){
	        this.a=a;
	        this.b=b;
	        this.c=c;
	    }
	    public double print(){
	       if(a+b>c&&a+c>b&&b+c>a){
	            double p=(a+b+c)/2;
	            return Math.sqrt(p*(p-a)*(p-b)*(p-c));
	        }else{
	            System.out.println("无法构成三角形!");
	             return 0;
	        }
	    }
	}
	class Rectangle extends shape{
	    private double width;
	    private double height;
	    public Rectangle(double width,double height){
	        
	        this.width=width;
	        this.height=height;
	    }
	    public double print(){
	        return width*height;
	    }
	}
	class yuan extends shape{
	    double radius;
	    public yuan(double radius){
	        this.radius=radius;
	    }
	    public double print(){
	        return Math.PI*radius*radius;
	    }
	}

	public class test{
		public static void main(String[] args){
	      sjx s1=new sjx(18,9,3);
	        Rectangle s2=new Rectangle(3,4);
	        yuan s3=new yuan(9);
	        System.out.println("三角形的面积是:"+s1.print());
	        System.out.println("矩形的面积是:"+s2.print());
	        System.out.println("圆的面积是:"+s3.print());
	    }
	}

截图:

(二)使用接口技术

1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;
(2) 利用接口类型的变量可引用实现该接口的类创建的对象。
代码:

package zy123;


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());
    }
}
package zy123;

import java.util.Scanner;

public class test{
    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());
    }
}

截图:

学习总结:

学习的内容有 异常的处理 抽象类与接口 系统接口的技术和创建自定义接口的方法 抽象类定义的方法在具体类要实现;
使用抽象类的引用变量可引用子类的对象;
通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

try{
           //有可能出现异常的语句    
}catch(异常类  异常对象){
         //编写异常的处理语句
}[ catch(异常类 异常对象){
       //编写异常的处理语句
}catch(异常类 异常对象){
      //编写异常的处理语句
}...]
[finally{
一定会运行到的程序代码  ;
}]

posted on 2019-10-12 20:22  不负  阅读(148)  评论(0编辑  收藏  举报

导航