class Chinese {
public static void print() {
System.out.println("this is 中国人");
}
}
class Guangdongren extends Chinese {
public static void print() {
System.out.println("this is 广东人 ");
}
}
class HunanRen extends Chinese {
public static void print() {
System.out.println("this is 湖南人 ");
}
}
class GuangzhouRen extends Guangdongren {
public static void print() {
System.out.println("this is 广州人 ");
}
}
public class TestUpAndDownChange {
public static void main(String args[]) {
Chinese c = new Chinese();// 这样编译可以通过,但运行会抛出异常
HunanRen h = (HunanRen) c;// 这是因为向下转型错误,中国人不一定是湖南人,这里
// 是不是可以这样理解要将c强制转换成一个HunanRen
HunanRen h1 = new HunanRen();// 这样就没有问题,这是向上转型,
Chinese c1 = (Chinese) h; // 湖南人一定是中国人这是是否可以理解为将h转换成Cinese
Chinese h2 = new HunanRen();// 向上转型
h.print();
}
}
package test;
class Animal {
static {
System.out.println("Animal static code block ");
}
{
System.out.println("Animal code block ");
}
Animal(){
System.out.println("Animal constructor");
}
}
class Tiger extends Animal {
static {
System.out.println("Tiger static code block ");
}
{
System.out.println("Tiger code block ");
}
Tiger(){
System.out.println("Tig constructor ");
}
}
class Dog extends Animal {
}
public class Zoo {
public static void main(String [] args)throws Exception {
//concurrent 并发 precision 精密
//System.out.println("new Zoo before");
//Zoo z = new Zoo();
//Class c = Class.forName("test.Dog",false,z.getClass().getClassLoader());
//Class c = Class.forName("test.Dog");
//Class c = Class.forName("test.Dog");
//System.out.println("initilize before ");
//Animal dog = (Animal)c.newInstance();
Animal dog1 = (Animal) new Tiger();
if(dog1 instanceof Animal){
System.out.println(new StringBuffer(" aaa "));
}
}
}