zoukankan      html  css  js  c++  java
  • java对象转型体现可扩展性

    java对象转型体现可扩展性

    public class TestAnimal {
    	public static void main(String args[]) {
    		TestAnimal t = new TestAnimal();
    		Animal a = new Animal("name");
    		Cat c = new Cat("catname","blue");
    		Dog d = new Dog("dogname","black");
    		t.f(a);t.f(c);t.f(d);
    	}	
    	public void f(Animal a) {
    		System.out.println("name: "+a.name);
    		if(a instanceof Cat ) {
    			Cat cat = (Cat)a;
    			System.out.println(" "+cat.eyescolor+" eyes");
    		} 
    		else if(a instanceof Dog) {
    			Dog dog = (Dog)a;
    			System.out.println(" "+dog.furcolor+" fur");
    		}
    	}	
    		
    		
    		
    	
    }
    
    class Animal {
    	public String name;
    	Animal (String name) {
    		this.name = name;
    	}
    }
    
    class Cat extends Animal {
    	public String eyescolor;
    	Cat(String n,String c) {
    		super(n); eyescolor = c;
    	}
    }
    
    class Dog extends Animal {
    	public String furcolor;
    	Dog(String n,String c) {
    		super(n); furcolor = c;
    	}
    }
    
  • 相关阅读:
    zoj 3693, 卡精度
    zoj 3690, 计数 dp , 快速幂
    hdu 1496,枚举
    zoj 2399, 哈弗曼编码
    poj 2560,mst
    poj 2007, 乱搞,计算几何
    bnu 29064, 期望 水题
    img,bg
    垂直居中,定位的方法
    .reverse ,join,split区分
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11268449.html
Copyright © 2011-2022 走看看