添加jar包org.apache.shiro:shiro-core:1.3.2
SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());
这个对象里的成员变量 cachedBase64是什么时候被赋值的。
问一个问题 ,关于shiro的?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 一枚小爪哇 2021-07-02 14:33关注
- 执行SimpleByteSource simpleByteSource = new SimpleByteSource("c6c6ff38f906f7822070edc2fe2eff7c".getBytes());会默认执行toString()方法,因为SimpleByteSource类中重写了toString方法:会执行this.toBase64();,因此会调用toBase64()方法,因为是默认调用,所以打断点是不会跳进去的;
- 为什么new一个对象会默认执行toString方法,因为所有类的基类都是Object,即都是继承了Object对象,创建对象时就自动调用xx的toString()方法。
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
如果不重写toString()方法:会得到输出:xxxx@xxxxxxx的类名加地址形式;
如果重写toString()方法,则输出重写后的值。
SimpleByteSource类中重写了toString()方法:public String toString() { return this.toBase64(); } public int hashCode() { return this.bytes != null && this.bytes.length != 0 ? Arrays.hashCode(this.bytes) : 0; } public boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof ByteSource) { ByteSource bs = (ByteSource)o; return Arrays.equals(this.getBytes(), bs.getBytes()); } else { return false; } }
public String toBase64() { if (this.cachedBase64 == null) { this.cachedBase64 = Base64.encodeToString(this.getBytes()); } return this.cachedBase64; }
- 举个例子:
重写一个类SimpleByteSourceTest,该类里面的成员变量与方法都与SimpleByteSource相同,不同的是SimpleByteSourceTest类不重写toString()方法。看一下运行结果:
SimpleByteSourceTest代码如下:
public class SimpleByteSourceTest implements ByteSource { private final byte[] bytes; private String cachedHex; private String cachedBase64; public SimpleByteSourceTest(byte[] bytes) { this.bytes = bytes; } public SimpleByteSourceTest(char[] chars) { this.bytes = CodecSupport.toBytes(chars); } public SimpleByteSourceTest(String string) { this.bytes = CodecSupport.toBytes(string); } public SimpleByteSourceTest(ByteSource source) { this.bytes = source.getBytes(); } public SimpleByteSourceTest(File file) { this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(file); } public SimpleByteSourceTest(InputStream stream) { this.bytes = (new SimpleByteSourceTest.BytesHelper()).getBytes(stream); } public static boolean isCompatible(Object o) { return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream; } @Override public byte[] getBytes() { return this.bytes; } @Override public boolean isEmpty() { return this.bytes == null || this.bytes.length == 0; } @Override public String toHex() { if (this.cachedHex == null) { this.cachedHex = Hex.encodeToString(this.getBytes()); } return this.cachedHex; } @Override public String toBase64() { if (this.cachedBase64 == null) { this.cachedBase64 = Base64.encodeToString(this.getBytes()); } return this.cachedBase64; } private static final class BytesHelper extends CodecSupport { private BytesHelper() { } public byte[] getBytes(File file) { return this.toBytes(file); } public byte[] getBytes(InputStream stream) { return this.toBytes(stream); } } }
运行结果图:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效