java编程思想中关于多态性的描述

该博客为转载内容,转载自https://www.cnblogs.com/sunsjorlin/archive/2005/12/30/308483.html ,涉及Java相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


多态性是继数据抽象和继承之后,面向对象语言的第三个特征。
它提供了另一个层面的接品与实现分离,也就是说把  做什么  和  怎么做分离开来。
 多态性是一项很重要的技术,它能够将会变的和不会变的东西分隔开来

一些代码
None.gifimport java.util.*;
None.gif
None.gif
class Note
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
private String noteName;
InBlock.gif    
private Note(String noteName)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.noteName = noteName;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public String toString()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return noteName;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static final Note
InBlock.gif        A 
= new Note("A"),
InBlock.gif        B 
= new Note("B"),
InBlock.gif        C 
= new Note("C");    
ExpandedBlockEnd.gif}

None.gif
class Instrument
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note note)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Instrument.play()"+note);
ExpandedSubBlockEnd.gif    }

InBlock.gif    String what()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "Instrument";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
void adjust()dot.gif{}
ExpandedBlockEnd.gif}

None.gif
class Wind extends Instrument
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Wind.play()" + n);
ExpandedSubBlockEnd.gif    }

InBlock.gif    String what()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "Wind";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
void adjust()dot.gif{}
ExpandedBlockEnd.gif}

None.gif
None.gif
class Percussion extends Instrument
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Percussion.play()"+n);        
ExpandedSubBlockEnd.gif    }

InBlock.gif    String what()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "Percussion";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
void adjust()dot.gif{}
ExpandedBlockEnd.gif}

None.gif
None.gif
class Stringed extends Instrument
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Stringed.play()"+n);
ExpandedSubBlockEnd.gif    }

InBlock.gif    String what()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "Stringed";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
void adjust()dot.gif{}
ExpandedBlockEnd.gif}

None.gif
None.gif
class Brass extends Wind
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Brass.play()"+n);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
void adjust()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Brass.adjust()");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
class Woodwind extends Wind
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
void play(Note n)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.
out.println("Woodwind.play()"+n);
ExpandedSubBlockEnd.gif    }

InBlock.gif    String what()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return "Woodwind";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
public class HelloWord
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public static void tune(Instrument i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        i.play(Note.C);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void tuneAll(Instrument[] e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for(int i = 0; i < e.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tune(e[i]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
public static void main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        Instrument[] orchestra 
= dot.gif{
InBlock.gif                
new Wind(),
InBlock.gif                
new Percussion(),
InBlock.gif                
new Stringed(),
InBlock.gif                
new Brass(),
InBlock.gif                
new Woodwind()
ExpandedSubBlockEnd.gif        }
;        
InBlock.gif        tuneAll(orchestra);
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif


1.当你想要通过一个公共的接口来控制一组类的时候,就可以使用抽象类.
2.如果构造函数调用了一个动态绑定的方法,且那个方法又属于那个正在创建中的对象,那么它会使用那个覆写后的版本。===》一个好的构造函数应该用尽可能少的工作量去把属性初始化并尽量少的调用方法.

转载于:https://www.cnblogs.com/sunsjorlin/archive/2005/12/30/308483.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值