Dúvida

4 respostas
javatar
Olá eu estou começando a aprender java e estou começando a criar alguns objetos, vou colocar um código abaixo e a dúvida abaixo do código.
class Movie {
	String title;
	String genre;
	int rating;
	
	void playIt () {
		System.out.println("Playing the movie");
	}
}

public class MovieTestDrive {
	public static void main (String[] args) {
		Movie one = new Movie() ;
		one.title = "Gone with the Stock";
		one.genre = "Tragic";
		one.rating = -2;
		Movie two = new Movie ();
		two.title = "Lost in Cubicle Space";
		two.genre = "Comedy";
		two.rating = 5;
		two.playIt ();
		Movie three = new Movie ();
		three.title = "Byte Club";
		three.genre = "Tragic but ultimately uplifting";
		three.rating = 127;
	}
}
Eu criei uma classe Movie e outra MovieTestDrive

agora vem a dúvida cruel como eu salvo (rsrs)
Movie.java
ou
MovieTestDrive.java
pois existem duas classes

Obrigado :)

4 Respostas

bRadoCk
vc tem que separar as classe em 2 arquivos, uma classe vai se a movie
class Movie {  
    String title;  
    String genre;  
    int rating;  
      
    void playIt () {  
        System.out.println("Playing the movie");  
    }  
}
a outra MovieTestDrive
public class MovieTestDrive {  
    public static void main (String[] args) {  
        Movie one = new Movie() ;  
        one.title = "Gone with the Stock";  
        one.genre = "Tragic";  
        one.rating = -2;  
        Movie two = new Movie ();  
        two.title = "Lost in Cubicle Space";  
        two.genre = "Comedy";  
        two.rating = 5;  
        two.playIt ();  
        Movie three = new Movie ();  
        three.title = "Byte Club";  
        three.genre = "Tragic but ultimately uplifting";  
        three.rating = 127;  
    }  
}
javatar

É um código só , eu salvei como MovieTestDrive e ele executou a primeira class , eu estou lendo o livro Usa a Cabeça e agora que começou a parte de OO =)

lucianodacunha.net

...para criar duas classes em um só arquivo vc pode fazer mais ou menos assim...

Nome do arquivo: CriandoDuasClassesEmUmSoArquivo.java
public class CriandoDuasClassesEmUmSoArquivo {
    
    public static void main(String args[]){
        
        MinhaSegundaClasse minhaSegundaClasse = new MinhaSegundaClasse();
        
        System.out.println("...classe construída!");       
        
    }    
}

class MinhaSegundaClasse{}
Procure nomear o arquivo com o nome da classe public. Perceba que somente uma classe poderá ser public nesse caso.

...ao compilar, vc criará dois arquivos .class.

[]'s

javatar

Obrigado khadi =)

Criado 29 de agosto de 2010
Ultima resposta 29 de ago. de 2010
Respostas 4
Participantes 3