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;
    	}
    }
    
  • 相关阅读:
    k8s-istio记录
    k8s
    单词 -(动物)
    RxJs
    .netcore 3.1 unbuntu
    单词规整
    AutoMapper
    时间
    ye
    特殊权限
  • 原文地址:https://www.cnblogs.com/lsswudi/p/11268449.html
Copyright © 2011-2022 走看看