简体   繁体   English

是否可以在java中获取class object的值,如果可以如何获取?

[英]Is it possible to have values for class object in java, if yes how can we acquire it?

Let's say, I am initializing a class obj as follows:比方说,我正在初始化一个 class 对象,如下所示:

<class> obj = <some value>;

Is it possible to initialize as such I mean we initialize String and primitive values like that, But I mean for any class. And how can we get them so they can be used in some method of the same class?是否可以这样初始化我的意思是我们像那样初始化字符串和原始值,但我的意思是对于任何 class。我们如何获得它们以便它们可以用于相同 class 的某些方法?

Let's say, I am initializing a class obj as follows:假设,我正在初始化一个 class obj,如下所示:

<class> obj = <some value>;

Is it possible to initialize as such I mean we initialize String and primitive values like that, But I mean for any class.是否可以这样初始化我的意思是我们初始化字符串和类似的原始值,但我的意思是任何 class。 And how can we get them so they can be used in some method of the same class?我们怎样才能得到它们,以便它们可以用于相同 class 的某种方法?

Yes if it is for built in types like string, int, float etc. These built in types are pass by value.是的,如果它用于内置类型,如字符串、整数、浮点数等。这些内置类型是按值传递的。 Which means if you say int a = 5 , 5 is copied over to memory location.这意味着如果你说int a = 55被复制到 memory 位置。

If you are asking about your class, I do not think so.如果你问的是你的 class,我不这么认为。 Unlike C++ you cannot overload operator.C++不同,您不能重载运算符。 Another reason why you cannot do this is objects are pass by reference, meaning你不能这样做的另一个原因是对象是通过引用传递的,意思是

MyClass myObject = <someValue>;

<someValue> is not copied to memory where myObject lives, instead reference (address of) it is. <someValue>不会复制到myObject所在的 memory,而是它的引用(地址)。 Thus, you need to have new myObject() .因此,您需要有new myObject()

One disclaimer you can do this:一个免责声明你可以这样做:

MyClass objA = new MyClass();
MyClass objB = objA;

but it is still copying reference not the actual values from myObj .但它仍在复制引用,而不是myObj的实际值。

OP please read this . OP 请阅读内容。

While I think from some aspect this question is duplicate of this , I think the levels of knowledge between OPs of these questions is so far apart that I am not going to flag it.虽然我认为从某些方面来看这个问题与this重复,但我认为这些问题的 OP 之间的知识水平相差甚远,因此我不会标记它。

暂无
暂无

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

相关问题 我们可以在java中创建自己的System类吗? - can we create our own System class in java , if yes then how? 当我们有 Class 类型的 object 时,如何在 Java 中创建实例 - How to create a instance in Java when we have object of type Class 我们如何在java中创建类Object类 - How can we create a class like Object class in java 是否可以将int转换为自定义类类型(在Java中)? 如果是,那怎么办? - is it possible to cast int into custom class type(in java)? if yes then how? 如果可以的话,我可以在不使用java中的Wrapper类的情况下求和两个String值吗? - Can i sum two String values without using Wrapper Class in java if yes than how ? 我们如何在Java中的第一个类中调用第二个调用的对象 - how we can call the object of second call in the first class in java 如何检查我们的 class 中有 object - How to check we have object in our class 我们可以在 Java 中创建一个 object 而不是像在 js 中那样使用 class 或使用随机键和值创建 python - Can we create an object in Java with out class like in js or python with random key and values 我们可以使用PrintWriter类在Android应用程序中将字符串保存到文件中吗? 如果是,那怎么办? - Can we use PrintWriter Class for saving a string in a file in an Android App? If yes, then how? 是否可以在Java中嵌套多个枚举? - Is it possible (and how if yes) to nest several enums in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM