Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965

    [Java] Ereditarietà e main()

    codice:
    public class Employee{
    	int age;
    	String firstname;
    	String lastname;
    	
    	public Employee(){ // noargs
    	}
    	
    	public Employee(int age, String firstname, String lastname){
    		this.age=age;
    		this.firstname=firstname;
    		this.lastname=lastname;
    	}
    	
    	public void showDetails(){
    		System.out.println("Dipendente: " + firstname + " " + lastname + "\nEta: " + age);
    	}
    	
    	public class Teacher extends Employee{
    		String school;
    		double salary;
    		
    		public Teacher(int age, String firstname, String lastname, String school, double salary){
    			super(age,firstname,lastname);
    			this.school=school;
    			this.salary=salary;
    		}
    		
    		public void showMoreDetails(){
    			System.out.println("He/She is teaching in " + school + " school.\nHis/Her salary is " + salary+ " €");
    		}
    		
    		public void teach(String subject){
    			System.out.println("Teacher is teaching "+subject);
    		}
    		
    		public static void main(String[] args){
    			Employee e1 = new Employee(23,"Mr.","Rossi");
    			e1.showDetails();
    			Teacher t1 = new Teacher(60,"Mrs.","Bianchi","ITIS",1030.5);
    			t1.showDetails();
    			t1.showMoreDetails();
    		}
    		
    	}
    	
    }
    Come mai c'è questo errore?

    codice:
    Employee.java:40: non-static variable this cannot be referenced from a static context
    Teacher t1 = new Teacher(60,"Mrs.","Bianchi","ITIS",1030.5);
                 ^                                     
    Employee.java:37: inner classes cannot have static declarations
    public static void main(String[] args){
                       ^
    Inoltre, se levo 'static' dal main compila correttamente, ma all'esecuzione genera questa eccezione:
    Exception in thread "main" java.lang.NoSuchMethodError: main

    Per la istanziazione/visualizzazione dovrei ricorrere a una classe ausiliaria?

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Teacher.java
    codice:
    class Employee{
    	int age;
    	String firstname;
    	String lastname;
    	
    	public Employee(){ // noargs
    	}
    	
    	public Employee(int age, String firstname, String lastname){
    		this.age=age;
    		this.firstname=firstname;
    		this.lastname=lastname;
    	}
    	
    	public void showDetails(){
    		System.out.println("Dipendente: " + firstname + " " + lastname + "\nEta: " + age);
    	}		
    }
    public class Teacher extends Employee{
    		String school;
    		double salary;
    		
    		public Teacher(int age, String firstname, String lastname, String school, double salary){
    			super(age,firstname,lastname);
    			this.school=school;
    			this.salary=salary;
    		}
    		
    		public void showMoreDetails(){
    			System.out.println("He/She is teaching in " + school + " school.\nHis/Her salary is " + salary+ " €");
    		}
    		
    		public void teach(String subject){
    			System.out.println("Teacher is teaching "+subject);
    		}
    		
    		public static void main(String[] args){
    			Employee e1 = new Employee(23,"Mr.","Rossi");
    			e1.showDetails();
    			Teacher t1 = new Teacher(60,"Mrs.","Bianchi","ITIS",1030.5);
    			t1.showDetails();
    			t1.showMoreDetails();
    		}
    		
    	}
    }
    Oppure
    Employee.java
    codice:
    public class Employee{
    	int age;
    	String firstname;
    	String lastname;
    	
    	public Employee(){ // noargs
    	}
    	
    	public Employee(int age, String firstname, String lastname){
    		this.age=age;
    		this.firstname=firstname;
    		this.lastname=lastname;
    	}
    	
    	public void showDetails(){
    		System.out.println("Dipendente: " + firstname + " " + lastname + "\nEta: " + age);
    	}		
    }
     Teacher.java
    public class Teacher extends Employee{
    		String school;
    		double salary;
    		
    		public Teacher(int age, String firstname, String lastname, String school, double salary){
    			super(age,firstname,lastname);
    			this.school=school;
    			this.salary=salary;
    		}
    		
    		public void showMoreDetails(){
    			System.out.println("He/She is teaching in " + school + " school.\nHis/Her salary is " + salary+ " €");
    		}
    		
    		public void teach(String subject){
    			System.out.println("Teacher is teaching "+subject);
    		}
    		
    		public static void main(String[] args){
    			Employee e1 = new Employee(23,"Mr.","Rossi");
    			e1.showDetails();
    			Teacher t1 = new Teacher(60,"Mrs.","Bianchi","ITIS",1030.5);
    			t1.showDetails();
    			t1.showMoreDetails();
    		}
    		
    	}
    }
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    Allora... devo separare le classi in 2 file separati?
    Dove sta la correzione?

    Se comunque li separo mi restituisce la stessa eccezione del main() e se provo a compilare Teacher.java mi dice che 'Employee' non esiste/è indefinito.

    A me comunque interessava di più una risposta teorica.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.