小编Sin*_*hil的帖子

java中的显式类型转换示例

我在http://www.javabeginner.com/learn-java/java-object-typecasting上遇到过这个例子,在谈到显式类型转换的部分中,有一个例子让我感到困惑.

这个例子:

class Vehicle {

    String name;
    Vehicle() {
        name = "Vehicle";
    }
}

class HeavyVehicle extends Vehicle {

    HeavyVehicle() {
        name = "HeavyVehicle";
    }
}

class Truck extends HeavyVehicle {

    Truck() {
        name = "Truck";
    }
}

class LightVehicle extends Vehicle {

    LightVehicle() {
        name = "LightVehicle";
    }
}

public class InstanceOfExample {

    static boolean result;
    static HeavyVehicle hV = new HeavyVehicle();
    static Truck T = new Truck();
    static HeavyVehicle hv2 = null;
    public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java casting downcast

31
推荐指数
2
解决办法
8万
查看次数

JavaScript将字符串添加到数字中

我正在阅读MDN上的JavaScript重新介绍,在Numbers部分中,它表示只需在前面添加一个加号运算符即可将字符串转换为数字.

例如:

+"42"将产生42的数字输出.

但是在关于运算符的部分中,它说通过向任何数字添加字符串"something",您可以将该数字转换为字符串.他们还提供了以下让我困惑的例子:

"3"+ 4 + 5可能会在输出中产生345的字符串,因为数字4和5也将转换为字符串.

但是,3 + 4 +"5"不会产生12的数字,而不是如他们的例子中所述的字符串75吗?

在第二个例子中,关于运算符的部分不会站在字符串"5"前面的+运算符将该字符串转换为数字5,然后将所有内容添加到等于12?

javascript string numbers operators

28
推荐指数
2
解决办法
6万
查看次数

TypeError:node.setAttribute不是函数

我收到一个错误:当我尝试在我的网页上调用以下函数时,"TypeError:item.setAttribute不是函数":

function list() {
    var colors = document.getElementById('colors');
    var colors = colors.childNodes.length;
    for (var i = 1; i < broj; i++) {
        var item = colors.childNodes.item(i);
        item.setAttribute("id", i);
        item.setAttribute("onmouseover", "moveover(src)");
        item.setAttribute("alt", "Color");
        item.hspace = "2";
        item.height = "23";
    }
}

function moveover(colorAddress) {
    var source = colorAddress;
    var image = source.slice(0, source.length - 4);
    var start = "url(";
    var extension = ".jpg)";
    var newImage = start.concat(image, extension);
    document.getElementById('image').style.backgroundImage = newImage;
}

window.onload = function() {
    try {
        list();
    } catch …
Run Code Online (Sandbox Code Playgroud)

html javascript error-handling try-catch setattribute

4
推荐指数
1
解决办法
2万
查看次数

JavaScript从构造函数中调用方法

我正在阅读MDN网站上对JavaScript重新介绍,并在自定义对象部分中看到了这一点:

function personFullName() {
    return this.first + ' ' + this.last;
}
function personFullNameReversed() {
    return this.last + ', ' + this.first;
}
function Person(first, last) {
    this.first = first;
    this.last = last;
    this.fullName = personFullName;
    this.fullNameReversed = personFullNameReversed;
}
Run Code Online (Sandbox Code Playgroud)

它在MDN网站上说,你可以在Person构造函数中引用personFullName()和personFullNameReversed()函数,只需输入它们的名称并将它们作为值分配给上面代码中规定的两个变量(this. fullName和this.fullNameReversed).这对我来说非常清楚,但我的问题是为什么personFullName和personFullNameReversed旁边的括号被省略了?不应该说:

this.fullName = personFullName();
this.fullNameReversed = personFullNameReversed();?
Run Code Online (Sandbox Code Playgroud)

它在MDN网站的示例中呈现的方式我觉得Person构造函数中的fullName和fullNameReversed属性指向一些已经声明的全局变量,而不是在Person构造函数之外声明的函数.

javascript methods constructor function custom-object

1
推荐指数
1
解决办法
58
查看次数