简体   繁体   English

你在 Java 中怎么称呼这个对象,你为什么使用它?

[英]What do you call this object in Java and why do you use?

So I'm working on with two class now and just learned to do this, yet still don't know why is this possible and what this is called.所以我现在正在和两个班级一起工作,刚刚学会了这样做,但仍然不知道为什么这是可能的,这叫什么。 Class variable?类变量? Objects creating objects?对象创建对象? And in the setter method, why the default value is null?而在setter方法中,为什么默认值为null? is it like String?它像字符串吗? From what I'm assuming, 'RetailItem item' is like it combined the whole RetailItem class and creating a new variable in another class with its feature.根据我的假设,'RetailItem item' 就像它结合了整个 RetailItem 类并在另一个类中创建了一个具有其功能的新变量。

There are two classes named CashRegister and RetailItem.有两个名为 CashRegister 和 RetailItem 的类。

Here goes the instance 'RetailItem item' from CashRegister with a setter.这是来自 CashRegister 的带有 setter 的实例“RetailItem item”。


public class CashRegister
{
    private RetailItem item;

    public void setItem (RetailItem newItem)
    {
        if (newItem != null)
        {
            item = newItem;
        }else{
            item = new RetailItem();
        }
    }
}

RetailItem() is a default constructor from RetailItem class. RetailItem()是来自RetailItem类的默认构造函数。 What does item = new RetailItem();什么item = new RetailItem(); mean?意思? I don't know where am I even going to start studying.我什至不知道我要从哪里开始学习。 What am I missing?我错过了什么?

I have some trouble understanding your English, so I might have misinterpreted some of your questions.我在理解你的英语时遇到了一些困难,所以我可能误解了你的一些问题。

What do you call this object in Java and why do you use?你在 Java 中怎么称呼这个对象,你为什么使用它?

I don't know which "object" you are referring to.我不知道你指的是哪个“对象”。

... yet still don't know why is this possible and what this is called. ...但还是不知道为什么this可能的和this叫。

It is just called this .它只是被称为this There is no other term for it.没有其他术语可以描述它。

Class variable?类变量?

No. this is not a class variable.不, this不是类变量。

Objects creating objects?对象创建对象?

(Huh?) No. this is not "objects creating objects". (嗯?)不。 this不是“对象创建对象”。

And in the setter method, why the default value is null?而在setter方法中,为什么默认值为null?

Because that is the way that Java defined.因为这是 Java 定义的方式。 Any instance variable (like item ) whose type is a reference type has a default initial value of null .任何类型为引用类型的实例变量(如item )的默认初始值为null

(Why? Because that is the only value that makes sense from a language perspective. Anything else, and there would need to be some way for the programmer to say what the default is. Compilers can't read your mind!) (为什么?因为从语言的角度来看,这是唯一有意义的值。其他任何事情,程序员都需要某种方式来说明默认值是什么。编译器无法读懂您的想法!)

is it like String?它像字符串吗?

Not sure what you mean.不明白你的意思。 But if you are asking if you would get the same default value behavior if item had been declared with type String , then Yes.但是,如果您要问的是,如果使用String类型声明item是否会获得相同的默认值行为,那么 Yes。

From what I'm assuming, RetailItem item is like it combined the whole RetailItem class and creating a new variable in another class with its feature.根据我的假设, RetailItem item就像它结合了整个RetailItem类并在另一个类中创建了一个具有其功能的新变量。

Not exactly.不完全是。 What RetailItem item is actually doing is declaring a variable in which you may then put a reference to a RealItem object. RetailItem item实际所做的是声明一个变量,然后您可以在其中放置对RealItem对象的引用。 This variable has the default value null .... until some other value is assigned to it.这个变量的默认值是null .... 直到其他一些值被分配给它。

What does item = new RetailItem();什么item = new RetailItem(); mean?意思?

That means create (construct) a new RetailItem instance (an object), and then assign its reference to the variable item .这意味着创建(构造)一个新的RetailItem实例(一个对象),然后将其引用分配给变量item

I don't know where am I even going to start studying.我什至不知道我要从哪里开始学习。

I recommend that you start with a good introductory Java textbook, or the Oracle Java Tutorial, or your course lecture notes.我建议您从一本优秀的 Java 入门教科书、Oracle Java 教程或课程讲义开始。 Ask your teachers.问你的老师。

But keep at it.但要坚持下去。 If you work at it, you will get to the point where it all starts to make sense.如果你努力工作,你就会到达一切开始变得有意义的地步。 Because, basic Java is a simple and consistent language.因为,基本的 Java 是一种简单且一致的语言。 The language only gets complicated when you learn about generics, type inference / lambdas and .... multi-threading.当您了解泛型、类型推断 / lambdas 和 .... 多线程时,语言只会变得复杂。

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

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