var makeSound = function( animal ){
if ( animal instanceof Duck ){
console.log( '嘎嘎嘎' );
}else if ( animal instanceof Chicken ){
console.log( '咯咯咯' );
}
};
var Duck = function(){};
var Chicken = function(){};
makeSound( new Duck() ); //嘎嘎嘎
makeSound( new Chicken() ); //咯咯咯
var makeSound = function( animal ){
animal.sound();
};
var Duck = function(){}
Duck.prototype.sound = function(){
console.log( '嘎嘎嘎' );
};
var Chicken = function(){}
Chicken.prototype.sound = function(){
console.log( '咯咯咯' );
};
makeSound( new Duck() ); //嘎嘎嘎
makeSound( new Chicken() ); //咯咯咯
var Dog = function(){}
Dog.prototype.sound = function(){
console.log( '汪汪汪' );
};
makeSound( new Dog() ); //汪汪汪
String str; str = abc; //没有问题 str = 2; //报错
public class Duck { //鸭子类
public void makeSound(){
System.out.println( 嘎嘎嘎 );
}
}
public class Chicken { //鸡类
public void makeSound(){
System.out.println( 咯咯咯 );
}
}
public class AnimalSound {
public void makeSound( Duck duck ){ //(1)
duck.makeSound();
}
}
public class Test {
public static void main( String args[] ){
AnimalSound animalSound = new AnimalSound();
Duck duck = new Duck();
animalSound.makeSound( duck ); //输出:嘎嘎嘎
}
}
public class Test {
public static void main( String args[] ){
AnimalSound animalSound = new AnimalSound();
Chicken chicken = new Chicken();
animalSound.makeSound( chicken ); //报错,只能接受Duck类型的参数
}
}
public abstract class Animal {
abstract void makeSound(); //抽象方法
}
public class Chicken extends Animal{
public void makeSound(){
System.out.println( 咯咯咯 );
}
}
public class Duck extends Animal{
public void makeSound(){
System.out.println( 嘎嘎嘎 );
}
}
Animal duck = new Duck(); //(1)
Animal chicken = new Chicken(); //(2)
public class AnimalSound{
public void makeSound( Animal animal ){ //接受Animal类型的参数
animal.makeSound();
}
}
public class Test {
public static void main( String args[] ){
AnimalSound animalSound= new AnimalSound ();
Animal duck = new Duck();
Animal chicken = new Chicken();
animalSound.makeSound( duck ); //输出嘎嘎嘎
animalSound.makeSound( chicken ); //输出咯咯咯
}
}
var googleMap = {
show: function(){
console.log( '开始渲染google地图' );
}
};
var renderMap = function(){
googleMap.show();
};
renderMap(); // 输出: 开始渲染google地图
var googleMap = {
show: function(){
console.log( '开始渲染google地图' );
}
};
var baiduMap = {
show: function(){
console.log( '开始渲染baidu地图' );
}
};
var renderMap = function( type ){
if ( type === 'google' ){
googleMap.show();
}else if ( type === 'baidu' ){
baiduMap.show();
}
};
renderMap( 'google' ); // 输出: 开始渲染google地图
renderMap( 'baidu' ); // 输出: 开始渲染baidu地图
var renderMap = function( map ){
if ( map.show instanceof Function ){
map.show();
}
};
renderMap( googleMap ); // 输出: 开始渲染google地图
renderMap( baiduMap ); // 输出: 开始渲染baidu地图
var sosoMap = {
show: function(){
console.log( '开始渲染soso地图' );
}
};
renderMap( sosoMap ); // 输出: 开始渲染soso地图
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有