2014-09-12 3 views
0

내 코드를 작성하는 데 문제가 있습니다. 이 코드는 객체 클래스를 만들고 다른 클래스 객체를 사용하여 실행해야하는 코드입니다. 이 프로그램의 이름은 bicycle과 bicycletest입니다. 나는 자전거 프로그램 (이미 쓰여졌습니다.)을 받았고 자전거를 이용하기 위해서는 자전거를 써야합니다. 이제 문제는 NiceBicycle 및 CoolBicycle이라는 2 개의 객체를 만들었습니다. 내 NiceBicycle 이름을 "Kenny McCormick"으로 변경해야하지만, 나는이 명령 줄에 대해 "오류 : 변수 NiceBicycle이 초기화되지 않았을 수 있습니다."라고 계속 오류가 발생합니다. 자바 자전거 및 자전거 테스트

// setOwnerName를 사용 케니 맥코믹에 소유자의 이름을 변경 NiceBicycle.setOwnerName("Kenny McCormick");

내가?

어쨌든, 여기에 자전거 번호 및 내가 강사를 기반으로 쓰기 그 bicycletest 명령. 는 귀하의 회신

주셔서 감사해야 할 일 ,bicycle.java

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입니다.

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()); 
} 

}

+0

안녕 모두를 호출 이름을 얻을 수 있습니다. 귀하의 의견을 모두 주셔서 감사합니다, 정말 고마워요. 이 프로그램은 모든 사람들의 조언을 따른 후에 작동합니다. –

답변

1

당신은 변경하여 테스트 NiceBicycle에 자전거를 인스턴스화하고 할당해야 소유자의 이름을 변경하려고 :

Bicycle NiceBicycle; 

to :

Bicycle NiceBicycle = new Bicycle("", 0); 

그럼 당신은() 그것을 setOwnerName 수 있습니다 또한

NiceBicycle.setOwnerName("Kenny McCormick"); 

을, 당신은 그래서 만약 NiceBicycle 정말해야, 자바 규칙은 변수 이름은 소문자로 시작하는 것이 좋습니다 것을 niceBicycle주의 Java 규칙을 따르고 싶습니다.

0

NiceBicycle는 (자전거 객체가 생성되지 않은) 초기화되지 않았다.

시도 컴파일러 종류 되

Bicycle NiceBicycle = new Bicycle("",0); 
0

으로

Bicycle NiceBicycle; 

교체. 사용하려고하기 전에 NiceBicycle을 설정하지 않았습니다. 변수가 설정되지 않았습니다.

자바 규칙은 항상 소문자로 시작하는 변수와 대문자로 시작하는 클래스의 이름을 지정하는 것입니다. .은 과부하가 걸리기 때문에 중요합니다. 즉, 많은 다른 것들을 부담합니다.

0

NiceBicycle 개체를 초기화해야합니다.그런 다음이

Bicycle NiceBicycle = new Bicycle("User2", 0);

같은

Bicycle NiceBicycle = new Bicycle("Some value", 9972); 
0

시도 뭔가

0

당신은 자전거 객체 따라서 실제 자전거 인스턴스가 메모리에 아직 작성하지 않은

Bicycle NiceBicycle; 

단지에 대한 참조를 가지고있다. 이름을 설정하기 전에 먼저 실제 인스턴스를 만들어야합니다. 그런 다음 이름을 설정하십시오.

Bicycle NiceBicycle = new Bicycle(null, 0); 
NiceBicycle.setOwnerName("Kenny McCormick"); 
0

Andy 소유자 및 licenseNumber의 이름을 설정할 Bicycle 클래스의 생성자를 생성했습니다. 당신이 사용하는 객체를 만들 때마다
new
키워드를 사용하십시오. 어떤 작업을 수행함에 따라
1은 사용자가 지정한 값에 따라 기본 생성자 또는 오버로드 된 생성자를 호출합니다.
2 메모리를 할당합니다. 즉, 클래스의 객체를 만들 때 제대로 당신이 호출 값을 설정하는 지금
Bicycle NiceBicycle = new Bicycle("",0);
로 할 것 초기화하는 객체를 초기화 해, Setter하지 않았기 때문에
지금 당신이 오류가 발생하는
new 키워드를 사용하는 이유 지금이
NiceBicycle.setOwnerName("SetName")
같은 Getter 방법은
string OwnerName = NiceBicycle.getOwnerName()

관련 문제