内部类的向上转型以及隐藏实现

内部类的实现的接口

package sky.java.innerclass1.inner1;

public interface Ainter {
    void hello();
}
package sky.java.innerclass1.inner1;

public interface Binter {
   void say();
}

内部类的外部类

package sky.java.innerclass1.inner1;

public class Ha {
    private class AChild implements Ainter {
        @Override
        public void hello() {
            System.out.println("hello");
        }
    }
    AChild aChild() {
        return new AChild();
    }
    private class BChild implements Binter {
        @Override
        public void say() {
            System.out.println("say");
        }
    }
    BChild bChild() {
        return new BChild();
    }
}

调用测试

package sky.java.innerclass1.inner1;

import java.math.BigInteger;

public class Demo {

    public static void main(String[] args) {

        Ha ha = new Ha();
        Ainter ainter = ha.aChild();
        Binter binter = ha.bChild();
        ainter.hello();
        binter.say();
    }
}

总结 :
1. 通过在类的内部 , 使用其接口实现 , 处理真正逻辑 , 并将其私有.
2. 并将该对象以接口的形式返回供其他应用使用.
3. 可对外隐藏其内部实现
4. 返回结果进行功能操作 , 更易于扩展与解耦
5. 非私有内部类其实就是一个普通java类 , 可以直接创建使用

package sky.java.innerclass1;

public class Outer {

    public void f() {
        System.out.println("f....");
    }

    public class Inner {
        public Outer outer() {
            return Outer.this;
        }
    }
    public Inner inner() {
        return new Inner();
    }

    /**
     * 总结: 通过外部类创建内部类对象 , 格式为 : Outer.Inner inner1 = outer.new Inner();
     *      通过内部类 , 获取外部类对象 , 格式为 : Outer.this;
     */
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.inner();
        inner.outer().f();
        // 获取内部对象实例
        Outer.Inner inner1 = outer.new Inner();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答你的问题!这是一个非常好的Java编程问题。 首先,创建私有内部类的语法如下: ``` class OuterClass { private class InnerClass implements PublicInterface { // Inner class implementation } public PublicInterface createObject() { return new InnerClass(); } } ``` 然后,我们可以在外部类中使用 `createObject()` 方法来获取对私有内部类的引用。由于 `InnerClass` 是私有的,它不能在外部类之外直接访问,因此需要通过公共接口向上转型。 下面是示例代码: ``` public interface PublicInterface { // Interface methods } class OuterClass { private class InnerClass implements PublicInterface { public void innerMethod() { System.out.println("Inner class method called"); } } public PublicInterface createObject() { return new InnerClass(); } } public class Main { public static void main(String[] args) { OuterClass outer = new OuterClass(); PublicInterface obj = outer.createObject(); // Upcasting done here // Try to downcast to InnerClass // This will fail at runtime as InnerClass is completely hidden InnerClass inner = (InnerClass) obj; // This line will cause the program to throw a ClassCastException } } ``` 我们在 `Main` 类中尝试向下转型 `PublicInterface` 对象为 `InnerClass`,但是由于 `InnerClass` 完全被隐藏了,所以会在运行时抛出 `ClassCastException` 异常。 另外,当你问我前面对你说了什么时,我想给你讲一个笑话: 有两个人在大街上说话,其中一个明显在向另一个炫耀自己的声音有多响。忽然,一个路过的母牛猛然大叫,你猜谁响声更大?答案是母牛,因为它没有说话,只是自然地发出了声音。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值