滴水穿石-03一道面试题引发的思考
按要求,补齐代码.要求在控制台输出"HelloWorld";
interface Inter {void show();}
class Outer {//补齐代码}
class OuterDemo{
public static void main(String[] args){
Outer.method().show();
}
}
1 链式编程
看到Outer.method().show();这行代码首先想到的是链式编程,即:方法调用后返回一个对象,对象接着可以继续调方法
1.1 链式编程例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 链式编程 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StudentDemo s = new StudentDemo(); s.getStudent().SayLevel(); } } class StudentDemo { public Student getStudent(){ return new Student(); } } class Student{ public void SayLevel(){ System.out.print("简单级别"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class 链式编程 { public static void main(String[] args) { // TODO Auto-generated method stub Student s = new Student(); s.setName("张三").setAge(20).SayLevel(); } } class Student{ private String name; private int age; public Student setName(String name){ this.name = name; return this; } public Student setAge(int age){ this.age = age; return this; } public void SayLevel(){ System.out.println("简单级别"); System.out.println("我叫"+this.name+"年龄: "+this.age); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 链式编程 { public static void main(String[] args) { // TODO Auto-generated method stub Student.builder().setName("张三").setAge(20).SayLevel(); } } class Student{ public static Builder builder(){ return new Builder(); } static class Builder{ private String name; private int age; public Builder setName(String name){ this.name = name; return this; } public Builder setAge(int age){ this.age = age; return this; } public void SayLevel(){ System.out.println("简单级别"); System.out.println("我叫"+this.name+"年龄: "+this.age); } } }
1.1.1 内部类:在类的内部定义的类
1.1.1.1 成员内部类:在成员位置定义的类被称为“成员内部类”
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 成员内部类 { public static void main(String [] args){ //访问方式 //外部类名.内部类名 对象名 = 外部类对象.内部对象 Outer.Inner ot = new Outer().new Inner(); ot.show(); } } class Outer{ private int num =10; //成员位置 class Inner{ public void show(){ System.out.print(num); } } }
1.1.1.2 方法内部类:在局部位置定义的类被称为“局部内部类”;这道题的答案也涉及到了局部内部类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class 局部内部类 { public static void main(String[] args){ Outer_a o = new Outer_a(); o.method(); } } class Outer_a{ private int num =10; public void method(){ class Inner{ public void show(){ System.out.print(num); } } Inner i = new Inner(); i.show(); } }
1.1.2 匿名内部类;其本质为对象(类的子类,实现了接口的类)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 匿名内部类 { public static void main(String[] args) { //格式: new 类名或者接口名(){重写方法;} Outer_b o = new Outer_b(); o.method(); } } interface Inter2{ public abstract void show(); } class Outer_b{ /** * */ public void method(){ new Inter2(){ public void show(){ System.out.print("Hello,World"); } }.show(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 匿名内部类 { public static void main(String[] args) { //格式: new 类名或者接口名(){重写方法;} Outer_b o = new Outer_b(); o.method(); } } interface Inter2{ public abstract void show(); public abstract void show2(); } class Outer_b{ public void method(){ Inter2 i = new Inter2(){ public void show(){ System.out.println("Hello,World"); } public void show2(){ System.out.print("Hello,World2"); } } ; i.show(); i.show2(); } }
2 自此,我们可以写出
但是return Inter;不能直接返回接口,而是返回接口的实现类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.DSCS_03; public class 面试题 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //01 根据此句是链式编程,可以推出返回值为Inter //02 由于通过类名.方法名直接调用可以退出时静态方法 Outer2.method().show(); } } interface Inter { //注意:这里默认为 public abstract void show(); } class Outer2 { public static Inter method(){ //匿名内部类 return new Inter(){ //由于继承类的访问权限不能比接口的低 public void show(){ System.out.print("HelloWorld"); } }; } }