创建一个水果类Fruit,并将它声明为抽象类。

本文介绍了如何在Java中创建一个抽象类Fruit,包含color属性和抽象方法harvest。随后,创建了Apple和Orange子类继承Fruit并实现了harvest方法,最后在test类中测试了这两个类的实例。
摘要由CSDN通过智能技术生成

创建一个水果类Fruit,并将它声明为抽象类。类中定义一个color属性,表示水果的颜色;定义一个构造方法给color属性赋值;再定义一个抽象方法harvest(),表示收获。然后定义两个类Apple和Orange分别继承Fruit,并在这两个类中实现harvest()方法。在测试类中,分别创建这两个类的实例对象,测试调用该方法。

先创建一个Fruit类

abstract class Fruit {
	  String color;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	 public void color(String color) {
		 this.color=color;
	 }
	 abstract void harvvest();
	  

}

创建Apple类

public class apple extends Fruit{
	public apple(String color) {
		super();
	}


	@Override
	void harvvest() {
		System.out.println("苹果已经收获");
		
	}
	

}

创建Orange类

public class orange extends Fruit{
	public orange(String color) {
		super();
	}

	 void harvvest() {
		System.out.println("橘子已经收获");
		
	}
	

}

创建一个test类

public class test1 {
	public static void main(String[] args) {
		apple a=new apple("红彤彤的"); 
		 
		a.harvvest();
		 
		orange o=new orange("黄橙橙的");
		o.harvvest();
	}

}

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值