面向对象(中)-多态性使用举例
package top.qaqaq.java.P281;

import java.sql.Connection;

//多态性的使用举例一:
public class AnimalTest {

	public static void main(String[] args) {
		
		AnimalTest test = new AnimalTest();
		test.func(new Dog());
		
		test.func(new Cat());
	}
	
	public void func(Animal animal) {//Animal animal = new Dog();
		animal.eat();
		animal.shout();
		if(animal instanceof Dog) {
			Dog d = (Dog)animal;
			d.watchDoor();
		}
	}
	
//	public void func(Dog dog) {
//		dog.eat();
//		dog.shout();
//	}
//	
//	public void func(Cat cat) {
//		cat.eat();
//		cat.shout();
//	}
}

class Animal{
	
	public void eat() {
		System.out.println("动物:进食");
	}
	
	public void shout() {
		System.out.println("动物:叫");
	}
}

class Dog extends Animal{
	
	public void eat() {
		System.out.println("狗吃骨头");
	}
	
	public void shout() {
		System.out.println("汪!汪!汪!");
	}
	
	public void watchDoor() {
		System.out.println("看门");
	}
}

class Cat extends Animal{
	
	public void eat() {
		System.out.println("猫吃鱼");
	}
	
	public void shout() {
		System.out.println("喵!喵!喵!");
	}
}

//举例二:

class Order{
	
	public void method(Object obj) {
		
	}
}

//举例三:

class Driver{
	
	public void doData(Connection conn) {//conn = new MySQLConnection(); / conn = new OracleConnection();
		//规范的步骤去操作数据
//		conn.method1();
//		conn.method2();
//		conn.method3();
		
	}
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇