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

实验四 类的继承

实验目的

理解抽象类与接口的使用;
了解包的作用,掌握包的设计方法。
实验要求
掌握使用抽象类的方法。
掌握使用系统接口的技术和创建自定义接口的方法。
了解 Java 系统包的结构。
掌握创建自定义包的方法。

实验内容

(一)抽象类的使用

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

(1) 抽象类定义的方法在具体类要实现;

(2) 使用抽象类的引用变量可引用子类的对象;

(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

实现代码

public abstract class Shape {

	public abstract void area();

}


public class Trilateral extends Shape {
	

	private double a,b,c;
	private double p,s;
	
	public void area() {
		System.out.println(s);
		
	}
	public Trilateral(double a,double b,double c,double p) {
		this.a=a;
		this.b=b;
		this.c=c;
		this.p=(a+b+c)/2;
		this.s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
	}

}


public class Rectange extends Shape{
	
	
	private double h,w,s;
	
	public void area() {
		System.out.println(s);
		
	}
	public Rectange(double h,double w) {
		this.h =h;
		this.w=w;
		this.s=h*w;
	}

}
public class Circle extends Shape{
	private double r,s;
	public void area() {
		System.out.println(s);
	}
	public Circle(double r) {
		this.r=r;
		this.s=Math.pow(r,2)*Math.PI;
	}
	
}
import java.util.Scanner;
public class test {

	
	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		System.out.println("输出三角形三条边长:");
		double a=sc.nextDouble();
		double b=sc.nextDouble();
		double c=sc.nextDouble();
		Trilateral trilateral=new Trilateral(a,b,c,(a+b+c)/2);
		System.out.println("三角形面积为:");
		trilateral.area();
		
		
		System.out.println("输入矩形高和宽:");
		double h=sc.nextDouble();
		double w=sc.nextDouble();
		System.out.println("矩形面积为:");
		Rectange rectange=new Rectange(h,w);
		rectange.area();
		
		
		System.out.println("输入圆的半径:");
		double r=sc.nextDouble();
		System.out.println("圆面积为:");
		Circle circle=new Circle(r);
		circle.area();
		
	}

}



实验截图

这里不知道为什么显示错误,但又没找到且运行没问题

(二)使用接口技术

1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。

编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;

(2) 利用接口类型的变量可引用实现该接口的类创建的对象。

实验代码


interface Shape {

	public abstract void size();

}
public class Circle implements Shape{
	private double r,s;
	public void size() {
		System.out.println(s);
	}
	public Circle(double r) {
		this.r=r;
		this.s=Math.pow(r,2)*Math.PI;
	}
	
}

public class Line implements Shape{
	
	private double l;
	public void size() {
		System.out.println(l);
		
	}
	public Line(double l) {
		this.l=l;
	}

}
import java.util.Scanner;
public class test {

	
	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		System.out.println("输入直线大小:");
		double l=sc.nextDouble();
		
		Line line=new Line(l);
		System.out.println("直线大小为:");
		line.size();
		
		
		
		
		
		System.out.println("输入圆的半径:");
		double r=sc.nextDouble();
		System.out.println("圆面积为:");
		Circle circle=new Circle(r);
		circle.size();
		
	}

}


总结

接口

接口用Interface表示
接口不是类,类描述对象的属性和方法。接口则包含类要实现的方法。
接口不能实例化对象,所有的方法必须是抽象方法。
接口支持多继承。
当类实现接口的时候,类要实现接口中所有的方法。否则,类必须声明为抽象的类。

原文地址:https://www.cnblogs.com/hy14157/p/11655340.html