日期:2014-05-20 浏览次数:21106 次
class OutContent {
int m = 0;
public void output(NodeList nodeList) {
int size = nodeList.getLength();
for (int k = 0; k < size; k++) {
Node node = nodeList.item(k);
if (node.getNodeType() == Node.TEXT_NODE) {
//父类型Node强转成子类型Text,怎么不报错?
Text textNode = (Text) node;
String content = textNode.getWholeText();
m++;
System.out.print(content);
}
if (node.getNodeType() == Node.ELEMENT_NODE) {
//这里也是样的问题
Element elementNode = (Element) node;
String name = elementNode.getNodeName();
System.out.print(name);
NodeList nodes = elementNode.getChildNodes();
output(nodes);
}
}
}
package com.test;
class A {
void aMthod() {
System.out.println("A method");
}
}
class B extends A {
void bMethod1() {
System.out.println("B method 1");
}
void bMethod2() {
System.out.println("B method 2");
}
}
public class C {
public static void main(String[] args) {
A a2 = new A();
B b2 = (B) a2; // 向下转型,编译无错误,运行时将出错
b2.aMthod();
b2.bMethod1();
b2.bMethod2();
}
}