简体   繁体   English

Java中的变量类型?

[英]Variable types in Java?

Sometimes I see methods that have a parameter like public method(ObjectName variable) 有时我会看到带有参数的方法,例如public method(ObjectName variable)

Does that mean this method only accepts ClassName 's objects? 这是否意味着此方法仅接受ClassName的对象? For example from another class? 例如来自另一个班级?

it might be a simple question but I am used to seeing only int's , String s etc. as parameters. 这可能是一个简单的问题,但是我习惯于仅将int'sString等等作为参数。 I guess some methods can accept instances of other classes? 我猜有些方法可以接受其他类的实例? How does compiler know then that the class in the parameter is valid? 编译器如何才能知道参数中的类有效? Does it know it from imports? 它从进口中知道吗?

Basically, yes to all your questions. 基本上,对您所有的问题都可以。 Some methods accept instances of classes, both others or this class. 一些方法接受类的实例,其他方法或此类都可以。 Compiler knows by imports and the other classes in the package. 编译器通过导入和包中的其他类知道。 If you have a more specific question put some code up. 如果您有更具体的问题,请编写一些代码。

The formal rules can be found at http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2 可以在http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2中找到正式规则

Basically if a method accepts class A then it can also accept any subclass of A . 基本上,如果方法接受类A那么它也可以接受A任何子类。 A can be any class, BigInteger InputStream URL etc. If a method can accepts interface B then it will also accept any class that implements B . A可以是任何类,例如BigInteger InputStream URL等。如果方法可以接受接口B那么它也将接受实现B任何类。

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

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