我向在大学学习这门课程的学生讲授Java编程语言的基础知识.
今天其中一个让我对她的问题感到困惑,所以我告诉她给我一天的时间来思考这个问题,我会尽可能准确地给她答案.
她告诉我,当她instanceof在考试中使用关键词时,老师非常生气.
此外,她说老师说没有办法证明如果使用这个词,多态性是如何起作用的.
我想了很多,试图找到一种方法来证明在某些情况下我们需要使用instanceof,而且即使我们使用它,在这种方法中仍然存在一些多态性.
所以这是我做的例子:
public interface Animal
{
public void talk();
}
class Dog implements Animal {
public void talk() {
System.out.println("Woof!");
}
}
public class Cat implements Animal
{
public void talk() {
System.out.println("Meow!");
}
public void climbToATree() {
System.out.println("Hop, the cat just cimbed to the tree");
}
}
class Hippopotamus implements Animal {
public void talk() {
System.out.println("Roar!");
}
}
public class Main {
public static void main(String[] args) {
//APPROACH 1 …Run Code Online (Sandbox Code Playgroud) 我有一个解析器,这个构造大约有数万次:
if (tokens.first() instanceof CommaToken) {
tokens.consume();
Run Code Online (Sandbox Code Playgroud)
我想知道如何做到这一点:
if (match(CommaToken)) { ... blah ... }
private boolean match(??? tokenType) {
if (tokens.first() instanceof tokenType) { ... blah ... }
}
Run Code Online (Sandbox Code Playgroud)
我有一个wetware故障,无法弄清楚方法中的tokenType类.另一个问题是Java将'tokenType'视为文字.那是:
instanceof tokenType
Run Code Online (Sandbox Code Playgroud)
看起来就像
instanceof CommaToken
Run Code Online (Sandbox Code Playgroud)
关于语法.
有任何想法吗?
在我的应用程序的路由部分,我有类路由和路由器,路由只保存有关特定映射路由的信息以及设置和获取这些信息的方法.我试图像这样传递Closure,
Router::map('/', function(){}, array());
Run Code Online (Sandbox Code Playgroud)
我存储第二个参数(Closure),就像这样
$route = new Route();
$route->setTarget($target);
Run Code Online (Sandbox Code Playgroud)
*第二个参数是$ target变量
当我试试这个
$target = $route->getTarget();
if($target instanceof Closure)
{
echo 1;
}else
{
echo 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印0,但当我尝试print_r($target),我得到
Closure Object ( )
Run Code Online (Sandbox Code Playgroud)
班级路线
class Route {
/**
* URL of this Route
* @var string
*/
private $url;
/**
* Accepted HTTP methods for this route
* @var array
*/
private $methods = array('GET','POST','PUT','DELETE');
/**
* Target for this route, can be anything.
* @var mixed
*/
private …Run Code Online (Sandbox Code Playgroud) 什么意思'动态等价'?
我只是想知道this.getClass().isInstance(aClass)取代的目的是this instanceof aClass什么?有区别吗?
确定指定的Object是否与此Class表示的对象分配兼容.此方法是Java语言instanceof运算符的动态等效项
已经反复说过,除了equals()方法之外,不应该使用instanceof运算符,否则它是一个糟糕的OOP设计.
有人写道,这是一个繁重的操作,但似乎,至少java,处理得相当好(甚至比Object.toString()比较更有效).
有人可以解释,或指导我一些文章,解释为什么这是一个糟糕的设计?
考虑一下:
Class Man{
doThingsWithAnimals(List<Animal> animals){
for(Animal animal : animals){
if(animal instanceOf Fish){
eatIt(animal);
}
else if(animal instanceof Dog){
playWithIt(animal);
}
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
决定如何处理动物,取决于男人.男人的欲望也可能偶尔改变,决定吃狗,玩鱼,而动物不会改变.
如果您认为instanceof运算符不是正确的OOP设计,请告诉您如何在没有instanceof的情况下执行此操作,为什么?
谢谢.
public class Parent {
....
}
public class Child1 extends Parent {
....
public void foo() {
....
}
}
public class Child2 extends Parent {
....
public void foo() {
....
}
}
Run Code Online (Sandbox Code Playgroud)
这里的方法foo()只存在于Child类中,不能添加到Parent类(甚至不是抽象方法).在这种情况下,当我想调用类的引用的foo()方法时obj,Parent我需要使用intanceof多个if..else我想避免的方法.
Parent obj = ...// Object of one of the child classes
obj.foo();
Run Code Online (Sandbox Code Playgroud)
编辑:我需要使用类型obj为Parent唯一的.否则我将无法调用父类中存在的obj上的方法.
我的解决方案:我想的方法是定义一个接口,FooInterface用foo()方法说,让所有子类实现它,然后我可以输入强制转换obj到该接口并调用foo()方法,如下所示:
if(obj instanceof FooInterface){
((FooInterface)obj).foo();
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?或者对这个有什么改进?
我在 angular2 工作,很想知道<div>当变量是某种数据类型时我是否可以使用 ngSwitch 加载标签。
<div [ng-switch]="value">
<p *ng-switch-when="isObject(value)">This is Object</p>
<p *ng-switch-when="isArray(value)">This is Array</p>
<p *ng-switch-when="isBoolean(value)">This is Boolean</p>
<p *ng-switch-when="isNumber(value)">This is Number</p>
<p *ng-switch-default>This is Simple Text !</p>
</div>
Run Code Online (Sandbox Code Playgroud)
div当变量是某种数据类型时,这是否可以加载标签?如果没有,有什么解决方法吗?
有人可以解释我为什么error instanceof CustomError下面的代码部分是false?
class CustomError extends Error {}
const error = new CustomError();
console.log(error instanceof Error); // true
console.log(error instanceof CustomError); // false ???
class ParentClass {}
class ChildClass extends ParentClass { }
const child = new ChildClass();
console.log(child instanceof ParentClass); // true
console.log(child instanceof ChildClass); // true
Run Code Online (Sandbox Code Playgroud)
Error 对象有什么特别之处吗?我想制作我自己可以检查的错误类型。
顺便说一句,我已经在最新的TypeScript Playground上检查了上面的代码
我正在寻找一种使用通用函数优化代码库中某些内容的方法。有一个带有返回类型的函数,该函数List<Object>可能具有return type List<SpecifiedType>。
贝娄是该函数的简约版本function。它采用参数类型,并基于它调用相应的函数(在此限于String)或泛型函数。
public static ArrayList<String> forString(){
ArrayList<String> res = new ArrayList<>();
// Fetching and processing data specific to String
return res;
}
public static <T> ArrayList<T> forGeneric(Class<T> type){
ArrayList<T> res = new ArrayList<>();
// Fetching data
return res;
}
public static <T> ArrayList<T> function(Class<T> type){
if(type == String.class)
return (ArrayList<T>) forString();
return forGeneric(type);
}
Run Code Online (Sandbox Code Playgroud)
上面的函数的目标是这样调用的: ArrayList<SomeType> someTypes = function(SomeType.class);
关于以上代码,我注意到了两件事:
投射到ArrayList<T>尽管我们知道,如果类型需要String被作为参数传递它将返回ArrayList<String>就像forString()方法
演员要ArrayList<T> …
我正在用 TS 建立一个图书馆。该库使用该ssh2库作为依赖项。
我正在尝试创建一个函数,该函数可以接受ssh2配置对象或已经存在的Client实例来执行命令(这是一个简化的情况):
import { Client, ConnectConfig } from 'ssh2';
export function runCommand(params: Client | ConnectConfig) {
if(params instanceof Client) {
// do something
} else {
// create our own client
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
当我构建这个库并像这样调用函数时:
import { Client, ConnectConfig } from 'ssh2';
export function runCommand(params: Client | ConnectConfig) {
if(params instanceof Client) {
// do something
} else {
// create our own client
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
我的函数中的检查将由于某种原因 …
instanceof ×10
java ×5
oop ×3
typescript ×3
class ×2
angular ×1
class-design ×1
closures ×1
generics ×1
inheritance ×1
javascript ×1
ng-switch ×1
node.js ×1
php ×1
polymorphism ×1
routing ×1
typeof ×1
types ×1