내 코드를 작성하는 데 문제가 있습니다. 이 코드는 객체 클래스를 만들고 다른 클래스 객체를 사용하여 실행해야하는 코드입니다. 이 프로그램의 이름은 bicycle과 bicycletest입니다. 나는 자전거 프로그램 (이미 쓰여졌습니다.)을 받았고 자전거를 이용하기 위해서는 자전거를 써야합니다. 이제 문제는 NiceBicycle 및 CoolBicycle이라는 2 개의 객체를 만들었습니다. 내 NiceBicycle 이름을 "Kenny McCormick"으로 변경해야하지만, 나는이 명령 줄에 대해 "오류 : 변수 NiceBicycle이 초기화되지 않았을 수 있습니다."라고 계속 오류가 발생합니다. 자바 자전거 및 자전거 테스트
// setOwnerName를 사용 케니 맥코믹에 소유자의 이름을 변경 NiceBicycle.setOwnerName("Kenny McCormick");
내가?
어쨌든, 여기에 자전거 번호 및 내가 강사를 기반으로 쓰기 그 bicycletest 명령. 는 귀하의 회신
주셔서 감사해야 할 일 ,bicycle.javapublic 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입니다.
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());
}
}
안녕 모두를 호출 이름을 얻을 수 있습니다. 귀하의 의견을 모두 주셔서 감사합니다, 정말 고마워요. 이 프로그램은 모든 사람들의 조언을 따른 후에 작동합니다. –