首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java自行车和自行车测试

Java自行车和自行车测试
EN

Stack Overflow用户
提问于 2014-09-12 05:57:03
回答 7查看 5.4K关注 0票数 0

我有问题要写我的代码。这是一个代码,我必须创建一个对象类并使用另一个类对象运行它。这个程序叫做自行车和自行车。我得到了自行车程序(它已经写好了),我只需要写自行车就可以使用自行车了。现在,问题是,我已经创建了两个对象,名为NiceBicycle和CoolBicycle。我需要将我的NiceBicycle名称更改为"Kenny McCormick,但我做不到。“对于我编写的命令行,我一直收到错误的信息" error :变量NiceBicycle可能没有初始化”。

//使用setOwnerName NiceBicycle.setOwnerName("Kenny McCormick");将所有者的名称更改为Kenny NiceBicycle.setOwnerName("Kenny McCormick");

我该怎么办?

总之,这是自行车代码,也是我根据指导员命令编写的自行车测试代码。感谢您的回复

bicycle.java

代码语言:javascript
复制
public class Bicycle
{

// Instance field
private String ownerName;
private int licenseNumber;

// Constructor
public Bicycle( String name, int license )
{
     ownerName = name;
     licenseNumber = license;
}

// Returns the name of this bicycle's owner
public String getOwnerName()
{
     return ownerName;
}

// Assigns the name of this bicycle's owner
public void setOwnerName( String name )
{
     ownerName = name;
}

// Returns the license number of this bicycle
public int getLicenseNumber()
{
     return licenseNumber;
}

// Assigns the license number of this bicycle
public void setLicenseNumber( int license ) 
{
     licenseNumber = license;
}

}

这是我写的bicycletest.java。

代码语言:javascript
复制
  public class BicycleTest 
   {

    public static void main( String[] args )
    {

    // Create 1 Bicycle reference variable. For example: myBike
    Bicycle NiceBicycle;

    // Create 1 String reference variable for the owner's name
    String name;

    // Create 1 integer variable for license number
    int licenceNumber;

    // Assign your full name and a license number to the String and
    // integer variables
    name = "Boo Yeah";
    int licenseNumber = 9972;

    // Create a Bicycle object with the Bicycle class constructor
    // Use the variables you created as arguments to the constructor
    Bicycle CoolBicycle = new Bicycle( "Boo Yeah", 9972 );

    // Output the owner's name and license number in printf statements
    // using the object reference and the get methods.
    // For example: bike.getOwnerName()
    System.out.printf ("The CoolBicycle owner's name is %s\nThe license number is %d\n", CoolBicycle.getOwnerName(), CoolBicycle.getLicenseNumber());

    // Change the owner's name to Kenny McCormick using setOwnerName
    NiceBicycle.setOwnerName("Kenny McCormick");

    // Output the owner's name and license number in printf statements
      // using the Bicycle object reference variable and the get methods.
      System.out.printf ("The NiceBicycle owner's name is %s\n", NiceBicycle.getOwnerName());
 }

}

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-09-12 06:04:18

您需要在测试中实例化自行车并将其分配给NiceBicycle,方法是更改:

代码语言:javascript
复制
Bicycle NiceBicycle;

至:

代码语言:javascript
复制
Bicycle NiceBicycle = new Bicycle("", 0);

然后您可以对其进行setOwnerName():

代码语言:javascript
复制
NiceBicycle.setOwnerName("Kenny McCormick");

另外,请注意,Java约定建议变量名以小写字母开头,因此,如果您想遵循约定,那么niceBicycle实际上应该是niceBicycle

票数 1
EN

Stack Overflow用户

发布于 2014-09-12 06:02:18

NiceBicycle尚未初始化(尚未创建自行车对象)。

试着替换

代码语言:javascript
复制
Bicycle NiceBicycle;

使用

代码语言:javascript
复制
Bicycle NiceBicycle = new Bicycle("",0);
票数 0
EN

Stack Overflow用户

发布于 2014-09-12 06:02:24

编译器是仁慈的。在尝试使用NiceBicycle之前,您没有设置它。变量未设置。

还请注意,Java约定总是以小写开头的变量命名,以大写开头的类命名。这一点很重要,因为.负载过重,也就是说,它产生了许多不同的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25801666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档