- class Test8
- {
- public static void main(String[] args)
- {
- //用了Student的匿名对象去调用listen方法
- //将Teacher类的匿名对象作为实际参数传递到listen方法中
- new Student().listen(new Teacher());
- }
- }
- class Teacher
- {
- String name = "张三";
- int age;
- char gender;
- public void teach(){
- System.out.println("讲课");
- }
- }
- class Student
- {
- String name;
- int age;
- char gender;
- public void listen(Teacher t){//这里形式参数指的是Teacher类的对象
- t.teach();//调用了Teacher类中的方法teach
- }
- }
复制代码
|
|