Gurzi Posted December 5, 2006 at 04:39 PM Report #68934 Posted December 5, 2006 at 04:39 PM public class Human { } class Woman extends Human {} class man extends Man{} Human n1 = new man(); man n2 = new man(); A minha dúvida é, qual a diferença do n1 para o n2 ? Se eles herdam os fields e métodos doa classe super(Human) qual a diferença de fazer Human n1 = new man() ou man n1 = new man()?
Rui Carlos Posted December 5, 2006 at 08:11 PM Report #68993 Posted December 5, 2006 at 08:11 PM não te enganaste em nada? não seria public class Human { } class Woman extends Human {} class Man extends Human{} Human n1 = new Man(); Man n2 = new Man(); quanto às diferenças, com o 'n1' tanto pode fazer 'n1=new Man()' como 'n1=new Woman()', enquanto que o 'n2' tem que ser do tipo 'Man'. é útil, por exemplo, se quisesses construir uma agenda onde tinhas contactos de homens e mulheres. em vez de criares uma lista de homens e uma lista de mulheres, criavas uma lista de humanos (que poderia conter homens e mulheres). Rui Carlos Gonçalves
Gurzi Posted December 5, 2006 at 08:38 PM Author Report #69003 Posted December 5, 2006 at 08:38 PM ok, então é usado em situações que não sabemos que dados vamos receber 😉
Rui Carlos Posted December 5, 2006 at 08:48 PM Report #69006 Posted December 5, 2006 at 08:48 PM ok, então é usado em situações que não sabemos que dados vamos receber 😉 não é bem quando não sabes o que vais receber, é mais quando podes receber coisas de vários tipos, mas a ideia é essa. o polimorfismo permite fazer aquilo que em C se faz com uma 'union'. Rui Carlos Gonçalves
Gurzi Posted December 6, 2006 at 12:11 AM Author Report #69078 Posted December 6, 2006 at 12:11 AM Ao fazer um exercício sobre esta matéria deparo-me com 2 erros absurdos que nao os entendo. isto /Users/tiagopalhota/JavaApplication5/src/Bike.java:115: illegal start of expression public static Bike getTheRightBike(String bikeCode) /Users/tiagopalhota/JavaApplication5/src/Bike.java:134: ';' expected } /* * @author Tiago Palhota ( Gurzi ) * * Created on December 5, 2006, 11:26 PM * ############################################ * Objectivo: * * * ############################################ * *--------------------------------------------- * USE THIS FOR FREE . BUT FIRST LEARN *--------------------------------------------- */ public class Bike { public String getModel() { return "Not Aplicable"; } public String getManuFacturer() { return "Not Aplicable"; } public double getCost() { return 0.0; } } class Splendor extends Bike { public String getModel() { return "Splendor"; } public String getManuFacturer() { return "hero Honda"; } public double getCost() { return 45000.0; } } class Pulsar extends Bike { public String getModel() { return "Pulsar"; } public String getManuFacturer() { return "bajaj"; } public double getCost() { return 55000.00; } } class Fiero extends Bike { public String getModel() { return "Fiero"; } public String getManuFacturer() { return "Suzuki"; } public double getCost() { return 520000.00; } } public class BikeShop { public static void main(String[] args) { System.out.println("Welcome to Bike Shop"); System.out.println("--------------"); System.out.println("Models Available"); System.out.println("Splendor .... S"); System.out.println("Pulsar ...... P"); System.out.println("Fiero...... F"); System.out.println("To get details abot a model, enter the code : "); BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); String choice = null; try { choice = dataIn.readLine(); } catch(IOException e) { System.out.println("O Utilizador n introduziu um dado correcto"); } Bike newBike = getTheRightBike(choice); if(newBike == null) { System.out.println("You entered an wrong code\nQuitting*****"); System.exit(1); } System.out.println("Model : " + newBike.getModel()); System.out.println("Manufacturer: " +newBike.getManuFacturer()); System.out.println(" Bike Cost : " + newBike.getCost()); public static Bike getTheRightBike(String bikeCode) { if(bikeCode.equals("S")) { return new Splendor(); } else if(bikeCode.equals("P")) { return new Pulsar(); } else if(bikeCode.equals("F")) { return new Fiero(); } else return null; } } } Alguem me pode dar uma ajuda ? Porque é que ele acusa uma expressão ilegal? e aquele ; é absurdo-..
Hipnoted Posted December 6, 2006 at 12:22 AM Report #69082 Posted December 6, 2006 at 12:22 AM Tenta dizer as linhas onde dão erro, ai tem 115 e 134 mas assim nos não sabemos a linha certa. "Nunca discutas com um idiota. Eles arrastam-te até ao seu nível e depois ganham-te em experiência"
Gurzi Posted December 6, 2006 at 12:37 AM Author Report #69088 Posted December 6, 2006 at 12:37 AM eu disse em cima public static Bike getTheRightBike(String bikeCode) aí aquela do invalid expression e a do ; é ao pé da chaveta a penúltima.
Knitter Posted December 6, 2006 at 10:29 AM Report #69136 Posted December 6, 2006 at 10:29 AM Essa identação está horrível e não estás a respeitar as regras de codificção do Java... mas penso que falta fechar o método "main", falta uma chaveta antes do método "public static Bike getTheRightBike(String bikeCode)" Ou, neste caso, uma das 3 chavetas que se encontram no fim está deslocada, deveria estar antes do método "public static Bike getTheRightBike(String bikeCode)" de forma a fechar correctamente o método "main"... Isso é o que me parece que seja o erro.
Gurzi Posted December 6, 2006 at 12:53 PM Author Report #69152 Posted December 6, 2006 at 12:53 PM Pois, até acho que a dentação se percebe, é à lá Pascal... gosto mais assim... Isso das chavetas já vi umas vinte vezes, vou ver de novo, obrigado
TiagoBernardo Posted December 7, 2006 at 11:38 AM Report #69304 Posted December 7, 2006 at 11:38 AM O erro é que estás a definir a função public static Bike getTheRightBike(String bikeCode) dentro do main, o que não é possível, como já tinha dito o Knitter.
Gurzi Posted December 7, 2006 at 02:48 PM Author Report #69348 Posted December 7, 2006 at 02:48 PM certo, não tinha reparado. Muito Obrigado
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