Java 抽象类和接口:Shape、Circle、Rectangle 示例

本示例演示了 Java 中抽象类和接口的使用,通过创建抽象类 Shape 和接口 AuthorInfo,以及其子类 Circle 和 Rectangle,并实现接口 AuthorInfo,展示了抽象类多态的应用。

1. 创建抽象类 Shape

package cn.edu.seig.zhangxiaohu;
//抽象类多态的应用示例

public abstract class Shape {							//Shape为抽象类
	public abstract String getTypeInfo();		//抽象方法,获取Shape的类型信息
	public abstract String getAreaInfo();		//抽象方法,获取面积信息
	public abstract String getPerimeterInfo();	//抽象方法,获取周长信息
}

2. 创建接口 AuthorInfo

package cn.edu.seig.zhangxiaohu;
public interface AuthorInfo  {	//接口,提供创建者信息
String getAuthorInfo();
}

3. 创建抽象类 Shape 的子类 Circle

package cn.edu.seig.zhangxiaohu;
public class Circle extends Shape implements AuthorInfo  {
//Circle类继承了Shape抽象类,实现AuthorInfo  接口
	private int radius;	//定义一个int类型的私有成员变量,命名为radius,表示圆半径
	public Circle(int r){
		this.radius = r; //利用形式参数 r初始化成员变量radius
	}
	//实现抽象方法 getTypeInfo()
	public String getTypeInfo(){
		return '类型:圆,半径:'+radius;
	}
//实现获取面积的抽象方法
	public String getAreaInfo() {
		return '面积:'+Math.PI*radius*radius;
	}
	//实现获取周长抽象方法
	public String getPerimeterInfo() {
		return '周长:'+2*Math.PI*radius;
	}	
	public String getAuthorInfo(){
		return '作者:张小虎'; //替换为自己的姓名,否则影响得分
	}
}

4. 创建抽象类 Shape 的子类 Rect

package cn.edu.seig.zhangxiaohu;
class Rect extends Shape implements AuthorInfo {	
//子类Rect类继承了Shape抽象类,实现AuthorInfo  接口
	private int length;	//定义一个int类型的私有成员变量,命名为length,表示矩形的长
	private int width;	//定义一个int类型的私有成员变量,命名为width,,表示矩形的宽

	public Rect(int l, int w){
		this.length = l; this.width = w; //利用形式参数 l、w分别初始化长、宽成员变量
	}
	//实现抽象方法 getTypeInfo()
	public String getTypeInfo(){
		return '类型:矩形,'+length+'x'+width;
	}
	public String getPerimeter(){
		return '周长:'+2*(length + width);
	}
}

5. 在主类的入口函数中添加测试代码

		Shape shapes[] = new Shape[6];
		Circle c1 = new Circle(50);  //实例化半径为10的圆
		Shape c2 = new Circle(100);  //实例化半径为20的圆
		AuthorInfo c3 = new Circle(200);  //实例化半径为30的圆

		Shape r1 = new Rect(100,100);
		Rect r2 = new Rect(180,160);
		AuthorInfo r3 = new Rect(200,240); 

         int pos = 0;
		shapes[pos++]=c1;
		shapes[pos++]=r1;
		shapes[pos++]=r2;
		shapes[pos++]=c2;
		shapes[pos++]=(Shape)c3;  //(17)_______________为什么此处和下一行需要强制类型转换
		shapes[pos++]=(Shape)r3;  

		pos = 1;
		for (Shape s : shapes ) {
			System.out.print('第'+pos+'个形状:'+s.getTypeInfo()+',');	
			System.out.print(((AuthorInfo)s).getAuthorInfo()+',');	
			System.out.print(s.getPerimeterInfo()+',');	
			System.out.print(s.getAreaInfo());
			System.out.println();	
			pos++;
	  	}

6. 运行程序并验证结果

解释 (17) 为什么需要强制类型转换

因为 c3r3 分别是 AuthorInfo 类型的对象,而 shapes 数组是 Shape 类型的。由于 CircleRect 实现了 AuthorInfo 接口,且继承了 Shape 类,所以需要将 AuthorInfo 类型的对象强制转换为 Shape 类型,才能放入 Shape 类型的数组中。

总结

通过该示例,我们可以了解到以下几点:

  • 抽象类是用来定义抽象方法和公共成员变量的,不能被实例化。
  • 接口是用来定义规范的,不能包含成员变量,只能包含抽象方法,且默认访问权限为 public
  • 非抽象类继承抽象类,必须实现全部的抽象方法。
  • 实现接口的类,必须实现全部的抽象方法,且不能减少访问权限。
  • 通过多态,我们可以使用相同类型的引用指向不同的子类对象,从而实现代码的复用和扩展。

希望本示例能帮助你更好地理解 Java 中抽象类和接口的概念和使用。

Java 抽象类和接口:Shape、Circle、Rectangle 示例

原文地址: https://www.cveoy.top/t/topic/nhDU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录