java 主要方法和类,在哪里声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16123410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Main method and classes, where do declare?
提问by nexus_2006
New to programming, new to java. I seem to understand the basic pieces like classes and their components, methods, etc, but I can't figure out how to put them together. For example, I'm following the tutorials and trying to write a class Card that can become any card based on args passed during construction.
编程新手,Java 新手。我似乎了解类及其组件、方法等基本部分,但我不知道如何将它们组合在一起。例如,我正在遵循教程并尝试编写一个类 Card,它可以根据构造过程中传递的 args 变成任何卡片。
My first problem is, if I declare class Card public (line 5), the compiler says illegal start of expression.
我的第一个问题是,如果我将 Card 类声明为 public(第 5 行),编译器会说表达式开始非法。
If I remove public, it continues to the println statement, where it complains non-static variable can't be referenced from a static environment. That is because I'm working inside the main method, which is always static, right? So, I need to add methods to Card that will read the internal state and give it to the println statement, would that work?
如果我删除 public,它会继续到 println 语句,在那里它抱怨不能从静态环境中引用非静态变量。那是因为我在 main 方法中工作,它总是静态的,对吧?所以,我需要向 Card 添加方法来读取内部状态并将其提供给 println 语句,这会起作用吗?
public class Deck {
public static void main(String[] args) {
class Card {
public int rank;
public int suit;
public Card(int startSuit, int startRank) {
rank = startRank;
suit = startSuit;
}
}
Card aceOfSpades = new Card(1, 1);
System.out.println("Card is ..." + Card.rank + " of " + Card.suit);
}
}
Round TwoHere is the new code, the file is Card.java:
第二轮这是新的代码,文件是 Card.java:
public class Card {
public class Card {
//declare states
//rank 1-13 for ace-king
//suit 1-4 spade,heart,club,diamond
public int rank;
public int suit;
//constructor
public Card(int startSuit, int startRank) {
rank = startRank;
suit = startSuit;
}
//methods for Card
public static void main(String[] args) {
//call Card constructor
//make card ace of spades (1,1)
Card aceOfSpades = new Card(1,1);
//read internal state of new Card object
//what kind of card is it?
System.out.println("Card is ..." + rank + " of " + suit);
}
}
//declare states
//rank 1-13 for ace-king
//suit 1-4 spade,heart,club,diamond
public int rank;
public int suit;
//constructor
public Card(int startSuit, int startRank) {
rank = startRank;
suit = startSuit;
}
//methods for Card
public static void main(String[] args) {
//call Card constructor
//make card ace of spades (1,1)
Card aceOfSpades = new Card(1,1);
//read internal state of new Card object
//what kind of card is it?
System.out.println("Card is ..." + rank + " of " + suit);
}
}
I balanced my braces, main method is now part of Card class, I think its looking better. The oney compile errors are now associated with the variables in the println statement. (non-static variable suit can't be referenced from a static context.) I think this means I have to write methods like getSuit() and getRank() that will read the variable states and then use the method in my println and not the variables themselves?
我平衡了我的括号,主要方法现在是 Card 类的一部分,我认为它看起来更好。oney 编译错误现在与 println 语句中的变量相关联。(不能从静态上下文中引用非静态变量西装。)我认为这意味着我必须编写像 getSuit() 和 getRank() 这样的方法来读取变量状态,然后在我的 println 中使用该方法而不是变量本身?
That method would look like this, right?
那个方法看起来像这样,对吧?
public int getSuit() {
public int getSuit() {
return suit;
}
return suit;
}
(pls bear with me, my formatting isn't coming out exactly correct, I'll work on it)
(请耐心等待,我的格式不完全正确,我会努力解决的)
采纳答案by mki
Your second code is nearly correct. You just have to modify the code in main like this :
你的第二个代码几乎是正确的。你只需要像这样修改 main 中的代码:
System.out.println("Card is ..." + aceOfSpades.rank + " of " + aceOfSpades.suit);
You are right about the static, main is static he can access only variables that are static.
By adding aceOfSpades you specify the object you have created. You have a reference and you can read the internal values rank and suit.
你是对的静态,主要是静态的,他只能访问静态的变量。通过添加 aceOfSpades,您可以指定您创建的对象。你有一个参考,你可以阅读内部值等级和西装。
But as you suggest a better way is to create setter/getter methods : setRank(int), getRank(), .... You can replace
但正如您所建议的,更好的方法是创建 setter/getter 方法:setRank(int), getRank(), .... 您可以替换
public int rank;
by
private int rank;
So your internal values are protected outside the object.
Note that even if you set your rank as private, the main() method can still read directly the value of rank. It is because private means only visible in the class and main is part of your class.
But if you instanciate a Card object in you Deck object, it will not be able to read directly the content.
因此,您的内部值在对象之外受到保护。
请注意,即使您将 rank 设置为私有,main() 方法仍然可以直接读取 rank 的值。这是因为 private 意味着只在类中可见,而 main 是你类的一部分。
但是如果你在你的 Deck 对象中实例化一个 Card 对象,它将无法直接读取内容。
The idea of hiding the internal values of the objects is fundamental in OO and is called encapsulation
隐藏对象内部值的想法在 OO 中是基本的,称为封装
回答by 0x6C38
The names of your classes have to match the name of their files.
类的名称必须与其文件的名称相匹配。
However, from Bill K's answer to can I compile a java file with a different name than the class:
但是,根据 Bill K 的回答,我可以编译一个名称与类不同的 java 文件:
You CAN have secondary classes inside the same file as long as they are not public. They can still be "default" though, so they can still be used by other classes in the same package.
This should not be done for the most part. Java's naming patterns regarding classes and packages are one of the bigger advantages it has--makes a programmers life easier at no cost.
你可以在同一个文件中有二级类,只要它们不是公共的。但是它们仍然可以是“默认的”,因此它们仍然可以被同一包中的其他类使用。
大多数情况下不应该这样做。Java 关于类和包的命名模式是它拥有的更大优势之一——让程序员的生活更轻松,而且不花钱。
回答by David R Tribble
Move class Card
out of the body of method main()
. All of this code should be in a file having the same name as the outer class, Deck.java
.
将类Card
移出方法体 main()
。所有这些代码都应该在一个与外部类同名的文件中,Deck.java
.