Java 为什么这些接口类型的变量不用于实例化新对象?
我正在使用api,遇到了一些让我困惑的代码示例。我知道我可以使用“new”将对象分配给接口,因为接口是数据类型。从下面的代码中我不明白的是,为什么变量“cc”和“audioDecoder”被分配了它们已经分配的值。据我所知,这些变量应该分配给新对象。有人能解释一下这是怎么回事吗Java 为什么这些接口类型的变量不用于实例化新对象?,java,Java,我正在使用api,遇到了一些让我困惑的代码示例。我知道我可以使用“new”将对象分配给接口,因为接口是数据类型。从下面的代码中我不明白的是,为什么变量“cc”和“audioDecoder”被分配了它们已经分配的值。据我所知,这些变量应该分配给新对象。有人能解释一下这是怎么回事吗 try { // open media file DefaultMediaPlayer player = new DefaultMediaPlayer("/home/me/walking.wav"); // get
try {
// open media file
DefaultMediaPlayer player = new DefaultMediaPlayer("/home/me/walking.wav");
// get some properties of the first audio stream
IDecoder audioDecoder = player.getAudioStreamDecoder(0);
ICodecContextWrapper cc = audioDecoder.getCodecContext();
int sampleFormat = cc.getSampleFormat();
int sampleRate = cc.getSampleRate();
int bytesPerSample = AVSampleFormat.getBytesPerSample(sampleFormat);
long channelLayout = cc.getChannelLayout();
int channelCount = AVChannelLayout.getChannelCount(channelLayout);
AudioFormat.Encoding encoding;
if (AVSampleFormat.isPlanar(sampleFormat) || AVSampleFormat.isReal(sampleFormat))
throw new LibavException("unsupported output sample format");
else if (AVSampleFormat.isSigned(sampleFormat))
encoding = AudioFormat.Encoding.PCM_SIGNED;
else
encoding = AudioFormat.Encoding.PCM_UNSIGNED;
// create Java InputStream for audio stream raw data
SampleInputStream sis = new SampleInputStream(sampleRate * bytesPerSample * channelCount, true);
// create AudioInputStream from the SampleInputStream
AudioInputStream audioStream = new AudioInputStream(sis, new AudioFormat(encoding, sampleRate,
bytesPerSample * 8, channelCount, bytesPerSample * channelCount, sampleRate,
ByteOrder.BIG_ENDIAN.equals(ByteOrder.nativeOrder())), -1);
// create adapter between Libav audio frames and the SampleInputStream
Frame2AudioFrameAdapter resampler = new Frame2AudioFrameAdapter(channelLayout, channelLayout, sampleRate,
sampleRate, sampleFormat, sampleFormat);
// get audio mixer for the audio stream format
PlaybackMixer audioMixer = PlaybackMixer.getMixer(audioStream.getFormat());
// connect all streams
audioDecoder.addFrameConsumer(resampler);
resampler.addAudioFrameConsumer(sis);
audioMixer.addInputStream(audioStream);
// enable audio stream decoding
player.setAudioStreamDecodingEnabled(0, true);
// start playback
audioMixer.play();
player.play();
// wait until the playback stops
player.join();
// release system resources
player.close();
resampler.dispose();
PlaybackMixer.closeAllMixers();
} catch (Exception ex) {
Logger.getLogger(PlaybackSample.class.getName()).log(Level.WARNING, "unable to play audio", ex);
}
您不需要像这样一直创建对象
SomeClass obj=new SomeClass();
public class OtherClass
{
public SomeClass getSomeClassObject()
{
return new SomeClass();
}
}
你可以有这样的案子
SomeClass obj=new SomeClass();
public class OtherClass
{
public SomeClass getSomeClassObject()
{
return new SomeClass();
}
}
假设某个类可以在另一个类中访问
您可以按如下方式使用它
OtherClass other=new OtherClass();
SomeClass come=other.getSomeClassObject();
如果您已经阅读了API文档。方法 正在返回IDecoder类型的实例。这就是为什么在src中,他们将返回类型分配给IDecoder类型的audioDecoder变量 没有规则规定只能使用
new
将对象指定给接口类型。您可以从方法返回类型分配对象实例
同样,方法IDecoder.getCodecContext()
返回实例icodeContextWrapper
的对象,该对象被分配给变量cc
ICodecContextWrapper cc = audioDecoder.getCodecContext();
GetCodeContext可能是这样的:
ICodecContextWrapper getCodecContext() {
return new ICodecContextWrapper() {
//override methods
};
}
你的问题让我困惑,因为我不明白上面的代码让你困惑。cc和audioDecoder似乎被分配给合法对象。澄清点:您不能创建
新SomeInterface()
对象,因为裸接口没有实现。唯一的方法是使用从接口创建的匿名内部类;有什么问题吗?变量不需要分配新对象,只需分配对象:Foo-Foo=new-Foo();Foo-foo2=Foo代码>谢谢你的帮助,Jayamohan。