1. 具体类:实际参数传递应该创建当前具体类对象
class Student{
public void study(){
System.out.println("好好学习,天天向上");
}
class StudentDemo{
public void method(Student student){
student.study() ;
}
}
//测试类
public class ArgsDemo1 {
public static void main(String[] args) {
StudentDemo sd = new StudentDemo() ;
Student s = new Student() ;
sd.method(s);
}
}
2.抽象类:实际参数需要传递应该创建当前抽象类的子类对象(抽象类多态)
//抽象类
abstract class Person{
public abstract void work() ;
}
//抽象类的子类
class Worker extends Person{
@Override
public void work() {
System.out.println("好好学习,天天向上");
}
}
class PersonDemo{
public void show(Person person){
person.work() ;
}
}
//测试类
public class ArgsDemo2 {
public static void main(String[] args) {
PersonDemo pd = new PersonDemo() ;
Person p = new Worker() ;
pd.show(p);
pd.show(new Worker());
}
3.接口
//接口
interface Study{
void study() ;
}
class StudyDemo{
public void function(Study study){
study.study();
}
}
//定义除接口的子实现类
class StudyImpl implements Study{
@Override
public void study() {
System.out.println("好好学习,天天向上");
}
}
//测试类
public class ArgsDemo3 {
public static void main(String[] args) {
StudyDemo ld = new StudyDemo() ;
//接口多态
Study s = new StudyImpl() ;
ld.function(s);
}
}