java - 对象和“.class"用 [英] java - Object and ".class" use

查看:48
本文介绍了java - 对象和“.class"用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我已经涵盖了 Java 的所有基本概念.到目前为止,我遇到过使用 Object 类和使用.class"语法创建对象引用的示例.

I am a newbie and I have covered all the basic concepts of Java. Till now, I have come across examples where object references are created using Object class and using the ".class" syntax.

我试图寻找关于如何使用这两个概念的解释,但我没有得到清晰的画面.

I tried searching for explanation on how these 2 concepts are used and I am not getting a clear picture.

我所看到的是,是的,我们可以创建对 Object 的引用.但是这个参考是如何使用的?

All I see is, yes we can create a reference to Object. But how/what is this reference used for?

对于.class 语法,我一直看到术语反射",但是当我在像existingclass.class 这样的现有类上使用这种语法时,会发生什么?是否只是在 <existingclass> 中使用方法/数据而不创建实例/对象?

And for .class syntax, I keep seeing the term 'Reflection' but what really happens when I use this syntax on an exsting class like existingclass.class? And is it just to use the methods/data with in the <existingclass> without creating an instance/object?

示例将不胜感激.

推荐答案

  1. Object的引用可以指向任何对象;它可以在不同时间指向不同种类的对象.当你想传入任何类型的对象时,它是一个很好的方法参数类型,当你想要能够返回任何类型的对象时,它是一个很好的返回类型.

  1. A reference to Object can point to any object; it can point to different kinds of objects at different times. It's a good parameter type for a method when you want to pass in any kind of object, and it's a good return type when you want to be able to return any kind of object.

符号ClassName.class 返回一个java.lang.Class 的实例,它代表ClassName.对于 Java 虚拟机中加载的每个类,都有一个 java.lang.Class 对象来表示该类.至于它有什么好处——它只适用于相当高级的、神奇的东西,比如编写询问类有什么方法的代码,或者当你只有用户输入的类的名称时创建类的实例或从文件中读取.

The notation ClassName.class returns an instance of java.lang.Class which represents ClassName. For every class loaded in the Java virtual machine, there is a java.lang.Class object to represent that class. As to what it's good for -- it's only good for fairly advanced, magical stuff like writing code that asks what methods a class has, or creating an instance of a class when all you have is the name of the class typed in by a user or read from a file.

此外,当您了解线程时,您会发现类对象在静态方法方面具有特殊用途;现在你不必担心了.作为初级 Java 程序员,您无需使用它;不过,总有一天你可能会.

Also, when you learn about threads, you'll see that the class object has a special purpose wih respect to static methods; this need not concern you now. As a beginning Java programmer, you'll have no need to use it; someday, though, you may.

这篇关于java - 对象和“.class"用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆