简体   繁体   English

了解Java中的方法

[英]Understanding methods in Java

I am new to Java.This was a example on OOP.I have a class file named "Automobile.java" and the driver program saved as AutomobileTest.java .My question is when a object is created in AutomobileTest.java how does it know that it has to access the methods and variables from Automobile.java. 我是Java的新手。这是OOP上的示例。我有一个名为“ Automobile.java”的类文件,其驱动程序另存为AutomobileTest.java。我的问题是,在AutomobileTest.java中创建对象时,如何知道它必须访问Automobile.java中的方法和变量。
This is my code: 这是我的代码:
Automobile.java 汽车.java

class Automobile{
    public int numWheels;
    public String colour;
    public boolean engineRunning;
    public double mileage;
    public int numSeats;


    public Automobile(int wheels,int seats,String vehicleColour){
        numWheels=wheels;
        numSeats=seats;
        colour=vehicleColour;
        engineRunning=false;
        mileage=0.0;    
    }

    public void startEngine(){
         engineRunning=true;
    }
    public void stopEngine(){
         engineRunning=false;
    }
    public double drive(double numMiles){
        return mileage+=numMiles;

    }
    public String toString(){
        String s="numWheels="+numWheels+"\nNumber of seats = "+numSeats+ "\nColour:" +colour+ "\nEngineRunning: " + engineRunning;
        return s;
    }
}

AutomobileTest.java AutomobileTest.java

public class AutomobileTest{
    public static void main (String args[]){
        Automobile ferrari=new Automobile(4,2,"red");
        System.out.println(ferrari);
        System.out.println("Engine started");
        ferrari.startEngine();
        System.out.println(ferrari);
    }
}

From your question I totally understand that you are new to Java programming. 从您的问题中,我完全理解您是Java编程的新手。

To answer your question, in the AutomobileTest.java file, you have included the statement 为了回答您的问题,您在AutomobileTest.java文件中添加了以下语句

Automobile ferrari=new Automobile(4,2,"red");

in this line, the keyword "new" will tell the java compiler to create a object of the class Automobile and thus java compiler will know which class to access. 在这一行中,关键字“ new”将告诉java编译器创建Automobile类的对象,因此java编译器将知道要访问哪个类。

To know more about this, you need to do a lot of study. 要了解更多信息,您需要做很多研究。 Refer this book. 请参阅本书。

If you are asking how the contents of AutomobileTest.java knows to find the Automobile class in Automobile.java , this has to do with Java packaging. 如果您要询问AutomobileTest.java的内容如何知道在Automobile.java找到Automobile类,则这与Java打包有关。

In Java, you can declare classes to be of a certain package by saying package X at the top of multiple Java source files. 在Java中,您可以通过在多个Java源文件的顶部说package X来声明类属于某个包。 This means that they will share a "namespace", and thus have access to methods and variables (subject the access modifiers that other commenters are mentioning, such as public and private ). 这意味着它们将共享一个“命名空间”,从而可以访问方法和变量(受其他评论者提及的访问修饰符的影响,例如publicprivate )。

But it seems like your files don't say package X at the top. 但似乎您的文件顶部没有说package X Well, by default Java puts files in the same directory in the "anonymous package", so technically Automobile.java and AutomobileTest.java share a namespace. 好吧,默认情况下,Java将文件放在“匿名包”中的同一目录中,因此从技术上讲Automobile.javaAutomobileTest.java共享一个名称空间。

For more information about Java packages, see these resources: http://en.wikipedia.org/wiki/Java_package http://docs.oracle.com/javase/tutorial/java/package/packages.html 有关Java软件包的更多信息,请参见以下资源: http : //en.wikipedia.org/wiki/Java_package http://docs.oracle.com/javase/tutorial/java/package/packages.html

It knows, because they are in the same package. 它知道,因为它们在同一个程序包中。 Read about packages and access protection here . 在此处阅读有关软件包和访问保护的信息

Have a look at this question: In Java, difference between default, public, protected, and private 看一下这个问题: 在Java中,默认,公共,受保护和私有之间的区别

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  ✔    |    ✔    |    ✔     |   ✔
————————————+———————+—————————+——————————+———————
protected   |  ✔    |    ✔    |    ✔     |   ✘
————————————+———————+—————————+——————————+———————
no modifier |  ✔    |    ✔    |    ✘     |   ✘
————————————+———————+—————————+——————————+———————
private     |  ✔    |    ✘    |    ✘     |   ✘

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

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