欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

j2se 用友面试题

程序员文章站 2022-07-12 21:41:57
...
/**
 * 
 */
package com.java;

/**
 * @author Administrator
 *
 */
public class TestA {

	interface An{
		void sayHi();
		String getName();
	}
	
	class Dog implements An{
		public String getName()
		{
			return "Bob";
		}
		public void sayHi()
		{
			System.out.println("I am " + getName() + "." +back());
		}
		private String back()
		{
			return "wang-wang";
		}
	}
	
	class BullDog extends Dog{
		public String getName()
		{
			return "Mike";
		}
		private String back()
		{
			return "WOO-WOO";
		}
	}
	
	public void checkout()
	{
		An a = new Dog();
		a.sayHi();
		
		An b = new BullDog();
		b.sayHi();
		
		Dog dog = new Dog();
		check(dog);
		
		BullDog bulldog = (BullDog)b;
		check(bulldog);
	}
	
	public void check(Dog dog)
	{
		System.out.println("check dog "+dog.getName());
	}
	public void check(BullDog bulldog)
	{
		System.out.println("check dog "+bulldog.getName());
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestA tA = new TestA();
		tA.checkout();
	}

}

 

 

结果:

I am Bob.wang-wang
I am Mike.wang-wang
check dog Bob
check dog Mike

相关标签: J2SE 面试