在 BlueJay 中,您使用的是 Windows 版还是 Mac 版?
无论哪种方式,我都会通过 Mac 版本并希望它能正常工作。
创建一个新类并输入 Person 作为类。
通过右键单击类并单击打开编辑器来调出编辑器窗口。
您的课程应具有以下格式。
/*** Write a description of class Person here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Person
{
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class Person
*/
public Person()
{
// initialise instance variables
x = 0;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}
你说的第一个要求是
- 它的构造函数接收 3 个参数,两个 String 代表名字和姓氏,一个 int 代表年龄
你在代码中做得好的地方
public Person(String firstName, String lastName, int age)
所以那里没有变化。 :D
不是
public Person(String first, String last, int age)
[我们可以改用这个,但不要混淆,似乎对方希望你使用第一个]
虽然我们希望这个类获取这 3 个参数所以我们需要
1)有私有变量来保存这些值
2) 在构造函数中赋值。
回顾一下 BlueJ 用这些台词带来的东西
// instance variables - replace the example below with your own
private int x;
/**
* Constructor for objects of class Person
*/
public Person()
{
// initialise instance variables
x = 0;
}
我们希望在您的构造函数中进行更改以及附加更改 1) 和 2)
// instance variables - replace the example below with your own
// private int x; replace blueJ sample variable
private String firstName; //not the same as the one given in the constructor
private String lastName; //not the same as the one given in the constructor
private int age; //not the same as the one given in the constructor
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, int age)
{
// initialise instance variables
//x = 0; replace BlueJ sample with
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
好的,所以只要呼吸并观察它。我们有两组每个变量!一个是用于类的,另一个是提供给构造函数的。现在要解释所有内容太多了 :( ,所以你需要阅读一本 Java 书籍 [只是前几章......在观看 24 季第 1 季时阅读它],大多数书籍都带有这个 Person 类示例。
好吧,所以取消了第一个要求。让我们看看第二个。
-它的方法getName没有参数,返回一个格式为“Lastname, Firstname”的String
所以它是一个方法,为简洁起见,跳转到本节
http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html
如果我这样做,它会更好地解释它。好的,我给你一点时间。
你读了吗?不 ?去读吧Grr! :( 这很重要
好的,现在您已经完成了以下操作。
/**
* The getName Method - put in a little description here
*
* @return the String with the format "Lastname, Firstname"
*/
public String getName()
{
// put your code here
// I did and this is called string concatenation in java
// Google it:"string concatenation in java"
return this.lastName + ", " + this.firstName;
}
所以我制作了一种类似于 BlueJ 制作的方法以及它的外观。它获取名称(this.lastName 和 this.firstName NOT lastName 和 firstName 也可以,但不要让我们自己混淆 k?)
BlueJ 中的完整代码
/**
* Write a description of class Person here.
*
* @user208639 (Is that your real name ?)
* @version (a version number or a date)
*/
public class Person
{
// instance variables - replace the example below with your own
// private int x; replace blueJ sample variable
private String firstName;
private String lastName;
private int age;
/**
* Constructor for objects of class Person
*/
public Person(String firstName, String lastName, int age)
{
// initialise instance variables
//x = 0; replace BlueJ sample with
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/**
* The getName Method - put in a little description here
*
* @return the String with the format "Lastname, Firstname"
*/
public String getName()
{
// put your code here
// I did and this is called string concatenation in java
// Google it:"string concatenation in java"
return this.lastName + ", " + this.firstName;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
// put your code here
return x + y;
}
}
你的方法正确吗?
有点 ?如果您对编码、变量、数据类型和方法一无所知,我会说这是一个合理的猜测 :) ...但您真的应该阅读一本 Java Intro 书。
正确的程序?呐……这个 BlueJ 程序很奇怪。
Google 的“NetBeans”是免费的。
好的,西海岸的早餐时间已经过去了。