Hercles Posted April 16, 2015 at 11:30 PM Report Share #581404 Posted April 16, 2015 at 11:30 PM (edited) Caros, é possível colocar numa variável de array tipos diferentes de objetos. Algo do tipo como coloco abaixo: BonusMes vetor[] = new BonusDia[5] or new bonusExtra[5]; vetor[0] = new BonusDia(1000); vetor[1] = new BonusExtras(500); vetor[2] ... vetor[3] ... vetor[4] ... Edited May 5, 2015 at 07:39 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
HappyHippyHippo Posted April 16, 2015 at 11:44 PM Report Share #581405 Posted April 16, 2015 at 11:44 PM se e só se todos os objectos a serem inseridos no array derivarem do mesmo objecto, podem declarar o array como um array desse objecto derivado IRC : sim, é algo que ainda existe >> #p@p Portugol Plus Link to comment Share on other sites More sharing options...
Hercles Posted April 16, 2015 at 11:49 PM Author Report Share #581406 Posted April 16, 2015 at 11:49 PM Como assim? Tanto BonusDia e BonusExtra são herança de BonusMes. Link to comment Share on other sites More sharing options...
Solution bitsplease Posted May 5, 2015 at 06:09 PM Solution Report Share #582516 Posted May 5, 2015 at 06:09 PM (edited) Hercles, Para inicializar o vetor BonusMes[] v = new BonusMes[2]; Para inserir elementos v[0] = new BonusDia(...); v[1] = new BonusExtras(...); Para executar métodos nos elementos for (BonusMes b : v) { if (b instanceof BonusDia) ((BonusDia) b).op(...); // Igual para BonusExtra. } Edited May 5, 2015 at 07:38 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
Hercles Posted May 6, 2015 at 02:44 PM Author Report Share #582602 Posted May 6, 2015 at 02:44 PM Obrigado pela ajuda, já tinha resolvido a questão. Soma a isso, utilizei conceitos de herança e sobrecarga de construtores... 👍 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now