简体   繁体   English

关于Java语法的一些疑问

[英]Some Doubts about Java Syntax

I am a beginner in Java, I know something of the basics, but sometimes I see lines of codes that I don't really understand why they are written that way, here are some questions that I have:我是 Java 的初学者,我知道一些基础知识,但有时我会看到一些我不太明白为什么要这样写的代码行,这里有一些问题:

Question 1:问题一:

Methods or attributes that are called with other methods or attributes:与其他方法或属性一起调用的方法或属性:

Ex.: System.out.print();例如:System.out.print();

I understand that system is a class, and when you write System.out, you are calling the "out" attribute, but from the "out" attribute, you call the "print()" method, is the print() method within the out attribute?我知道系统是 class,当您编写 System.out 时,您正在调用“out”属性,但是从“out”属性中,您调用“print()”方法,是 print() 方法出属性? how does this works.这是如何工作的。 Also sometimes I see method being called that way: ... method1().method2();有时我也会看到这样调用方法: ... method1().method2(); If I put a method inside the scope of another method, won't it run automattically?如果我把一个方法放在另一个方法的 scope 里面,它不会自动运行吗? like:喜欢:

public void method1(){ method 2};公共无效方法1(){方法2};


Question 2: I've been learning about the Date and Calendar classes, and I saw a video where the guy instantieted objects of Date and Calendar, without using the world "new", neither a construction method:问题 2:我一直在学习 Date 和 Calendar 类,我看到了一个视频,其中那个家伙实例化了 Date 和 Calendar 的对象,没有使用世界“新”,也不是构造方法:

Date d = Date.from(Instant.parse("2018-06-25T15:42:07Z"));日期 d = Date.from(Instant.parse("2018-06-25T15:42:07Z"));

Calendar cal = Calendar.getInstance();日历 cal = Calendar.getInstance();

How does that works?这是怎么回事? Can I instantiate any object of any class by calling a abstract method (if the class have one)?我可以通过调用抽象方法(如果 class 有一个)实例化任何 class 的任何 object 吗? Or is just that in those methods they are returning a Date and Calendar object?或者只是在那些方法中他们返回一个日期和日历 object?


Question 3:问题 3:

How can a array of type have a the atributte "length", aren't array just a set of primitive types?类型数组如何具有属性“长度”,数组不只是一组原始类型吗? how can a primitive type have attributes??原始类型如何具有属性?

Ex: int[] x = new int[3];例如: int[] x = new int[3];

System.out.print(x.length); System.out.print(x.length); //Prints 3; //打印3;

Question 1:问题一:

System has a class, and it has a field (often in Java, it's called a field instead of an attribute. They are the same thing though) called out . System有一个 class,并且它有一个字段(通常在 Java 中,它被称为字段而不是属性。尽管它们是相同的东西)调用out out is of type PrintStream . outPrintStream类型。 You don't have to worry about what a printStream is, but just know that printStream has a method called print .您不必担心printStream是什么,只需知道printStream有一个名为print的方法。 So, you access System 's field called out , and you call that fields print method.因此,您访问System的字段调用out ,然后调用该字段的print方法。

You can do stuff like method1().method2() because method1 returns an object, and you call that objects method.您可以执行诸如method1().method2()之类的操作,因为method1返回 object,并且您调用该对象方法。 For example, let's say you have a class which has a method called print() .例如,假设您有一个 class ,它有一个名为print()的方法。 Then if you have a method like this:然后,如果您有这样的方法:

public A getA() {
    return new A();
}

Then if you call that method, you will get an a class.然后,如果您调用该方法,您将得到一个 class。 With that a class, you can call it's method and access it's fields.有了 class,您可以调用它的方法并访问它的字段。

getA().print();

Question 2问题2

Like in the previous answer, you can get objects from methods.与前面的答案一样,您可以从方法中获取对象。 So, you assign your object to the return value of that method.因此,您将 object 分配给该方法的返回值。 For example, if you have the same method as before:例如,如果您使用与以前相同的方法:

public A getA() {
    return new A();
}

You can do:你可以做:

A a = getA();

Since it returns an A type, you assign it to your A .由于它返回A类型,因此您将其分配给您的A

Question 3问题 3

Array is actually a special type.数组实际上是一种特殊类型。 It's technically an object, which allows it to have fields like length , however you can still get elements using array[5] for example.从技术上讲,它是一个 object,它允许它具有像length这样的字段,但是您仍然可以使用array[5]获取元素。 I don't think you should worry about arrays, it is very different than regular objects.我认为您不必担心 arrays,它与常规对象非常不同。

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

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