方法重写、Final关键字的使用

本文探讨了Java中的继承和方法覆盖,通过fruit1、apple和juice类展示如何创建类之间的继承关系,并演示了方法覆盖的实例。重点在于apple类如何重写父类的printMassage方法,以及juice类如何继承banana并实现自己的果汁特性和扩展功能。
摘要由CSDN通过智能技术生成
class fruit1{
	String color;
	String taste;
	void printmassage(String color1,String taste1) {
		System.out.println("水果的颜色:"+color1+"水果的味道是"+taste1);
	}   
}
class apple extends fruit1{
	final void printmassage(String color2,String taste2) {
		System.out.println("苹果的颜色是:"+color2+"\n"+"水果的味道是:"+taste2);
	}   //方法覆盖、断子绝孙
}    //方法继承
class banana extends fruit1{
	banana(String color3,String taste3) {
		System.out.println("香蕉的颜色是:"+color3+"\n"+"水果的味道是:"+taste3);
	}
	banana(){
		System.out.println("*************");
	}   //继承的传递性
}
final class juice extends banana{       
	juice(){
	    System.out.println("!!!!!!!!!!!!!");
	}   //继承的传递性
	juice(String color3,String taste3){
		super("黄色","甜的");
		System.out.println("香蕉汁的颜色是:"+color3+"\n"+"水果汁的味道是:"+taste3);
	}
}     //断子绝孙
public class fruit {
	public static void main(String args[]) {
	apple F1=new apple();
	F1.printmassage("红色","酸的");
	juice J1=new juice();
	juice J2=new juice("黄的","甜的");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值