如何创建带有数据字段的 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25924690/
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
How to create a Java class with data fields
提问by Robert
I am in a programming class that has provided me with a project but I have no idea where to start and was hoping someone could push me in the right direction. I am only posting part of the project so that someone can show me a bit of the code to get an idea of how its done as I have taken a programming class before but I am out of practice.
我所在的编程课为我提供了一个项目,但我不知道从哪里开始,并希望有人能将我推向正确的方向。我只是发布项目的一部分,以便有人可以向我展示一些代码,以了解它是如何完成的,因为我之前参加过编程课程,但我没有练习。
Create an application called Registrar that has the following classes:
A Student class that minimally stores the following data fields for a student:
- Name
- Student id number
- Number of credits
The following methods should also be provided:
- A constructor that initializes the name and id fields
- A method that returns the student name field
- Methods to set and retrieve the total number of credits
创建一个名为 Registrar 的应用程序,它具有以下类:
一个 Student 类,它最低限度地为学生存储以下数据字段:
- 名称
- 学生证号码
- 学分数
还应提供以下方法:
- 初始化 name 和 id 字段的构造函数
- 返回学生姓名字段的方法
- 设置和检索学分总数的方法
I have removed most of the question as I am not trying to get the full answer but to just get this little sample to try to get going on the rest of the project. I am also having trouble with the 2nd part as to how I can create names and ID's on a second program and retrieve them into the first program with the classes.
我已经删除了大部分问题,因为我不是要获得完整的答案,而是要获取这个小样本以尝试进行项目的其余部分。关于如何在第二个程序上创建名称和 ID 并将它们检索到第一个程序中的类,我也遇到了第二部分的问题。
回答by DotNet NF
Here is a bit of an translation for what you need to do, the words in bold are keywords that, when googled, will most likely return information relevant to what you are doing.
这是您需要做的事情的一些翻译,粗体字是关键字,当使用谷歌搜索时,很可能会返回与您正在做的事情相关的信息。
A Student class that minimally stores the following data fields for a student:
一个 Student 类,它最低限度地为学生存储以下数据字段:
This basically means to create a classwhich has the following properties:
这基本上意味着创建类,其具有以下性质:
? Name ? Student id number ? Number of credits
? 名称 ?学生证号码 ?学分数
Think hard about what types of data those would be? What typedo you need to create to store somebody's name? Or their Id? Remember, these are all properties
仔细想想这些数据是什么类型的?你需要创建什么类型来存储某人的名字?还是他们的身?记住,这些都是属性
A constructor that initializes the name and id fields
初始化 name 和 id 字段的构造函数
Google constructorand learn all about how they work, pay special attention when a learning source discusses how to initialize propertiesinside of the constructor.
谷歌构造函数并了解它们是如何工作的,当学习源讨论如何在构造函数内部初始化属性时要特别注意。
A method that returns the student name field
返回学生姓名字段的方法
Research about methodsand how you can create one to return your propertyStudent Name. Learn how you will actually use this method.
研究方法以及如何创建一种方法来返回您的财产学生姓名。了解您将如何实际使用此方法。
Methods to set and retrieve the total number of credits
设置和检索学分总数的方法
Research Getters and Settersand understand how they interact with a classesproperties
研究Getter 和 Setter并了解它们如何与类属性交互
Best of luck buddy, google is your best friend/lover in programming..
祝你好运,谷歌是你在编程方面最好的朋友/情人..
回答by BurningDocker
public class Student {
private String name;
private String id;
private int numOfCredits;
public Student(String name, String id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getNumOfCredits() {
return numOfCredits;
}
public void setNumOfCredits(int numOfCredits) {
this.numOfCredits = numOfCredits;
}
}