简体   繁体   English

Java对象和方法参数

[英]Java object and method parameters

I was just wondering if I was missing anything for the following question. 我只是想知道我是否遗漏了以下问题的任何内容。 I've posed the question with my answer after it. 我跟我的答案提出了问题。

What kind of objects can be passed into the following method? 什么样的对象可以传递给以下方法? What methods could be invoked on obj inside this method? 可以在此方法中的obj上调用哪些方法?

public void doThis (Object obj)
{
    // some code
}

My answer: 我的答案:

The type of objects that can be passed in the above method are objects instantiated from actual classes that are super classes of the current class and also objects that have been instantiated in the current class itself. 可以在上述方法中传递的对象类型是从作为当前类的超类的实际类实例化的对象,以及已在当前类本身中实例化的对象。 Additionally, objects that have been instantiated from other actual classes can be type casted into the current class or one of it's super classes to be allowed as a parameter of the method doThis(). 此外,已经从其他实际类实例化的对象可以类型转换为当前类或其中一个超类,以允许作为方法doThis()的参数。

The methods that can be invoked inside the method include any public, protected, or private methods within the current class and any inherited methods from a superclass. 可以在方法内部调用的方法包括当前类中的任何公共,受保护或私有方法以及来自超类的任何继承方法。

Is it correct? 这是正确的吗?

What kind of objects can be passed into the following method? 什么样的对象可以传递给以下方法?

Any subclass of Object , including Object itself. Object任何子类,包括Object本身。

What methods could be invoked on obj inside this method? 可以在此方法中的obj上调用哪些方法?

Any public/protected method defined in the Object class (eg toString , notify , wait , and etc.). Object类中定义的任何公共/受保护方法(例如toStringnotifywait等)。 This does not include methods defined in the subclass, unless you explicitly downcast. 除非您明确地向下转换,否则这不包括子类中定义的方法。

You are overwording what is really a simple answer. 你真的是一个简单的答案。 The argument you can pass into doThis can be an object of type Object or any of its subclasses. 您可以传递给doThis的参数可以是Object类型的Object或其任何子类。 Which is everything (except the primitive types). 这是一切(原始类型除外)。

Inside doThis , you can invoke most methods, not just the ones in the class and superclasses. doThis ,您可以调用大多数方法,而不仅仅是类和超类中的方法。 For example, System.out.println() . 例如, System.out.println()

Your answer is incorrect. 你的答案是不正确的。

Any subclass of Object can be passed into this method. Object任何子类都可以传递给此方法。 All objects are subclasses of Object , including arrays. 所有对象都是Object子类,包括数组。 Once passed in, you can invoke any method defined in the Object class on it, unless you typecast it first. 传入后,您可以调用Object类中定义的任何方法,除非您首先对其进行类型转换。

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

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