2011-10-29 15 views
1

別のクラスからメソッドを呼び出そうとしています。私が収集したメソッドから、呼び出すメソッドはインスタンスメソッドです。私はそれを集めました。つまり、オブジェクトのインスタンス変数を使用しています。このメソッドを呼び出す簡単な方法はありますか?静的コンテキストから非静的メソッドを参照することはできません

これはmainメソッド、

public void main() 
{ 
    Test.testPetOwner(); 
} 

であり、これは私たちがアクセスしようとすると「テスト」

public void testPetOwner() 
{ 
    String petName; 
    String species; 
    String hairCondition; 
    String posture; 

    testCompetitor(); 

    PetOwner petOwner1 = new PetOwner(); 

    System.out.println("What is the pet's name?"); 
    petName = Genio.getString(); 
    petOwner1.setPetName(petName); 

    System.out.println("What is the species of the pet?"); 
    species = Genio.getString(); 
    petOwner1.setSpecies(species); 

    System.out.println("What is the hair condition of the pet?"); 
    hairCondition = Genio.getString(); 
    petOwner1.setHairCondition(hairCondition); 

    System.out.println("How is the pet's posture?"); 
    posture = Genio.getString(); 
    petOwner1.setPosture(posture); 
} 

答えて

1
public void main() 
{ 
    Test t = new Test(); 
    t.testPetOwner(); 
} 
+0

ありがとうございました!あなたが答えを見ると、いつも明白に見えます! – Struan

0

というクラスで呼び出すようにしようとしている方法であり、静的コンテキストからインスタンスメソッドを呼び出すと、コンパイラはどのインスタンスメソッド(オブジェクトの変数)を推測するのか、あなたも参照しています。オブジェクト参照を使用していつでもアクセスできます。

関連する問題