简体   繁体   English

JavaSound API Sequencer接口

[英]JavaSound API Sequencer Interface

How can the method getSequencer in JavaSound API return an instance of the interface Sequencer? JavaSound API中的getSequencer方法如何返回接口Sequencer的实例?

Sequencer sequencer=MidiSystem.getSequencer();

I have read that we cant create an instance of an interface. 我读过我们不能创建接口的实例。

static is a very troublesome concept at best, its greatest akin for trouble at explaining is "volatile" keyword declaration. static充其量是一个非常麻烦的概念,其最大的麻烦在于解释“ volatile”关键字声明。

You would have less trouble with "synchronized" keyword on a code block than the previous two for explaining their usage parameters and concept! 与前面的两个代码块解释其用法参数和概念相比,在代码块上使用“ synchronized”关键字的麻烦会更少!

"static" is not constructed as "new" because it is not a "separate instance" it is already available when compiled in as a static object. “静态”不是构造为“新”的,因为它不是“单独的实例” ,当作为静态对象编译时已经可用。 All interfaces in Java are abstract but have "static" fields(variables) only , ONLY ONE of those loaded static class instruction version of AKA(A Kind of Alias) "instance" of a class(or alternately interface) will be present at that class hierarchy level on the process in the JVM runtime in that particular "user classes" hierarchy structure of call for any number of classes created that commit call of a static object or a static method (static code DOES NOT MAKE A NEW SEPARATE SET OF INSTRUCTION IF CALLED CONCURRENTLY FROM VARIOUS CLASS COPIES). Java中的所有接口都是抽象的,但仅具有“静态”字段(变量)届时将仅存在 那些已加载的AKA(一种别名)的静态类指令版本的某个类(或替代接口)的“实例”。 JVM运行时中进程的类层次结构级别,该特定“用户类”调用的层次结构用于创建的任何数量的提交了对静态对象或静态方法的调用的类 (静态代码不进行新的单独指令集设置) (如果从各种类别的副本中同时调用)。

With anything "static" there is only one copy in use for all of the program at that calling class on the PID process level in the JVM during runtime. 对于任何“静态”,在运行期间,JVM的PID进程级别上的那个调用类的所有程序都只使用一个副本。

You cannot instantiate MidiSystem because all its methods are "static" So to use ANY static class to call one of its static methods from it (or the same on an interface) you only use its class name followed by the dot operator on its method you wish to call. 您不能实例化MidiSystem,因为其所有方法都是“静态的”,因此要使用ANY静态类从其调用其静态方法之一(或在接口上 使用 相同的静态方法 ,则只能使用其类名,然后在其方法上使用点运算符希望致电。 Exactly as you have it in the code and syntax you posted. 与发布的代码和语法完全相同。 (NOTE "Sequencer" is actually static ) (注意“ Sequencer”实际上是静态的)

But if you need your variable non static to remove static you cast it if the class type to cast to is non static, only if the class you are casting is not itself an actual "static" compiled class ! 但是,如果您需要使用非静态变量来删除静态变量,则仅当要转换的类类型本身不是实际的“静态”编译类时,才将其强制转换为非静态!

eg DriverManager.getDriver() for JDBC database running more than one connection concurrently cannot use static driver copies or there would only be one copy available in use of during runtime with the instructions template (the class byte code for the static class) ! 例如,同时运行多个连接的JDBC数据库的DriverManager.getDriver()无法使用静态驱动程序副本,或者在运行时使用带有指令模板的静态副本(静态类的类字节码)! To remove "static" from an object, the object must be cast to non static into a variable of same object type that is not of static notation(declared). 要从对象中删除“静态”,必须将该对象强制转换为非静态类型的对象,该对象类型不是静态表示法(声明)。

// the getDriver() method is static inside class DriverManager , 
// Driver is an Interface not a class
Driver driver = (Driver) DriverManager.getDriver( configuration.jdbcUrl() );
// After casting, there is now a separate non static reference of Driver 
// interface , so **note that neither MidiSystem or DriverManager class are** 
// actually declared static and both have no constructor and not declared 
// abstract but contain only static methods !

an "INSTANCE" is something you construct , so another one is a new instance ! 一个“ INSTANCE” 是您构造的东西 ,因此另一个是一个实例! Interfaces are not constructed, they operate much more alike "abstract" and "static" declarations. 接口不是构造的,它们的操作更像“抽象”和“静态”声明。 Using the class name only is the syntax of calling EITHER abstract or static classes to obtain their methods. 仅使用类名称是调用EITHER抽象或静态类以获取其方法的语法

A final point , to refer to an interface as a "data type" is to make a reference variable to represent it because an interface IS a data type (known as an object) the same as a class or abstract class. 最后一点,将接口称为“数据类型”是使引用变量表示它,因为接口是与类或抽象类相同的数据类型(称为对象)。

So your above code has "Sequencer" interface as a data type. 因此,您上面的代码具有“ Sequencer”接口作为数据类型。 When a class "implements" an interface the class itself can be cast to that interface because it is ALSO that object type. 当类“实现”接口时,该类本身也可以转换为该接口,因为它也是该对象类型。 eg 例如

    public class Example implements Extra{.....}
    Extra example = (Extra)new Example();
// next below shorthand implicit cast is compiler dependent
    public class Example implements Extra{.....}
    Extra example = new Example();

If you do not implement an interface in a class the interface can be called into the code with assignment of a reference variable by using a class that has a method that obtains that interface data type. 如果您未在类中实现接口,则可以通过使用具有获取该接口数据类型的方法的类来调用该接口,并分配参考变量。 There is a huge relationship between abstract classes and interfaces but they are not the same. 抽象类和接口之间存在巨大的关系,但它们并不相同。

Abstract classes do not have global variables. 抽象类没有全局变量。

Interfaces do have global variables but all of them must be static and final. 接口的确具有全局变量,但是它们都必须是静态的和最终的。 Abstract classes cannot have any global variables or it would be an " instance of a class" and would then require to be constructed as "new". 抽象类不能具有任何全局变量 ,否则它将是“类的实例 ”, 然后需要构造为“新”。

Abstract classes have less strict rules on method declaration than interfaces. 抽象类在方法声明方面的规则不如接口严格。

Abstract classes can have most class modifiers interfaces are all public 抽象类可以具有大多数类修饰符,接口都是公共的

Interfaces have "default" modifier for methods that contain an implementation body of code or must be static method. 接口具有“默认”修饰符,用于包含代码的实现主体或必须为静态方法的方法。

In short the variable for Sequencer interface is not an instance variable, it is a reference and (clause for static) you are referring to something defined as "static" so IT MUST be there when the class that calls it starts ! 简而言之,Sequencer接口的变量不是实例变量,它是一个引用,并且(对于静态而言是) 您所引用的是定义为“静态”的内容,因此,在调用它的类开始时,它必须存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM