void和Void有何区别?

总结

 特点
void用于无返回值的方法定义
Void
  • void的包装方法
  • Void不能被实例化
  • 可用于一直返回null的方法或者返回null的泛型

 

 

 

 

 

 

剖析
public final  ---无法继承
class Void {

    /**
     * The {@code Class} object representing the pseudo-type corresponding to
     * the keyword {@code void}.
     */
    @SuppressWarnings("unchecked")
    public static final Class<Void> TYPE = (Class<Void>) Class.getPrimitiveClass("void");

    /*
     * The Void class cannot be instantiated.
     */
    private Void() {} ---不能实例化
}

样例
package com.bat.example;

public class TestVoid {
    public static void main(String[] args) {
        test1();

        Void v1 = test2();
        System.out.println(v1);

        TestDao<Void> testDao = new TestDao<>();
        Void v2 = testDao.get();
        System.out.println(v2);

    }

    public static void test1(){

    }

    public static Void test2(){
        return null;
    }

    static class TestDao<T> {
        public T get(){
            return null;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
装饰器模式和代理模式是两种结构型设计模式,它们之间的区别如下: 1. 装饰器模式是在不改变原有对象结构的情况下,动态地给对象添加新的行为。也就是说,装饰器模式关注于对对象的功能扩展,而不是对象本身。 2. 代理模式则是为了控制对对象的访问,通常是因为对象具有重要的敏感信息或功能。代理对象可以在调用原始对象之前或之后执行一些操作,这种方式可以提高系统的安全性、稳定性和灵活性。 下面分别给出装饰器模式和代理模式的代码示例: 装饰器模式示例: ```java // 定义一个抽象组件 interface Component { void operation(); } // 具体组件类 class ConcreteComponent implements Component { @Override public void operation() { System.out.println("ConcreteComponent operation..."); } } // 抽象装饰器类 abstract class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { if (component != null) { component.operation(); } } } // 具体装饰器类A class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } @Override public void operation() { super.operation(); addBehaviorA(); } private void addBehaviorA() { System.out.println("ConcreteDecoratorA added behavior A..."); } } // 具体装饰器类B class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } @Override public void operation() { super.operation(); addBehaviorB(); } private void addBehaviorB() { System.out.println("ConcreteDecoratorB added behavior B..."); } } // 客户端代码 public class Client { public static void main(String[] args) { Component component = new ConcreteComponent(); Component decorator1 = new ConcreteDecoratorA(component); Component decorator2 = new ConcreteDecoratorB(decorator1); decorator2.operation(); } } ``` 代理模式示例: ```java // 抽象主题类 interface Subject { void request(); } // 具体主题类 class ConcreteSubject implements Subject { @Override public void request() { System.out.println("ConcreteSubject request..."); } } // 代理类 class ProxySubject implements Subject { private Subject subject; public ProxySubject(Subject subject) { this.subject = subject; } @Override public void request() { beforeRequest(); subject.request(); afterRequest(); } private void beforeRequest() { System.out.println("ProxySubject before request..."); } private void afterRequest() { System.out.println("ProxySubject after request..."); } } // 客户端代码 public class Client { public static void main(String[] args) { Subject realSubject = new ConcreteSubject(); Subject proxySubject = new ProxySubject(realSubject); proxySubject.request(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值