抽象类&&接口做形参(其实同理)

58 篇文章 0 订阅
  • 抽象类:传入该抽象类的子类对象

eg:

package javaBasic;

public class TestAbstract {
	public static void main(String[] args) {
		 FreshLad fl = new FreshLad();//想调ta的方法,要先new出来ta先呀~
		 Lad l = new Fool();//间接实现抽象类
		 fl.method(l);
	}
}

abstract class Lad{
	public abstract void need();
}

class FreshLad{
	public void method(Lad l) {
		l.need();
	}
}

class Fool extends Lad{
	public void need() {
		System.out.println("memorize more words\ncet-6 come on!");
	}
}

  • 接口:传入该接口的实现类对象
    eg如下:
    package javaBasic;
    
    public class TestAbstract {
    	public static void main(String[] args) {
    		 /*FreshLad fl = new FreshLad();
    		 Lad l = new Fool();//间接实现抽象类
    		 fl.method(l);*/
    		FreshYouth fy = new FreshYouth();
    		Youth y = new Idiot();
    		fy.method(y);
    	}
    }
    
    interface Youth{
    	abstract void struggle();
    }
    
    class FreshYouth{
    	public void method(Youth y) {
    		y.struggle();
    	}
    }
    
    class Idiot implements Youth{
    	public void struggle() {
    		System.out.println("study grammer well\ncet-6 fighting!!");
    	}
    }
    

     

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值