1 Star 0 Fork 18

Gui/力扣题库(完整版)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
check-if-object-instance-of-class.html 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
小墨 提交于 2023-04-14 14:39 +08:00 . update
<p>Write a function that checks if a given object is an instance of a given class or superclass.</p>
<p>There are&nbsp;no constraints on the data types that can be passed to the function.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> func = () =&gt; checkIfInstance(new Date(), Date)
<strong>Output:</strong> true
<strong>Explanation: </strong>The object returned by the Date constructor is, by definition, an instance of Date.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> func = () =&gt; { class Animal {}; class Dog extends Animal {}; return checkIfInstance(new Dog(), Animal); }
<strong>Output:</strong> true
<strong>Explanation:</strong>
class Animal {};
class Dog extends Animal {};
checkIfInstance(new Dog(), Animal); // true
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> func = () =&gt; checkIfInstance(Date, Date)
<strong>Output:</strong> false
<strong>Explanation: </strong>A date constructor cannot logically be an instance of itself.
</pre>
<p><strong class="example">Example 4:</strong></p>
<pre>
<strong>Input:</strong> func = () =&gt; checkIfInstance(5, Number)
<strong>Output:</strong> true
<strong>Explanation: </strong>5 is a Number. Note that the &quot;instanceof&quot; keyword would return false.
</pre>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/guihaowe/leetcode-problemset.git
git@gitee.com:guihaowe/leetcode-problemset.git
guihaowe
leetcode-problemset
力扣题库(完整版)
master

搜索帮助